Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Arrays.

Similar presentations


Presentation on theme: "Programming Arrays."— Presentation transcript:

1 Programming Arrays

2 Array Declaration -- Ar 4 5 6 3 2 8 9 7 1 1 2 3 4 5
// array of 10 uninitialized ints int Ar[10]; -- Ar 4 5 6 3 2 8 9 7 1 1 2 3 4 5

3 Subscripting -- 1 Ar 4 5 6 3 2 8 9 7 Ar[4] Ar[5] Ar[6] Ar[3] Ar[0]
// array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- 1 Ar 4 5 6 3 2 8 9 7 Ar[4] Ar[5] Ar[6] Ar[3] Ar[0] Ar[2] Ar[8] Ar[9] Ar[7] Ar[1]

4 Array Element Manipulation Ex. 3
Consider int Ar[10], i = 7, j = 2, k = 4; Ar[0] = 1; Ar[i] = 5; Ar[j] = Ar[i] + 3; Ar[j+1] = Ar[i] + Ar[0]; Ar[5+3] = 12; Scanf(“%d”,&Ar[k]); // where the next input value is 3 -- 8 6 1 Ar 5 3 12 4 2 9 7 Ar[4] Ar[5] Ar[6] Ar[3] Ar[0] Ar[2] Ar[8] Ar[9] Ar[7] Ar[1]

5 Array Initialization Ex. 4
int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; Ar[3] = -1; 8 7 6 9 Ar 4 3 2 5 1 6 -1 8 7 -1 9 Ar 4 3 2 5 1 6

6 Initializing Arrays double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; OR double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

7 Example #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; for (j = 0; j < 10; j++ ) { printf("Element[%d] = %d\n", j, n[j] ); } return 0; Output Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109


Download ppt "Programming Arrays."

Similar presentations


Ads by Google