More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions...
Transcript of More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions...
MoreExamplesUsingFunctionsandCommand-LineArgumentsinC++
CS16:SolvingProblemswithComputersILecture#6
ZiadMatni
Dept.ofComputerScience,UCSB
Administrative• CHANGEDT.A.OFFICE/OPENLABHOURS!– Thursday,10AM–12PM MuqsitNawaz– Friday,11AM–1PM XiyouZhou
• LinuxWorkshopTHISWeek!– HFHConferenceRoom(HFH1132)– Friday,April20th,1:00–2:30PM– Materialwillbeputupontheclasswebsite
• Your1stMidtermExamisNEXTTUESDAY(4/24)!!!– Omgomgomgomgomgomgomgomgomgomg
4/19/18 Matni,CS16,Sp18 2
• Tuesday,4/24inthisclassroom• Startsat2:00PM**SHARP**– Pleasestartarriving5-10minutesbeforeclass
• Imayaskyoutochangeseats• PleasebringyourUCSBIDswithyou
• Closedbook:nocalculators,nophones,nocomputers• OnlyallowedONE8.5”x11”sheetofnotes–onesidedonly– Youhavetoturnitinwithyourexam
• Youwillwriteyouranswersontheexamsheetitself.4/19/18 Matni,CS64,Wi18 3
What’sontheMidterm#1?FromtheLectures,including…
• IntrotoComputers,Programming,andC++• VariablesandAssignments• BooleanExpressions
(comparisonofvariables)• InputandOutputonStandardDevices
(cout,cin)• DataTypes,EscapeSequences,
FormattingDecimal• ArithmeticOperationsandtheirPriorities• BooleanLogicOperators• FlowofControl&ConditionalStatements
• Loops:for,while,do-while• TypesofErrorsinProgramming• MultiwayBranchingandtheswitch
command• GeneratingRandomNumbers• FunctionsinC++:
pre-defined,user-definedvoidfunctions,themain()functioncall-by-refvs.call-by-value
• CommandLineInputstoC++Programs
4/19/18 Matni,CS16,Sp18 4
MidtermPrep
1. Lectureslides
2. Labprograms
3. Homeworkproblems
4. Bookchapters1thru5*
*checkwhichlectureslidesgowithit!!4/19/18 Matni,CS16,Sp18 5
4/19/18 Matni,CS16,Sp18 6
ExampleQuestionsforMidterm#1
SampleQuestionMultipleChoice
CompletethefollowingC++codethatissupposedtoprintthenumbers23456(withspacesinbetween):
intc=0;while(_____________){ cout<<c+2<<“”; c++;}
A. c<7B. c>5C. (c+2)<6D. (c+2)!=6E. c<54/19/18 Matni,CS16,Sp18 7
SampleQuestionMultipleChoice
4/19/18 Matni,CS16,Sp18 8
WhatistheexactoutputofthisC++code? intprod(1);for(intm=1;m<=5;m+=2){prod*=m;}cout<<“Totalproductis:”<<prod<<endl;
A. Totalproductis:720B. Totalproductis:90C. Totalproductis:15D. Totalproductis:3E. Totalproductis:1
9
#include<iostream>_____________________________intfindMax2(inta,intb);intmain(){
_____________________________cout<<"Enter2numbers:";
_____________________________cout<<"Thebiggestoftheseis:"<<findMax2(x,y);return0;
}intfindMax2(inta,intb){
returnmax;}
cin>>x>>y;
usingnamespacestd; SampleQuestionShort-AnswerCoding
intx,y;
intmax=a;if(b>a){
max=b;}
CompletethisprograminC++.Usewhat’sgivenascluesforwhat’smissing.
4/19/18 Matni,CS16,Sp18
SampleQuestionCodingSyntax:Find10Mistakes(ignorestyling)
4/19/18 Matni,CS16,Sp18 10
2:Shouldbe:<string>
9:Shouldbe:\n10:Missing;attheend
11:Shouldbe:cin>>word;
14:Mustremovethe;attheend
17:Shouldbe:x++
19:Shouldbe:cout<<endl;return0;
3:Shouldbe:usingnamespacestd;
6:Shouldbe:intnumber,x=0;
16:Shouldbe:cout<<word<<““;
SampleQuestionShortProgram
Ifwelistalltheintegernumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.WriteaC++programthatcanfindthesumofalltheintegermultiplesof3or5belowanynumbernthatisgivenviastandardinput.
12
SampleQuestionShortProgram
Ifwelistalltheintegernumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.WriteaC++programthatcanfindthesumofalltheintegermultiplesof3or5belowanynumbernthatisgivenviastandardinput.
#include<iostream>usingnamespacestd;intmain(){intsum(0),intn;cout<<"Entern:";cin>>n;for(inti=1;i<n;i++){ if((i%3==0)||(i%5==0)) { sum+=i; }}return0;
}
LectureOutline
• UsingandInterpretingGradescope
• MoreExamplesofFunctionsinC++• MoreExamplesofCommand-LineUseinC++
• Makefiles
4/19/18 Matni,CS16,Sp18 13
4/19/18 Matni,CS16,Sp18 14
Interpretation:Studentprobablydidnotaccountforthistypeoferror
Asummaryofwhattestsfailedandwhatpassed
WatchOutFor…
• Theuseofcerrvscout(esp.inthislab)– Usecerrwhenrelayingerrormessages– Usecoutforregularstandardoutput
• Whenyoucreateyourprograms,testthemwithasmanydifferentscenariosand“edgecases”asyoucan– Sothatyoucancatcherrorsandunderstandwhere/whytheyoccur
4/19/18 Matni,CS16,Sp18 15
MoreDEMOS!JJJ
• function_example1.cpp
• function_example2.cpp
• args.cpp
4/19/18 Matni,CS16,Sp18 16
YOURTO-DOs
q STUDYFORYOUREXAM!Jq FinishLab3bynextMondayq PrepareLab4fornextWednesdayq DoHW6bynextTuesday
q GototheLinuxWorkshoponFriday(optional,butrecommended)!q VisitProf’sandTAs‘officehoursifyouneedhelp!
q Callyourparentsandsayhello
4/19/18 Matni,CS16,Sp18 17
4/19/18 Matni,CS16,Sp18 18