Functions with arrays.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Repetition statements
ARRAYS.
Stack and Heap Memory Stack resident variables include:
UNIT 5 C Pointers.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Hassan Khosravi / Geoffrey Tien
Array An “average.cpp” program
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
New Structure Recall “average.cpp” program
Arrays in C.
Arrays and Records.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
FUNCTIONS WITH ARGUMENTS
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CNG 140 C Programming (Lecture set 8)
Functions.
EKT150 : Computer Programming
1) C program development 2) Selection structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers Call-by-Reference CSCI 230
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Further C Programming Variable types Loops Conditional statements
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
REPETITION STATEMENTS
Initializing variables
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CISC181 Introduction to Computer Science Dr
Arrays I Handling lists of data.
7 Arrays.
CS150 Introduction to Computer Science 1
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Functions Extra Examples.
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
EECE.2160 ECE Application Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

functions with arrays

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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