Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS

Similar presentations


Presentation on theme: "ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS"— Presentation transcript:

1 ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS

2 Memory Map – What happens after an array is declared & initialized to zero
Address Memory Array element 2400 List[0] 2402 List[1] 2404 List[2] 2406 List[3] 2408 List[4]

3 Review #include <stdio.h> #define MAX_SIZE 10 int main(void) {
int i, sum, x[MAX_SIZE]; sum = 0; printf("Enter %d integers=> ", MAX_SIZE); for (i = 0; i < MAX_SIZE; ++i) { scanf("%d", &x[i]); sum += x[i]; } printf(" n %% of total\n"); for (i = 0; i < MAX_SIZE; ++i) printf("%2d%10.2f\n", x[i], (double)x[i] / sum * 100); return (0);

4 Arrays and pointers For arrays, only the address of the first cell is important as all cells are stored together in the machine. x[1] is stored next-door to x[0]. The array name without any subscript is the address of the first cell (but is not actually a pointer as you cannot modify it). If I have an array declared int temp[4];, temp is the same thing as &temp[0]. temp, as we know, is the address of temp [0], but temp+1 is the address of temp [1], temp+2 the address of temp [2],and so on...

5 Arrays and cells temp being an array, a statement like temp =70;would be illegal as temp would be an address, not a value. With a value, you must always mention which cell to fill, so temp[3]=70; would be correct. The sizeof operator will return the size (in bytes) of the array. printf ("%d", sizeof (temp)); would display 16 on a 32-bit system (4 cells of integers at 32 bits or 4 bytes each).

6 How to Initialize Arrayes?
/* PROGRAM #70 */ /* INITIALIZE TO NON-ZERO */ /* INITIALIZE AT THE TIME OF DECLARATION*/ #include <stdio.h> #define ListSize 5 main( ){ int i; /* Declare and initialize array */ int List[ListSize] = {2, 4, 6, 8, 10}; /*Print content of elements of array */ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %3d.\n", i, List[i]); }

7 Memory Map – What happens after an array is declared & initialized to non-zero values
Address Memory Array element 2400 2 List[0] 2402 4 List[1] 2404 6 List[2] 2406 8 List[3] 2408 10 List[4]

8 INITIALIZE TO NON-ZERO
/* PROGRAM # 71 */ /* ASK FOR USER’S INPUT */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents */ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, List[i]);

9 /* GOOD USAGE OF INDEX */
/* PROGRAM # 72 */ #include <stdio.h> #define asize 10 main( ){ int i, vector[asize]; /* Input array contents from user */ for(i=0; i<asize; i++){ printf("Pls enter vector[%d]: \n",i); scanf("%d",&vector[i]); } /* Print array contents */ for(i= 0; i < asize; i++) printf("vector[%d] = %d\n" , i ,vector[i]);

10 Q? Write a program which fills an array from a file.

11 /* Problem: This program fills an array from a file –ar#3*/
#include <stdio.h> int main (void) { int numbers[10], i; FILE *input; input = fopen("numbers.txt", "r"); /* reading file - filling array */ for (i=0; i<10; ++i) fscanf(input, "%d", &numbers[i]); /* printing the content of array */ printf("The numbers read are: "); printf("%4d", numbers[i]); printf ("\n"); fclose (input); return (0); }

12 Q? /* Input data from user */ /* Print array contents using pointer*/

13 Arrays and Pointers /* PROGRAM # 73 */ /* POINTERS & ARRAYS */
Pointers can be used to access the contents of elements of an array. /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents using pointer*/ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, *(List+i));

14 Working with an array using pointers
Address Pointers memory Array element 2400 *(List+0) 2 List[0] 2402 *(List+1) 4 List[1] 2404 *(List+2) 6 List[2] 2406 *(List+3) 8 List[3] 2408 *(List+4) 10 List[4]

15 NOTE!!! Name of the array is a pointer that’s holding the address of the top of the array, i.e., the address of the first element of the array. Knowing the name of the array and the size of the array, we can access all elements of the array. Pointer is defined as type integer. Incrementing the pointer by one each time, will actually increment it by two since integers take 2 bytes in memory(8bit).

16 Sending arrays to functions
Sending an array to a function is like sending multiple values all at once. The best way is to send the address (the array name alone) to the function so that the function can work with the original array stored in the calling function (main). Let's have a function that finds the largest value in an array of integers and returns the cell number where it was found. Note that we must always send the size of the array as an argument because the function will have no way of knowing it otherwise.

17 Passing Numeric Arrays between calling function & the function
/* PASSING ARRAYS TO FUNCTIONS *//* PROGRAM # 74 */ #include <stdio.h> #define asize 5 void Get_List(int list[ ]); void Print_List(int list[ ]); main( ){ int vector[asize]; /* Call functions */ Get_List(vector); Print_List(vector); } /* To get the elements of the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To printout the elements of the array */ void Print_List(int list[ ]){ for(i=0; i<asize; i++) printf("list[%d] is %d\n", i, list[i]);

18 NOTE!!! When using an array as the argument of a function, we need to use [ ] to indicate that the argument is an array. In this case, we don’t need to mention the size of the array. If we want, we can mention the size of the array. Name of the array is a pointer that points to the 1st element of the array.

19 Q? /* PRINTING AN ARRAY IN REVERSE ORDER */

20 Manipulation of Array Index
/* PRINTING AN ARRAY IN REVERSE ORDER *//* PROGRAM # 75 */ #include <stdio.h> #define asize 10 void Get_List(int list[ ]); void Print_Backward(int list[ ]); main( ){ /* Declare array */ int vector[asize]; /* Call to the functions */ Get_List(vector); Print_Backward(vector); } /* To get the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To print the elements of the array in reverse order */ void Print_Backward(int list[ ]){ printf("\n\nthe entered numbers displayed \n"); printf("in the reverse order of entry:\n"); for(i = asize - 1; i >= 0; i--) printf("List[%d] = %d\n", i, list[i]);

21 NOTE!!! In this program, we DON’T make any changes to the array and its placement in the memory; we simply PRINT the array in reverse order.

22 REVIEW

23 /* GOOD USAGE OF INDEX */
/* PROGRAM # 72 */ #include <stdio.h> #define asize 10 main( ){ int i, vector[asize]; /* Input array contents from user */ for(i=0; i<asize; i++){ printf("Pls enter vector[%d]: \n",i); scanf("%d",&vector[i]); } /* Print array contents */ for(i= 0; i < asize; i++) printf("vector[%d] = %d\n" , i ,vector[i]);

24 Q? Write a program which fills an array from a file.

25 /* Problem: This program fills an array from a file –ar#3*/
#include <stdio.h> int main (void) { int numbers[10], i; FILE *input; input = fopen("numbers.txt", "r"); /* reading file - filling array */ for (i=0; i<10; ++i) fscanf(input, "%d", &numbers[i]); /* printing the content of array */ printf("The numbers read are: "); printf("%4d", numbers[i]); printf ("\n"); fclose (input); return (0); }

26 Q? /* Input data from user */ /* Print array contents using pointer*/

27 Arrays and Pointers /* PROGRAM # 73 */ /* POINTERS & ARRAYS */
Pointers can be used to access the contents of elements of an array. /* PROGRAM # 73 */ /* POINTERS & ARRAYS */ #include <stdio.h> #define ListSize 5 main( ){ int i; int List[ListSize]; /* Input data from user */ for(i=0; i<ListSize; i++){ printf(“Pls enter List[%d]= ”, i); scanf(“%d”, &List[i]); } /* Print array contents using pointer*/ for(i=0; i<ListSize; i++) printf("The content of List[%d] is %d.\n", i, *(List+i));

28 Working with an array using pointers
Address Pointers memory Array element 2400 *(List+0) 2 List[0] 2402 *(List+1) 4 List[1] 2404 *(List+2) 6 List[2] 2406 *(List+3) 8 List[3] 2408 *(List+4) 10 List[4]

29 NOTE!!! Name of the array is a pointer that’s holding the address of the top of the array, i.e., the address of the first element of the array. Knowing the name of the array and the size of the array, we can access all elements of the array. Pointer is defined as type integer. Incrementing the pointer by one each time, will actually increment it by two since integers take 2 bytes in memory(8bit).

30 Sending arrays to functions
Sending an array to a function is like sending multiple values all at once. The best way is to send the address (the array name alone) to the function so that the function can work with the original array stored in the calling function (main). Let's have a function that finds the largest value in an array of integers and returns the cell number where it was found. Note that we must always send the size of the array as an argument because the function will have no way of knowing it otherwise.

31 NOTE!!! When using an array as the argument of a function, we need to use [ ] to indicate that the argument is an array. In this case, we don’t need to mention the size of the array. If we want, we can mention the size of the array. Name of the array is a pointer that points to the 1st element of the array.

32 Q? /* PRINTING AN ARRAY IN REVERSE ORDER */

33 Manipulation of Array Index
/* PRINTING AN ARRAY IN REVERSE ORDER *//* PROGRAM # 75 */ #include <stdio.h> #define asize 10 void Get_List(int list[ ]); void Print_Backward(int list[ ]); main( ){ /* Declare array */ int vector[asize]; /* Call to the functions */ Get_List(vector); Print_Backward(vector); } /* To get the array from the user */ void Get_List(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To print the elements of the array in reverse order */ void Print_Backward(int list[ ]){ printf("\n\nthe entered numbers displayed \n"); printf("in the reverse order of entry:\n"); for(i = asize - 1; i >= 0; i--) printf("List[%d] = %d\n", i, list[i]);

34 NOTE!!! In this program, we DON’T make any changes to the array and its placement in the memory; we simply PRINT the array in reverse order.

35 Q? /* FIND MINIMUM IN AN ARRAY */

36 Finding MIN Value /* FIND MINIMUM IN AN ARRAY *//* PROGRAM # 76 */
#include <stdio.h> #define asize 8 void GetList(int list[ ]); /* Declare functions */ int MinimumValue(int list[ ]); main( ){ int vector[asize]; int index; GetList(vector); printf("The smallest number in the list is %d\n", MinimumValue(vector) ); } /* To get the array from the user */ void GetList(int list[ ]){ int i; for(i=0; i<asize; i++){ printf("Pls enter list[%d]: ",i); scanf("%d",&list[i]); /* To find the minimum */ int MinimumValue(int list[ ]){ int i, minimum; minimum = list[0]; for(i = 1; i < asize; i++) if(list[i] < minimum) minimum = list[i]; return(minimum);


Download ppt "ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS"

Similar presentations


Ads by Google