Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.

Similar presentations


Presentation on theme: "Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers."— Presentation transcript:

1 Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.

2 Arrays Pseudo-code #include int main() { /* declare variables */ /* read in 20 integers */ /* Ask User */ /* display results */ return 0; }

3 Arrays Pseudo-code #include int main() { int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13, i14,i15,i16,i17,i18,i19,i20; int ans; printf(“Enter number 1: “); scanf(“%d”, &i1); printf(“Enter number 2: “); scanf(“%d”, &i2); … /* Ask User */ displayResults(ans,i1,i2,i3,i4,i5,i6,i7,i8,i9, i10,i11,i12,i13,i14,i15,i16,i17,i18, i19, i20); return 0; }

4 Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt] ); cnt++; } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i]); else if( !(nums[i]%2) ) printf(“%d “, nums[i]); return; }

5 Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt++] ); } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i] ); else if( !(nums[i]%2) ) printf(“%d “, nums[i] ); return; }

6 Arrays Referencing Arrays –identifier[index] –NOTE: index can be any expression Examples –int x[10]; –x[2+1] = 5; –x[3] = x[3] + num; (same as x[3] += num;) –x[5] = x[z] + x[3]; –sum = x[3] + x[f(r)];

7 Arrays Memory –int i[5]; –NOTE: Indexing starts with 0! 100104108112116 i[0] i[1]i[2]i[3]i[4]

8 Arrays Initializing arrays –datatype identifier[array_size] = { init list }; Examples –int x[3] = { 2, 4, 8}; –char v[] = { ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ }; 248 x[0] x[1]x[2] aeiou v[0] v[1]v[2]v[3]v[4]

9 Functions, Arrays, and Pointers call-by-reference –Array int f(int n[]); int x[5]; f(x); int f(int n[]); int x[5]; f(&x[0]); int f(int *n); int x[5]; f(x); int f(int *n); int x[5]; f(&x[0]); –Why are these all equivalent?

10 Functions, Arrays, and Pointers Main Data Area x Function f data Area n 200 204 208 212 216

11 Functions, Arrays, and Pointers Pointer and Array equivalence int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9]; for (i = 0; i < 10; i++) printf(“%d “, x[i]);

12 Arrays #include #define SIZE 20 int main() { int i[SIZE]; int cnt = 0, ans = 0; while( cnt < SIZE ) { printf(“Enter number %d: “, cnt+1); scanf(“%d”, &i[cnt++] ); } printf(“\nEnter 0=even, 1=odd: “); scanf(“%d”, &ans); displayResults(ans,i); return 0; } void displayResults( int ans, int nums[] ) { int i; for(i=0; i<SIZE; i++) if( ans ) if( nums[i]%2 ) printf(“%d “, nums[i] ); else if( !(nums[i]%2) ) printf(“%d “, nums[i] ); return; }


Download ppt "Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers."

Similar presentations


Ads by Google