120 likes | 208 Views
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. Arrays. Pseudo-code. #include <stdio.h> int main() { /* declare variables */ /* read in 20 integers */ /* Ask User */
E N D
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.
Arrays • Pseudo-code #include <stdio.h> int main() { /* declare variables */ /* read in 20 integers */ /* Ask User */ /* display results */ return 0; }
Arrays #include <stdio.h> 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; } • Pseudo-code
Arrays #include <stdio.h> #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; }
Arrays #include <stdio.h> #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; }
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)];
Arrays • Memory • int i[5]; • NOTE: Indexing starts with 0! i[1] i[2] i[3] i[4] i[0] 100 104 108 112 116
Arrays • Initializing arrays • datatype identifier[array_size] = { init list }; • Examples • int x[3] = { 2, 4, 8}; • char v[] = { ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ }; x[1] x[2] x[0] 2 4 8 v[1] v[2] v[3] v[4] v[0] a e i o u
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?
Functions, Arrays, and Pointers Main Data Area x Function f data Area n 200 204 208 212 216
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]);
Arrays #include <stdio.h> #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; }