More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions...

18
More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB

Transcript of More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions...

Page 1: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

MoreExamplesUsingFunctionsandCommand-LineArgumentsinC++

CS16:SolvingProblemswithComputersILecture#6

ZiadMatni

Dept.ofComputerScience,UCSB

Page 2: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 3: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

•  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

Page 4: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 5: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

MidtermPrep

1.  Lectureslides

2.  Labprograms

3.  Homeworkproblems

4.  Bookchapters1thru5*

*checkwhichlectureslidesgowithit!!4/19/18 Matni,CS16,Sp18 5

Page 6: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

4/19/18 Matni,CS16,Sp18 6

ExampleQuestionsforMidterm#1

Page 7: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 8: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 9: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 10: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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<<““;

Page 11: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

SampleQuestionShortProgram

Ifwelistalltheintegernumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.WriteaC++programthatcanfindthesumofalltheintegermultiplesof3or5belowanynumbernthatisgivenviastandardinput.

Page 12: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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;

}

Page 13: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

LectureOutline

•  UsingandInterpretingGradescope

•  MoreExamplesofFunctionsinC++•  MoreExamplesofCommand-LineUseinC++

•  Makefiles

4/19/18 Matni,CS16,Sp18 13

Page 14: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

4/19/18 Matni,CS16,Sp18 14

Interpretation:Studentprobablydidnotaccountforthistypeoferror

Asummaryofwhattestsfailedandwhatpassed

Page 15: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

WatchOutFor…

•  Theuseofcerrvscout(esp.inthislab)– Usecerrwhenrelayingerrormessages– Usecoutforregularstandardoutput

•  Whenyoucreateyourprograms,testthemwithasmanydifferentscenariosand“edgecases”asyoucan– Sothatyoucancatcherrorsandunderstandwhere/whytheyoccur

4/19/18 Matni,CS16,Sp18 15

Page 16: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

MoreDEMOS!JJJ

•  function_example1.cpp

•  function_example2.cpp

•  args.cpp

4/19/18 Matni,CS16,Sp18 16

Page 17: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

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

Page 18: More Examples Using Functions and Command-Line Arguments in C++ · More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture

4/19/18 Matni,CS16,Sp18 18