test 2
Post on 30-Dec-2015
35 Views
Preview:
DESCRIPTION
TRANSCRIPT
CS 1430: Programming in C++
1
Test 2
Friday
Functions
Arrays
For Loops
• Understand Concepts and Rules
• Memorize Concepts and Rules
• Apply Concepts and Rules to Different Problems
2
3
C++ Functions
Why?
Modularity!
4
C++ Functions• Function Prototype int IndexOfMax(const float s[], int size);
• Function Definition int IndexOfMax(const float s[], int size) { int index = 0; for (int i = 1; i < size; i ++) if (s[i] > s[index]) index = i; return index; }
• Function Call index = IndexOfMax(Test2Scores, numStudents);
5
Function Type
The type of the value returned by a function
float sqrt(float x);int IndexOfMax(const float s[], int size);bool eof();char RomanChar(int romanDigitValue);StudentType GetStudent();void ReadAnswers(char answers[], int numQuestions);
RETURN Statement!Not Parameters!
6
Prog4 Required Function
//---------------------------------------------------// Finds and returns an index of the look-up-mass // in the Masses array. Since the Masses array is // ordered in ascending order by mass, this function // returns the index of the first mass in the masses // array such that lookUpMass is less than or equal // to masses[i]. // On the off chance that no such index can be found, // returns -1. // params: ( ? , ? , ? )//---------------------------------------------------int FindIndexOfMass(const int masses[], int lookUpMass, int size);
7
Prog4 Required Function
//---------------------------------------------------// Returns the largest value in the dim array; // size is how many items are in the dim array. // params: ( ? , ? )//---------------------------------------------------float FindLargestDimension(const int dim[], int size);
8
Prog4 Required Function
//---------------------------------------------------// Returns the girth of a parcel where girth is // calculated as twice the difference of the sum of // the elements in the dim array and the largest of // the dimensions. // params: ( ? , ? )//---------------------------------------------------float Girth( const int dim[], int size);
Function Parameters
• Need to pass data to the function
• May need more than one value out of the function
Not Return Statement!
• Parameters are local variables
• Parameters get values or addresses at function calls
9
Formal Parameters and Actual Parameters
• Formal Parameters
In function prototype and definition
Must specify type with/without &
[] for arrays with/without const
• Actual Parameters
In function call
No type, &, and no [] or const for arrays10
Passing Parameters
• Basic Data Types (char, int, float, string, bool)
Pass by Value (without & in prototype)
Pass by Reference (with & in prototype)
• Arrays (of any data type)
Always Pass by Reference (pointer!)
(Never &)
• (Structure and Class)11
Value Parameters and Reference Parameters
const float REG_HOURS = 40.0;const float OVER_TIME = 1.5;
int main(){ float hours, rate, gross;
GetInput(rate, hours);
gross = GrossPay(rate, hours);
// display result
return 0;}
float GrossPay(float payRate, float hours){ if (hours > REG_HOURS) payRate = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hours * payRate; return payRate;}
main()
GetInput()
void GetInput(float& payRate, float& hoursWorked){ cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked;
return;}
GrossPay()Passing parameters
12.5 50
Passing parameters
Addresses of rate and hours
Return control
With value
Return control
12
In, Out, InOut Parameters
The original value of the actual parameterThe value of the actual parameter before the function call
InThe function uses the original value of the actual parameter, but
does not change its value.
OutThe function does not use the original value of the actual
parameter, but changes its value.
InOutThe function uses the original value of the actual parameter,
and also changes its value.
13
Tracing FunctionsInput: 10.5 53
float GrossPay(float payRate, float hours)void GetInput(float& payRate, float& hoursWorked);
main() GetInput() GrossPay() hours rate gross payRate& hoursWorked& hours payRate ? ? ? ? ? ? ? Addr of Addr of rate hours 10.5 53.0 53.0 10.5 556.5 556.5
14
Arrays• Array Declaration
const int MAX_SIZE = 30;int size = 30;
int nums[30];// Valid?float ArrayA[MAX_SIZE]; // Valid?
float ArrayB[size]; // Valid?
• Memory allocation
• Array Element and Index
• Index starts with 0
• An array element is the same as a single variable
• For Loop on arrayfor (int i = 0; i < size; i ++) cin >> ArrayA[i];
15
Functions on Arrays• Always passing by reference
• No &
• Size is passed separately
• Const for IN parameter
void ReadAnswers(char answers[], int numQuestions);
void ProcessAnswer(const char keys[], const char answers[],
int numQuestion, int& correct, bool& pass);
• Function calls
No []• Passing arrays and passing array elements
average = ArrayAvg(Scores, count);
max = Largest(Scores[0], Scores[1], Scores[2]);
• Linear search
return value or index
16
For Loops
• Same as while loops
• Better on arrays
• Better on Count-Controlled loops
17
Nested For Loops
int result;
for (int i = 4; i < 6; i ++)
{
result = 1;
for (int j = i; j > 1; j --)
result *= j;
cout << “The result: “ << result;
}
18
Tracing
int i, j, result;
for (i = 4; i < 6; i ++)
{
result = 1;
for (j = i; j > 1; j --)
result *= j;
cout << “The result: ”
<< result;
}
Do it yourself now!
i j result
? ? ?
4
1
4
4
3
12
2
24
1
5
1
5
5
4
20
3
60
2
120
1
619
Schedule
• Quiz7-1
• Lab7 & Lab8
• Prog4
• Test 2
20
21
Test 1
Test 2
Test 3
FinalFinal
top related