Download presentation
Presentation is loading. Please wait.
Published byHollie Norman Modified over 8 years ago
1
Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS], int& numRows, int& numCols) { cin >> numRows >> numCols; for (...) cin >> a[i][j]; } Rows: cols: allScores[][]: ?????? 24 ?????? &numRows: &numCols: a[][]: Return Address 5 24 … 5 …
2
Activation Record 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() 2
3
Activation Record int main() { float hours, rate, gross; GetInput(rate, hours);... } 3 void GetInput(float& payRate, float& hoursWorked) { cin >> payRate; cin >> hoursWorked; return; } hours: rate: gross: ?????? 45 ?????? &payRate: &hoursWorked: Return Address 10 45 10
4
Activation Record int main() { float hours, rate, gross; GetInput(rate, hours); gross = GrossPay(rate, hours);... } 4 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; } hours: rate: gross: 45 10 ? payRate: 10 hours: 45 Return Address 475
5
Activation Record int main() { int num1 = 5, num2 = 9, x = 10; Final(num1, num2, x + num2); return 0; } 5 // Parameters; (,, ) void Final(int& x, int& y, int z) { int temp; temp = z / x; z = 2 * temp - 5; x += z; y = z - x; return; } num1: 5 num2: 9 X : 10 &x : 5 (num1) &y : 9 (num2) z : 19 Temp: ? Return Address 1 6 3 6 -5 ? ? InInOut ? Out
6
Array and typedef float Scores[9]; // an array variable typedef float arrayType[9]; // a (new) data type // a nick name for array of // 9 float elements 6
7
2-D Array and typedef typedef float arrayType[9]; arrayType Scores; // Array of float with 9 elements arrayType allScores[10]; // Array of arrays // 2-D array // Same as float allScores[10][9]; 7
8
2-D Array as Function Parameter float rowAverage(const float nums[][MAX_COLS], int rowIndex, int numCols); // Must specify MAX_COLS! // nums[] is an array // Data Type of array elements: // an array of MAX_COLS floats 8
9
2-D Array as Function Parameter const int MAX_COLS = 9; typedef float arrayType[MAX_COLS]; float rowAverage(const arrayType nums[], int rowIndex, int numCols); // nums[] is an array // Data Type of array elements: arrayType // an array of MAX_COLS floats float rowAverage(const float nums[][MAX_COLS], int rowIndex, int numCols); // Must specify MAX_COLS! // Must specify data type of array elements! 9
10
2-D Array and Array of Array typedef float arrayType[9]; arrayType allScores[10]; // To understand float allScores[10][9]; // To code 10
11
2-D Array and Array of Array float rowAverage(const float nums [], int numCols); float arrayAverage(const float nums [][MAX_COLS], int numRows, int numCols); float nums[MAX_ROWS][MAX_COLS]; float Average; Average = rowAverage(nums[row], cols); // Passing one row as an array Average = arrayAverage(nums, rows, cols); // Passing the entire 2-D array 11
12
Schedule Quiz9-4: 5 points Due 5 pm, Monday Prog6 Due Wednesday Grace Friday Fix Prog5 errors C-String for C++ String Selection Sort 12
13
Final Exam 7:00 – 8:52 PM Thursday, May19, 2011 Doudna 103 100 Points Comprehensive #2 pencil 13
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.