1 / 45

ARRAYS

ARRAYS. Definition. is a sequence of contiguous memory locations that each contains data of the same type. Each value is referred to as an element of the array only one name is assigned to an entire array individual elements are accessed by specifying the array name and its subscript(index).

odelia
Download Presentation

ARRAYS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ARRAYS

  2. Definition • is a sequence of contiguous memory locations that each contains data of the same type. • Each value is referred to as an element of the array • only one name is assigned to an entire array • individual elements are accessed by specifying the array name and its subscript(index)

  3. One Dimensional Arrays • Syntax data type array-name [ number of elements ] • Example int numbers [ 7 ]; declares an array named numbers that contains seven int type elements

  4. char Grades [5]; Prices[0] Grades[0] Prices[1] Grades[1] Prices[2] Grades[2] Prices[3] Grades[3] Grades[4] Conceptual View int Scores [6]; float Prices [4]; Scores[0] Scores[1] Scores[2] Scores[3] Scores[4] Scores[5]

  5. Array name Array Length Each Element Size Each Element Type Array Size ____________________________________________________________________ Scores 6 2 bytes int 12 bytes Prices 4 4 bytes float 16 bytes Grades 5 1 byte char 5 bytes NOTES the length of an array is the number of data elements in the array, indicating the number of cells the size of an array is the array length times the size of an element, indicating number of bytes

  6. Accessing Array Element • Accessing array elements means to be able to store values in and retrieve values from each array element • each element is assigned a unique number called subscript or index • index 0 is assigned to the first element

  7. Notes • C doesn’t warn you when an array subscript exceeds the length of the array • index assigned to first element in an array is number 0; thus the last index is 1 less than the length of the array • any integer number can be used as the array index • an individual array element can be used anywhere that a simple variable is used.

  8. Examples Scores[5]; /* accessing the sixth element of the array Scores */ Grade[0]; /* accessing the first element of the array Grade */ Prices[1]; /* accessing the sixth element of the array Prices */ int offset=3; Scores[offset+2]; /* accessing the sixth element of the array Scores */ Grade[offset-2]; /* accessing the second element of the array Grade */ Prices[++offset]; /* accessing the forth element of the array Prices */

  9. More examples • The assignment statement Scores[1]=10; • increment operator ++ Scores[2]; • decrement operator --Scores[2]; • assignment operator keep=Scores[5]; • printf statement printf(“%d”, Scores[1]);

  10. Array Initializations • int table[4]={10, 20, 30, 40}; • char alphabet[3]={‘A’, ‘B’, ‘C’}; • int table[ ]={10, 20, 30, 40}; • the array length may be omitted when initializing values are provided in the declaration statement. • int table[100] = {0} • initializes the whole array, 100 elements, to zero

  11. int Scores [6]; Prices[0] float Prices [4]; Prices[1] 11.09 99 99 Scores[0] Prices[2] 87 9.88 Scores[1] Prices[3] 18.67 91 Scores[2] 7.08 55 Scores[3] 67 Scores[4] 75 Scores[5] Conceptual View • int Scores[6]={99,87,91,55,67,75} • float Prices[4]={11.09, 9.88, 18.67, 7.08}

  12. Program Example /* Definition: This program shows the array declaration, initialization, and accessing the array elements. */ # include <stdio.h> int main ( ) { /* declaring and initializing an array */ int Scores[ 6 ] = { 99, 87, 91, 55, 67, 75 } ; int i ; /* reading data from the array */ for ( i = 0 ; i < 6 ; ++i ) printf ( " Scores [%d] ------> %d\n", i, Scores [ i ] ) ; return 0 ; }

  13. Program Output Scores[0] -----> 99 Scores[1] -----> 87 Scores[2] -----> 91 Scores[3] -----> 55 Scores[4] -----> 67 Scores[5] -----> 75

  14. Using loop int table[100]; /*declare an int type array that contains 100 elements */ int i; /* i is the loop counter */ for(i=0; i<100; ++i) table[i]=-1; /*assign -1 to the ith element of the array */

  15. Array /* initializing an array to characters A-Z Version 1*/ char table[26]; int i; char ch; for(i=0, ch=‘A’; ch<=‘Z’; ++i, ch++) table[i]=ch; /* initializing an array to characters A-Z Version 2*/ char table[26]; int i=0; char ch=‘A’; for(; ch<=‘Z’; ) table[i++]=ch++;

  16. Address 2000 2002 2004 2006 X 20 2008 name of the variable 2010 Arrays and Pointers int X=20; address of X &X = = 2006 X = = 20 Address of the variable &X Value of the variable

  17. Array Elements Address Computation &Scores[index] == &Scores[0]+(index *2) Examples &Scores[3]==&Scores[0]+(3*2)

  18. Accessing Array Elements Using Pointer int *P_Scores; int Scores[6]={99,87,91,55,67,75}; P_Scores = &Scores[0]; /* assign array address to a pointer variable */ Scores[0] = = *P_Scores == 99 Scores[3] = = *(P_Scores + 3) = = 55

  19. P_Score Address of Scores[0] 99 99 *P_Scores Scores[0] 87 *(P_Scores+1) Scores[1] 91 Scores[2] *(P_Scores+2) 55 Scores[3] *(P_Scores+3) 67 Scores[4] *(P_Scores+4) 75 *(P_Scores+5) Scores[5] Conceptual View int Scores[6]={99,87,91,55,67,75}; The indirect access method The direct access method

  20. Array Access Using Subscript and Pointer Notations int Scores[6] = {99,87,91,55,67,75}; int *P_Scores=&Scores[0]; Array Subscript Pointer Contents of Element Notation Notation the Element 0 Scores[0] *(P_Scores+0) 99 1 Scores[1] *(P_Scores+1) 87 2 Scores[2] *(P_Scores+2) 91 3 Scores[3] *(P_Scores+3) 55 4 Scores[4] *(P_Scores+4) 67 5 Scores[5] *(P_Scores+5) 75

  21. Program Example /* Definition: This program shows the array declaration, initialization, and indirect (pointer) method of accessing the arrays elements. */ # include <stdio.h> int main ( ) { /* declaring and initializing the array */ int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75} ; int i ; /* declare an int variable */ int * P_Scores ; /* declare an int type pointer */ P_Scores = & Scores[ 0 ]; /* assign the array starting address */ for ( i = 0 ; i < 6 ; ++i ) printf ( " Scores [%d] ------> %d\n", i, * (P_Scores + i ) ); return 0 ; }

  22. Program Output Scores[0] -----> 99 Scores[1] -----> 87 Scores[2] -----> 91 Scores[3] -----> 55 Scores[4] -----> 67 Scores[5] -----> 75

  23. Name of the array • &Scores = = Scores equivalent P_Scores = &Scores[0]; • Scores = &Scores[1]; /*invalid assignment */

  24. Notes • An array name is a pointer constant and the address of a pointer constant cannot be obtain. Any attempt to use & with an array . • The address stored in the array name, a pointer constant, cannot be changed • The address stored in the array name is the address of the array’s first element, thus Score = = &Scores[0]

  25. Accessing Array Element Array Subscript Pointer Contents of Element Notation Notation(Array name) the Element 0 Scores[0] *(Scores+0) 99 1 Scores[1] *(Scores+1) 87 2 Scores[2] *(Scores+2) 91 3 Scores[3] *(Scores+3) 55 4 Scores[4] *(Scores+4) 67 5 Scores[5] *(Scores+5) 75

  26. Program Example /* Definition: This program shows the array declaration, initialization, and indirect (using the array name as pointer) method of accessing the arrays elements. */ # include <stdio.h> int main ( ) { /* declaring and initializing the array */ int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ; int i ; /* declare an int variable */ for ( i = 0 ; i < 6 ; ++i ) printf ( " Scores [%d] ------> %d\n", i, * (Scores + i ) ) ; return 0 ; }

  27. Program Example /* Definition: This program shows the array declaration, initialization, and indirect (using the pointer in the script notation) method of accessing the arrays elements.*/ # include <stdio.h> int main ( ) { /* declaring and initializing the array */ int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ; int i ; /* declare an int variable */ int * P_Scores = Scores ; /* declare and initialize an int type pointer */ for ( i = 0 ; i < 6 ; ++i ) printf ( " Scores [%d] ------> %d\n", i, P_Scores [ i ] ); return 0 ; }

  28. POINTER ARITHMETIC

  29. Pointer Arithmetic • A pointer is a valid operand only for the addition and substraction operators • In performing arithmetic on pointers we should be careful to produce addresses that point to something meaningful

  30. P_Scores 2000 2000 Scores[0] 2002 Scores[1] The array name also point to the starting address of the array Scores[2] 2004 Scores[3] 2006 2008 Scores[4] Scores[5] 2010 Array’s Conceptual View Memory int Scores[6]; int *P_Scores; P_Scores = Scores

  31. Common Error • Attempting to obtain the address of a pointer constant. P_Scores = &Scores is invalid, should be P_Scores = Scores;

  32. Pointer Manipulation Examples /* Pointer current Value is 2000 */ Pointer Type Expression New Address Expression New Address char (1 byte) Pointer ++; 2001 -- Pointer; 1999 int (2 byte) ++ Pointer; 2002 -- Pointer; 1998 float (4 bytes) Pointer ++; 2004 -- Pointer; 1996 double (8 bytes) ++Pointer; 2008 -- Pointer; 1992 int (2 bytes) Pointer +=5; 2010 Pointer -=5; 1990 char (1 byte) Pointer +=5; 2005 Pointer -=5; 1995

  33. Examples int Scores[6]; /*declare an array */ int R, P = Scores; /* declare and initialize a pointer variable */ Alternative Statement Statement Explanation R = *++P R = * (++P) Increment the pointer P, and then assign the value pointed to by P to R R = *--P R = * (--P) Decrement the pointer P, and then assign the value pointed to by P to R R = *P++ R = * (P++) Assign the value ponited to by P to R and then increment the pointer P R = *P-- R = * (P--) Assign the value pointed by P to R, and then decrement the pointer R = ++*P R = ++ (*P) Increment the value pointed to by P, and then assign the new value to R; the pointer P remains unchanged

  34. Examples int Scores[6]; /*declare an array */ int R, P = Scores; /* declare and initialize a pointer variable */ Alternative Statement Statement Explanation R = --*P R = -- (*P) Decrement the value pointed to by P and then assign the new value to R; the pointer P remains unchanged. R = (*P)++ Retrieve the value pointed to by the pointer P, and then increment the value pointed to by the pointer P; the pointer P remains unchanged R = (*P) Retrieve the value pointed to by the pointer P, and then decrement the value pointed to by the pointer P; the pointer P remains unchanged

  35. Program Example This program shows the array declaration, initialization, and indirect (using a pointer ) method of accessing the arrays elements. # include <stdio.h> /* including the stdio library */ int main( ) { /* declaring and initializing the array */ int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ; int i ; /* declare an int variable */ int * P_Scores ; /* declare an int type pointer */ P_Scores = Scores ; /* assign the array starting address */ for ( i = 0 ; i < 6 ; ++i ) printf ( " Scores [%d] ------> %d\n", i, * P_Scores++ ) ; return 0 ; }

  36. Pointer Comparison int Scores[6], *P_Scores; P_Scores = Scores; P_Scores == &Scores /* comparing the address of the 1st element */ P_Scores + 5 == &Scores[5] /* comparing the address of the fifth element */ P_Scores < &Scores[1] /* comparing the addresses of the first and second elements */

  37. Program Example This program shows the array declaration, initialization, indirect (using the pointer) method of accessing the arrays ele-ments, and pointers comparisons. # include <stdio.h> /* including the stdio library */ int main( ) { /* declaring and initializing the array */ int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ; int i = 6 ; /* declare and initialize an int variable */ int * P_Scores ; /* declare an int type pointer */ P_Scores = &Scores[5]; /* assign the address of the array last element */ while ( P_Scores >= Scores ) printf ( " Scores [%d] --> %d\n", --i, * P_Scores -- ) ; return 0 ; }

  38. Passing arrays as function argument Function declaration float average(int, int, int); printf (“%.2f\n”, average(table[0], table[1],table[2])); float average(int n1, int n2, int n3) Function call Function header

  39. Starting address of the array ----- creating the array table[0] nums[0] Referencing the same array nums[1] table[1] nums[2] table[2] in average in main Passing an Array to a Function … int table [ ] = {10, 20, 30} float average (int nums [ ]); ... … printf(“%.2f\n”, average (table)); return 0; … float average(int nums[ ]) { return …; }

  40. Passing arrays as function argument Function declaration float average(int[ ]); printf (“%.2f\n”, average(table)); float average(int nums[ ]) Function call Function header

  41. Program Example....continue This program shows the array declaration, initialization,and different ways of passing an entire array to a function. # include <stdio.h> /* including the stdio library */ # define LEN 4 /* create a constant LEN */ int main ( ) { int table [ 4] ; /* declaring an int type array */ void input ( int [ ] ) ; /* declaring the function input */ int largest ( int [ ] ) ;/* declaring the function largest */ void display ( int * ) ;/* declaring the function display */ /* call functions */ input ( table ) ; /* read input data */ display ( &table[0] ) ; /* display the array */ printf ( "The largest is %d \n", largest ( table ) ) ; /* display the largest */ return 0 ; }

  42. ...continue /* input: This function receives an array address. It prompts the user to enter 4 integer numbers and saves them in the array elements. */ void input ( int * ptr ) { int i ; /* declare the loop counter variable */ printf ( ”Enter 4 numbers\n" ) ; /* informative message */ for ( i = 0 ; i < LEN ; i++, ptr ++ ) { printf ( "#%d: ", i + 1 ) ;/* prompt for number */ scanf ( "%d", ptr ) ; /* read and save one number */ } return ; /* end of function */ }

  43. ...continue /* Largest: This function receives an array address, finds the largest of the array elements, and returns it to the caller. */ int largest ( int table [ ] ) { int index; /* the loop counter */ /*declare Lnum and initialize it to the value of the array 1st element */ int Lnum = table [ 0] ; for ( index = 1 ; index < LEN ; ++index )/* iterate 4 times */ if ( Lnum < table [index] ) /* if a new larger number */ Lnum = table [index] ; /* assign the new value */ return ( Lnum ) ; /* return the largest */ }

  44. ...end of program example /* display: This function receives an array address, and displays the array elements. */ void display ( int array [ ] ) /* array notation is used in the parameter */ { int i ; /* the loop counter */ for ( i = 0 ; i < 4 ; ++i ) printf ( "[%d] ", array [ i ] ) ; putchar ( '\n' ) ; return ; }

  45. Program output Enter four numbers #1: 10 #2: 5 #3: 6 #4: 40 [10][5][6][40] The largest is 40

More Related