Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions with arrays.

Similar presentations


Presentation on theme: "Functions with arrays."— Presentation transcript:

1 functions with arrays

2 1. array elements as arguments
Array elements are passed as parameters to a function in the same way as simple parameters. Consider this trivial example: #include <stdio.h> int sumf (int num1, int num2); //prototype int main (void) { int sub, x[100]; int sum = 0; // get the values of the array for (sub = 1; sub <= 100; sub++) { printf (“Enter an integer> “); scanf (“%d”, &x[sub]); sum = sumf (sum, x[sub]); // sum = sum + x[sub] }// end for } //end main int sumf (int num1, int num2) { int total; total = num1 + num2; return (total); } //end sumf Dr. Soha S. Zaghloul 2

3 When we declare an array int x[5], the memory layout is as follows:
2. Arrays – step backward When we declare an array int x[5], the memory layout is as follows: Therefore, we can get the address of any array element by adding its subscript to the address of x[0]. x[0] Adrs of x[0] = Array Adrs x[1] Adrs of x[1] = Adrs of x[0] + 1 byte x[2] Adrs of x[2] = Adrs of x[0] + 2 bytes x[3] Adrs of x[3] = Adrs of x[0] + 3 bytes x[4] Adrs of x[4] = Adrs of x[0] + 4 bytes Dr. Soha S. Zaghloul 3

4 3. Arrays – step backward – example
Assume that after declaration, the compiler assigned the address 1000 to the array. Therefore: x[0] Adrs = 1000 x[1] Adrs = 1001 x[2] Adrs = 1002 x[3] Adrs = 1003 x[4] Adrs = 1004 Dr. Soha S. Zaghloul 4

5 4. Arrays as arguments Therefore, it suffices to pass the address of the first element (x[0]) only to a header function in order to pass an array. The following is an example of a function header with the array x as an argument: int example (int x[]) Since the function manipulates the array through its address, then – in contrast to simple types - any change to any of the array elements in the function is reflected in the whole program. Dr. Soha S. Zaghloul 5

6 5. Arrays as arguments – example
Consider the following example that initializes an array list of size size with element n This function will change the values of the array elements in the memory directly using their addresses. Therefore, changes are seen by all the program outside the function. void initarray ( int list[], // this is the array int size, // array size int n) // initialization element { int sub; //the array subscript // loop over all the array for (sub = 0; sub < size; sub++) list[sub] = n; } // end of initarray Dr. Soha S. Zaghloul 6

7 6. Calling initarray – example
#include <stdio.h> void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50]; int num; initarray (x, 10, 0); // init array x of size 10 with 0 printf (“Enter the initialization value>”); scanf (“%d”, &num); initarray (y, 50, num); // init array y of size 50 with num } // end main void initarray (int list[], int size, int n) { int sub; for (sub = 0; sub < size; sub++) list[sub] = n; } // end initarray Dr. Soha S. Zaghloul 7

8 7. Arrays as input arguments
If a function does not intend to modify the array, then we write the following function header: int search( const double list[], int size, double element) The above example is a header for a function called search. It searches for a target element element in an array list of size size. The search function does not intend to modify the contents of the array list. Therefore, in order to avoid any inadvertent (unexpected) changes, we add the qualifier const to the array in the function header. Dr. Soha S. Zaghloul 8

9 8. Arrays as input arguments – example (1)
Write a complete modular program that searches for an element in an array of type double. The array has 100 elements and should not be modified. Dr. Soha S. Zaghloul 9

10 9. Arrays as input arguments – solution
#include <stdio.h> #define SIZE 100 int search(const double list[], int size, double element); //prototype int main (void) { double numbers[SIZE], num; int sub, index; //fill the array for (sub = 0; sub < SIZE; sub++) { printf (“Enter array element> “); scanf (“%f”, &numbers[sub]); } // end for printf (“Enter element you are searching for> “); scanf (“%f”, &num); index = search(numbers, SIZE, num); if (index == -1) printf (“Element not found\n”); else printf (“Element found at index = %d”, index); } // end main int search(const double list[], int size, double element) { int found = 0, i = 0; while (!found && i < size) { if (list[i] == element) found = 1; else i++; } // end while if (found) return i; else return -1; } // end search Dr. Soha S. Zaghloul 10

11 10. Example (2) Write a complete modular program that returns the maximum number in an array of type double. The array has 100 elements and should not be modified. Dr. Soha S. Zaghloul 11

12 11. Example (2) – solution #include <stdio.h> #define SIZE 100
double getmax (const double list[], int size); //prototype int main (void) { double numbers[SIZE], num; int sub; //fill the array for (sub = 0; sub < SIZE; sub++) { printf (“Enter array element> “); scanf (“%f”, &numbers[sub]); } // end for num = getmax(numbers, SIZE); printf (“The maximum element = %f”, num); } // end main double getmax(const double list[], int size) { double max; max = list[0]; for (sub = 0; sub < size; sub++) if (list[sub] > max) max = list[sub]; return max; } // end getmax Dr. Soha S. Zaghloul 12

13 12. Example (3) Write a complete modular program that takes two arrays of size 100 as input parameters, adds them, and stores the result in a third array. The arrays are of type integer. Dr. Soha S. Zaghloul 13

14 13. Example (3) – solution #include <stdio.h> #define SIZE 100
void add2arrays (const int arr1[], const int arr2[], int sum[], int size); //prototype int main (void) { int array1[SIZE], array2[SIZE], array3[SIZE]; int sub; //fill the arrays for (sub = 0; sub < SIZE; sub++) { printf (“Enter array1 element> “); scanf (“%d”, &array1[sub]); printf (“Enter array2 element> “); scanf (“%d”, &array2[sub]); } // end for add2arrays (array1, array2, array3, SIZE); printf (“The summation array is: \n”) printf (“%d \t”, array3[sub]); } // end main void add2arrays(const int arr1[], const int arr2[], int sum[], int size) { int sub; for (sub = 0; sub < size; sub++) sum[sub] = arr1[sub] + arr2[sub]; } // end add2arrays Dr. Soha S. Zaghloul 14

15 Identify the error(s) in the following fragment code: int x[8], i;
14. Exercise (4) Identify the error(s) in the following fragment code: int x[8], i; for (i==0; i <= 8; i++) x[i] = i; int counts[10], i; double x[5]; printf (“Enter an integer between 0 and 4> “); i = 0; scanf (“%d”, &counts[i]); x[counts[i]] = 8.384; //is this valid??? Dr. Soha S. Zaghloul 15

16 15. Reference by-value vs. reference by-address
When the main program calls a function by its value, then the changes are not seen outside the function. This is known as reference by-value. When a program calls a function by its address, then the changes are seen by the whole program outside the function, even the function is of type void. This is known as reference by-address. Arrays are always referred to by addresses. Simple variables may be referred to by values (what we have learnt till now) or by address (to be explained later). Dr. Soha S. Zaghloul 16

17 16. self-check exercise (1)
Write a complete modular program that contains a function called reverse. The function takes an array named x as an input parameter; and an array named y as an output parameter. A third function parameter is size, which is the size of each array. The function should copy the integers in x into y in reverse order. For example, if x[5] = {2, 4, 6, 8, 10} then y[5] = {10, 8, 6, 4, 2} Dr. Soha S. Zaghloul 17

18 17. self-check exercise (2)
Write a complete modular program that contains a function called reverse. The function takes an array named x as an input parameter; and an array named y as an output parameter. A third function parameter is size, which is the size of each array. The function should copy the integers in x into y in reverse order. For example, if x[5] = {2, 4, 6, 8, 10} then y[5] = {10, 8, 6, 4, 2} Dr. Soha S. Zaghloul 18


Download ppt "Functions with arrays."

Similar presentations


Ads by Google