CS-361: Control Structures Week 2
-
Upload
teagan-gaines -
Category
Documents
-
view
35 -
download
1
description
Transcript of CS-361: Control Structures Week 2
![Page 1: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/1.jpg)
scis.regis.edu ● [email protected]
CS-361: Control StructuresWeek 2
Dr. Jesús BorregoLead Faculty, COSRegis University
1
![Page 2: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/2.jpg)
Topics
•Homework #1 review•Key Terms•Chapter 6 (pp. 320-321)•Activity #1•Chapter 7 (p. 362)•Appendix F (pp. 1247-1253)•Activity #2•Homework #2
2
![Page 3: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/3.jpg)
Homework 1 Review
• Program 6, page 173 in the textbook• Use coding standards• Review inputs and outputs• Submit Word documentation• Submit to WorldClass/Desire to Learn before
week 2
3
![Page 4: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/4.jpg)
Key Terms
• Compiler – compilador• Hard coded variable – valor usado en vez de
constante• I/O stream – flujo de entradas y salidas• Linker – enlazador• Object code – código objeto• Server – ordenador, servidor, estación de trabajo• Source code – código fuente• Syntax and semantics – sintaxis y semántica• Void type – sin tipo de variable• Whitespace – líneas en blanco, espacio, indentar4
![Page 5: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/5.jpg)
scis.regis.edu ● [email protected]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Chapter 6: User Defined Functions I
5
![Page 6: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/6.jpg)
Ch. 6: User Defined Functions
•Functions are like building blocks•They allow complicated programs to be
divided into manageable pieces •Some advantages of functions:
▫A programmer can focus on just that part of the program and construct it, debug it, and perfect it
▫Different people can work on different functions simultaneously
▫Can be re-used (even in different programs)▫Enhance program readability
6
![Page 7: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/7.jpg)
Predefined Functions
•In algebra, a function is defined as a rule or correspondence between values, called the function’s arguments, and the unique value of the function associated with the arguments ▫If f(x)= 2x + 5, then
f(1)= 7, f(2)= 9, and f(3)= 11
▫1, 2, and 3 are arguments▫7, 9, and 11 are the corresponding values
7
![Page 8: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/8.jpg)
Some Math Functions
•Some predefined mathematical functions are: sqrt(x)pow(x, y)floor(x)
•Predefined functions are organized into separate libraries
•I/O functions are in iostream header•Math functions are in cmath header
8
![Page 9: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/9.jpg)
Sample programs
•PredefinedFunctions.cpp
9
![Page 10: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/10.jpg)
Activity 1
•Type the program on page 323 by hand (Victor)
•Start with your template•Compile and run•Verify your output matches the sample
run shown•Submit the program and output to
WorldClass•Time: 30 minutes
10
![Page 11: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/11.jpg)
11
![Page 12: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/12.jpg)
12
![Page 13: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/13.jpg)
Examples
13
![Page 14: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/14.jpg)
scis.regis.edu ● [email protected]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Chapter 7: User Defined Functions II
14
![Page 15: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/15.jpg)
Void Functions
• Void functions and value-returning functions have similar structures–Both have a heading part and a statement
part• User-defined void functions can be placed
either before or after the function main • If user-defined void functions are placed
after the function main–The function prototype must be placed
before the function main
15
![Page 16: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/16.jpg)
Void Functions II
•A void function does not have a return type▫return statement without any value is
typically used to exit the function early•Formal parameters are optional•A call to a void function is a stand-alone
statement
16
![Page 17: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/17.jpg)
Void Functions III
17
•Function definition syntax:
•Formal parameter list syntax:
•Function call syntax
•Actual parameter list syntax
![Page 18: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/18.jpg)
Void Functions IV
18
![Page 19: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/19.jpg)
Sample programs
•VoidFunctions.cpp•ASCII.cpp•CalcGrade.cpp
▫Make sure you check the output of your programs
•VoidFunc_NoParam.cpp•VoidFunc_Param.cpp
19
![Page 20: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/20.jpg)
Parameters
•Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter
•Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter
20
![Page 21: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/21.jpg)
Parameter list
21
![Page 22: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/22.jpg)
Value Parameters
•If a formal parameter is a value parameter▫The value of the corresponding actual
parameter is copied into it •The value parameter has its own copy of
the data •During program execution
▫The value parameter manipulates the data stored in its own memory space
22
![Page 23: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/23.jpg)
Reference Parameters
• If a formal parameter is a reference parameter– It receives the memory address of the
corresponding actual parameter• A reference parameter stores the address of
the corresponding actual parameter• During program execution to manipulate
data–The address stored in the reference parameter
directs it to the memory space of the corresponding actual parameter
23
![Page 24: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/24.jpg)
Reference Parameters II
• Reference parameters can: –Pass one or more values from a function –Change the value of the actual parameter
• Reference parameters are useful in three situations: –Returning more than one value–Changing the actual parameter–When passing the address would save
memory space and time
24
![Page 25: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/25.jpg)
Sample Program
•Larger.cpp
25
![Page 26: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/26.jpg)
Activity 2
•We (all of us together) will write a program from scratch.
•Review textbook programming assignment #3 on page 172
26
![Page 27: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/27.jpg)
scis.regis.edu ● [email protected]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Appendix F: Header Files
27
![Page 28: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/28.jpg)
Header files
•See HeaderFiles.xlsx▫Headers tab▫Constants tab
28
![Page 29: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/29.jpg)
Sample programs
•CharFunctions.cpp•Strcat.cpp•Strcat1.cpp
29
![Page 30: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/30.jpg)
Activity 3
•Work in groups of 2, but groups should be different from last class.
•Fix program on pages 171-172 so that it returns the correct output.
• Include the header from the coding standards and include the names of the members of the group
•Submit your corrected program to WorldClass
•30 minutes30
![Page 31: CS-361: Control Structures Week 2](https://reader036.fdocuments.in/reader036/viewer/2022062319/56812d0e550346895d91e71e/html5/thumbnails/31.jpg)
Homework 2
•Program 10 on page 174•Follow coding standards•Review input and output
▫Create test cases•Submit to WorldClass/Desire to Learn
before week 3
31