Download presentation
Presentation is loading. Please wait.
1
Arrays & Functions Lesson xx
In this presentation, we’ll talk about passing arrays and functions.
2
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 We are going to learn the following items in this module, how to: 1) Pass one element of an array to a function 2) Pass entire array to function 3) Write a program that passes an array to a function .
3
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 7.16 8.33 17.87 num[0] num[1] num[2] num[3] num[4] num[5] num[99] . . . Here is a piece of code that shows you how to pass 1 element of an array into a function. Outside of main() we have declared a function called squareRoot which takes 1 argument and returns the square root of x as a double. Inside of main(), we have a one-dimensional array called num. On the right side of the slide, we have illustrated the array for you.
4
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 7.16 8.33 17.87 num[0] num[1] num[2] num[3] num[4] num[5] num[99] . . . The statement: sr = squareRoot (num[3]); calls the squareRoot function and passes in the contents of num [3] which is the # The function returns the square root of This is how you pass in 1 element of an array. When you pass one element of the array into a function, it is pass by value.
5
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; } Let’s look at this piece of code which shows you how to pass an entire array to a function. Outside of main(), we have a function declaration: int minimum ( int g[ ] ); We pass an entire array into the function minimum which then returns the smallest # that is contained in the array. The first int is the return type of the function, the name of the function is minimum, the int g[ ] inside of the ( ) tells the computer that we are going to pass in an integer array. In main(), we have declared an array called grade and a variable called smallest.
6
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; } The statement in red: smallest = minimum (grade); is how you call the function. Notice that you put the name of the array grade with no [ ]s when you specify the array to be passed in. Arrays are passed by address. In English, this means that if you change g[4] in the function, grade[4] will be changed in main().
7
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. Let’s write a program that will read in 2-80 digit numbers. We will then pass the 2 numbers into a function which produces the sum of the 2-80 digit numbers .
8
Program Logic- Plan A int a, b, c; cin >> a >> b;
c = a + b; Our 1st impulse is to write the program as shown: declare variables a,b, & c, read the values into a & b, lastly, add a and b together and store the answer in c. However, this won’t work because an int cannot hold 80 digits. This means that we will have to come up with some alternate solution.
9
Program Logic Plan B . . . . . . . . . a[0] a[1] a[2] a[79] a[80] b[0]
a[80] b[0] b[1] b[2] b[79] b[80] c[0] c[1] c[2] c[79] Let’s do it this way, we’ll set up 3 one dimensional arrays that are 81 digits long. We’ll read each digit of the first 80 digit number into 1 element of array a. We’ll do the same for the 2nd 80 digit # except that we’ll read it into array b. Starting at the end of the array, we’ll sum a[80] and b[80] and put the result in c [80]. Of course, we’ll have to take care of stuff like 2 digit numbers and carries. More on that later. c[80]
10
Program Listing int main() { int a[81]= {0}, b[81]= {0}, c[81], i;
void add (int aa[ ],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]; Here is part 1 of the code which sets up 3 arrays and reads in the two 80 digit numbers
11
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 (int aa[ ], 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 ; The 2nd part of the code passes 3 arrays to function add() which produces the sum of the digit numbers. Let’s now go over the program in detail.
12
Set Up Arrays int main() { int a[81]= {0}, b[81]= {0}, c[81], i;
void add (int aa[ ],int bb[ ],int cc[ ]); a[0] a[1] a[2] a[79] b[0] b[1] b[2] b[79] c[0] c[1] c[2] c[79] b[80] c[80] a[80] In the 1st part of main() we set up 3 one dimensional arrays. Array a and b are used to input the digit numbers. Each digit of the 80-digit number will go into 1 element of the array. Array c will be used to store the sum of a and b. We also have a function prototype for the add function. You send in array a, b, and c and function add() indirectly returns the sum of a and b in array c. We have drawn a picture for you. Array a and b are initialized with all 0s. Array c has garbage in it. Not to worry, we’ll replace the garbage with the sum when we execute the add( ) function.
13
Input 1st 80 Digit Number for (i = 1; i < 81; i++) /*read in array a*/ { cout << "enter number " << i << " "; cin >>a[i]; } 9 7 1 8 a[0] a[1] a[2] a[79] b[0] b[1] b[2] b[79] c[0] c[1] c[2] c[79] b[80] c[80] a[80] Here is the code to read in the first 80 digit # into array a. Each digit of the 80 digit number will go into 1 element of array a. The first digit goes into a[1], the 2nd goes into a[2] and the 80th digit goes into a[80]. We start reading in digits into a[1] because we want to use a[0] for the last carry position.
14
Input 2nd 80 Digit Number for (i = 1; i < 81; i++) /*read in array b*/ { cout << "enter number " << i << " "; cin >>b[i]; } 9 7 1 8 a[0] a[1] a[2] a[79] 5 6 3 b[0] b[1] b[2] b[79] c[0] c[1] c[2] c[79] b[80] c[80] a[80] The code for reading in the 2nd 80-digit # is the same except that we use array b instead of array a. Shown here is the array a and b with data read in
15
Call to Function add( ) add (a,b,c); /*call function to add 2 numbers*/ 9 7 1 8 a[0] a[1] a[2] a[79] 5 6 3 b[0] b[1] b[2] b[79] c[0] c[1] c[2] c[79] b[80] c[80] a[80] The statement: add (a,b,c ); tells the computer to call function add ( ) and pass in arrays a, b, and c. a and b have good data in them and c has garbage but, that’s ok. Function add ( ) will store the answer in array c so that we get our answer returned indirectly.
16
Addition Logic 129 +34 ------ 3 1 63 163 A B C
Let’s look at how we do addition by hand. Suppose we want to add In box A, we start on the right and add and we get 13. You write down the 3 and in box B, you can see that we carry the 1. On the 2nd pass, we add and get 6. Since 6 is a 1 digit number, we have no carry. The last step is to add so that = 163. We’ll simulate this in the next section of code.
17
Function add ( ) void add (int aa[ ], int bb[ ], int cc[ ]) 9 7 1 8
9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] When we enter the add( ) function, we have passed in arrays a b and c. Inside the function, the arrays are known as aa, bb, and cc. aa and bb have our 80 digit numbers and cc has garbage.
18
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 carry 9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] 13 cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] In this piece of code, we try to simulate the steps that we do by hand. We set carry to 0 because it is implied that when we add the 2 rightmost digits, carry is 0. Our counter i starts at 80 because when we add numbers, we start at the right hand side of the number. If we add aa[i] + bb[i] + carry in this example, cc[i] will have the # 13 in it.
19
Check to See If Sum is 2 Digits
if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; } 80 i 1 carry 9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] When we have a two digit sum like 13, you write down the 3 and carry the one. This is the code that simulates that situation. If cc[i] is > 9, this implies that cc[i] has 2 digits. cc[i] = cc[i]-10; subtracts 10 from the sum and we are left with 3 in cc[i]. We set the carry flag = to 1 so that when we add aa[79] + bb[79] we will add in a carry of 1.
20
2nd Time Through Loop for (i = 80; i > -1; i--) {
cc[i] = aa[i] +bb[i]+ carry; 79 i 1 carry 9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] When we finish adding aa[80] and bb[80], we decrement i in the for loop and we are ready to add aa[79] + bb[79] + carry. As you can see from the picture, we add and get 5 in cc [79].
21
No Carry Situation if (cc[i] > 9) { cc[i] = cc[i]-10; carry = 1; }
else carry = 0 ; 79 i carry 9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] Since cc[79] is the one digit # 5 we set carry = to 0 in the else branch of the if statement. The process is repeated in the for loop. We add aa[i] + bb[i] + carry and store the sum in cc[i]. If the sum is > 9, we subtract 10 and set carry = to 1. If the sum is < 10, we set carry = 0. We do this for every element of the array from element 80 to element 0.
22
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 carry 9 7 1 8 aa[0] aa[1] aa[2] aa[79] 5 6 3 bb[0] bb[1] bb[2] bb[79] 4 cc[0] cc[1] cc[2] cc[79] bb[80] cc[80] aa[80] When we exit the loop, the array cc will have the sum of the aa and bb.
23
Print the Sum in main( ) cout << "the sum =\n";
for ( i = 0; i< 81; i++) cout << c[i]; return 0; -1 i carry 9 7 1 8 a[0] a[1] a[2] a[79] 5 6 3 b[0] b[1] b[2] b[79] 4 c[0] c[1] c[2] c[79] b[80] c[80] a[80] Upon return to main() our picture looks as follows: array a and b still have our input. Array c has the sum of the two 80 digit numbers which was computed in function add ( ). Remember, changing array cc in function add() changes array c in main () because arrays are passed by address. Now, all we have to do is to print array c to get our answer.
24
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 In this module we learned how to pass one element of a an array into a function. Then we passed an entire array into a function. Finally, we wrote a program that passes arrays to function.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.