1 / 12

Arrays

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 */

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 • 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 <stdio.h> int main() { /* declare variables */ /* read in 20 integers */ /* Ask User */ /* display results */ return 0; }

  3. 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

  4. 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; }

  5. 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; }

  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! i[1] i[2] i[3] i[4] i[0] 100 104 108 112 116

  8. 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

  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 <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; }

More Related