250 likes | 338 Views
Arrays & Functions Lesson xx. Objective. Pass one element of an array to a function Pass entire array to function Write a program that passes an array to a function. Pass One Element of Array to a Function. double squareRoot (double x);
E N D
Objective • Pass one element of an array to a function • Pass entire array to function • Write a program that passes an array to a function
Pass One Element of Array to a Function double squareRoot (double x); int main() { double num[100] = {6.5,7.16,8.33,17.87}; double sr; sr = squareRoot(num[3]); return 0; } 6.5 num[0] 7.16 num[1] 8.33 num[2] 17.87 num[3] . . . 0 num[4] 0 0 num[5] num[99]
Pass One Element of Array to a Function double squareRoot (double x); int main() { double num[100] = {6.5,7.16,8.33,17.87}; double sr; sr = squareRoot(num[3]); return 0; } 6.5 num[0] 7.16 num[1] 8.33 num[2] 17.87 num[3] . . . 0 num[4] 0 0 num[5] num[99]
Pass an Entire Array to a Function int minimum( int g [ ]); int main() { int grade[100]; int smallest; // code to read in the grades smallest = minimum(grade); return 0; }
Pass an Entire Array to a Function int minimum( int g [ ]); int main() { int grade[100]; int smallest; // code to read in the gradessmallest = minimum(grade); return 0; }
Program Description Write a program that reads in two 80 digit numbers. Pass the 2 numbers to a function that adds the two numbers together.
Program Logic- Plan A int a, b, c; cin >> a >> b; c = a + b;
Program Logic Plan B a[0] c[0] b[0] a[1] c[1] b[1] b[2] a[2] c[2] b[79] a[79] c[79] a[80] . . . . . . . . . b[80] c[80]
Program Listing int main() { int a[81]= {0}, b[81]= {0}, c[81], i; void add (intaa[ ],int bb[ ],int cc[ ]); for (i = 1; i < 81; i++) /*read in array a*/ { cout << "enter number " << i << " "; cin >>a[i]; } cout << "\n"; for (i = 1; i < 81; i++) /*read in array b*/ { cout << "enter number " << i << " "; cin >>b[i]; }
Program Listing Part 2 add (a,b,c); /*call function to add 2 numbers*/ cout << "the sum =\n"; for ( i = 0; i< 81; i++) cout << c[i]; return 0; } /*******************add routine*****************/ void add (intaa[ ], int bb[ ], int cc[ ]) { int carry = 0, i; for (i = 80; i > -1; i--) { cc[i] = aa[i] +bb[i]+ carry; if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; } else carry = 0 ; } }
Set Up Arrays int main() { int a[81]= {0}, b[81]= {0}, c[81], i; void add (intaa[ ],int bb[ ],int cc[ ]); 0 0 0 0 0 0 0 0 0 0 a[0] c[0] b[0] a[1] b[1] c[1] a[2] c[2] b[2] c[79] b[79] a[79] . . . . . . . . . b[80] a[80] c[80]
Input 1st 80 Digit Number for (i = 1; i < 81; i++) /*read in array a*/ { cout << "enter number " << i << " "; cin >>a[i]; } 7 0 0 0 1 0 0 9 0 0 0 8 b[0] a[0] c[0] a[1] c[1] b[1] c[2] b[2] a[2] b[79] c[79] a[79] . . . . . . . . . b[80] a[80] c[80]
Input 2nd 80 Digit Number for (i = 1; i < 81; i++) /*read in array b*/ { cout << "enter number " << i << " "; cin >>b[i]; } 7 0 0 6 1 3 5 9 0 5 0 8 b[0] a[0] c[0] a[1] c[1] b[1] c[2] b[2] a[2] b[79] c[79] a[79] . . . . . . . . . b[80] a[80] c[80]
Call to Function add( ) add (a,b,c); /*call function to add 2 numbers*/ 7 0 0 6 1 3 5 9 0 5 0 8 b[0] a[0] c[0] a[1] c[1] b[1] c[2] b[2] a[2] b[79] c[79] a[79] . . . . . . . . . b[80] a[80] c[80]
Addition Logic 129 +34 ------ 163 129 +34 ------ 3 1 129 +34 ------ 63 C B A
Function add ( ) void add (intaa[ ], int bb[ ], int cc[ ]) 7 0 0 6 1 3 5 9 0 5 0 8 bb[0] aa[0] cc[0] aa[1] cc[1] bb[1] cc[2] bb[2] aa[2] aa[79] bb[79] cc[79] . . . . . . . . . bb[80] aa[80] cc[80]
Set Up Carry & Add 2 Rightmost Digits int carry = 0, i; for (i = 80; i > -1; i--) { cc[i] = aa[i] +bb[i]+ carry; 80 i 7 0 6 0 5 3 1 9 8 0 13 0 5 aa[0] bb[0] cc[0] cc[1] bb[1] aa[1] aa[2] cc[2] bb[2] aa[79] bb[79] cc[79] . . . . . . . . . 0 carry bb[80] aa[80] cc[80]
Check to See If Sum is 2 Digits if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; } 80 i 7 0 6 0 5 3 1 9 8 0 3 0 5 aa[0] bb[0] cc[0] cc[1] bb[1] aa[1] aa[2] cc[2] bb[2] aa[79] bb[79] cc[79] . . . . . . . . . 1 carry bb[80] aa[80] cc[80]
2nd Time Through Loop for (i = 80; i > -1; i--) { cc[i] = aa[i] +bb[i]+ carry; 79 i 7 0 6 0 5 3 5 1 9 8 0 3 5 0 aa[0] cc[0] bb[0] cc[1] bb[1] aa[1] bb[2] aa[2] cc[2] aa[79] bb[79] cc[79] . . . . . . . . . 1 carry bb[80] aa[80] cc[80]
No Carry Situation if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; } else carry = 0 ; 79 i 7 0 6 0 5 3 5 1 9 8 0 3 5 0 aa[0] cc[0] bb[0] cc[1] bb[1] aa[1] bb[2] aa[2] cc[2] aa[79] bb[79] cc[79] . . . . . . . . . 0 carry bb[80] aa[80] cc[80]
Finished Sum for (i = 80; i > -1; i--) { cc[i] = aa[i] +bb[i]+ carry; if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; } else carry = 0 ; } -1 i 7 0 6 1 0 3 5 1 3 5 9 4 1 5 0 8 0 3 cc[0] bb[0] aa[0] cc[1] bb[1] aa[1] cc[2] bb[2] aa[2] aa[79] bb[79] cc[79] . . . . . . . . . 0 carry bb[80] aa[80] cc[80]
Print the Sum in main( ) cout << "the sum =\n"; for ( i = 0; i< 81; i++) cout << c[i]; return 0; -1 i 7 0 6 1 3 0 5 9 3 4 1 5 0 8 1 0 5 3 c[0] b[0] a[0] c[1] b[1] a[1] a[2] c[2] b[2] a[79] b[79] c[79] . . . . . . . . . 0 carry b[80] a[80] c[80]
Summary • Pass one element of an array to a function • Pass entire array to function • Write a program that passes an array to a function