L8 - 8. ARRAYS Aggregate variables Scalar variables Single...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 8. ARRAYS Aggregate variables Scalar variables Single value Aggregate variables Collection of values Arrays: elements have the same type One-Dimensional Arrays A one-dimensional array is declared in the following way: int a[10]; The number 10 declares that a will have ten elements. Array bounds always start at 0, so a has the following appearance: Notice that 10 is the length of the array, not the arrays upper bound. Array size #define N 10 int a[N]; Array Subscripting To select an element of an array, use the operator: a[0] a[i] a[i*2+1] This operation is called array subscripting. Expressions of the form a[i] are lvalues a[0] = 1; printf("%d\n", a[5]); ++a[i]; Array idioms for (i = 0; i < N; i++) a[i] = 0; /* clears a */ for (i = 0; i < N; i++) scanf("%d", &a[i]); /* reads data into a */ for (i = 0; i < N; i++) sum += a[i]; /* sums the elements of a */ Array index out of bounds int a[10], i; for (i = 1; i <= 10; i++) a[i] = 0; Array subscripts An array subscript may be any integer expression: a[i+j*10] = 0; The expression can even have side effects: i = 0; while (i < N) a[i++] = 0; Wrong i = 0; while (i < N) a[i] = b[i++]; Correct for (i = 0; i < N; i++) a[i] = b[i]; Reversing a series of numbers Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31 In reverse order: 31 50 11 23 94 7 102 49 82 34 /* Reverses a series of numbers */ #include <stdio.h> #define N 100 int main(void) { int a[N], n, i; printf(Enter number of elements: "); scanf("%d",&n); printf("Enter %d numbers: ", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("In reverse order:"); for (i = n - 1; i >= 0; i--) printf(" %d", a[i]); printf("\n"); return 0; } Computing the average #include <stdio.h> #define N 100 int main(void){ int a[N], n, i, sum; printf("Enter number of elements"); scanf("%d", &n); printf("Enter %d numbers: ", n);...
View Full Document

Page1 / 30

L8 - 8. ARRAYS Aggregate variables Scalar variables Single...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online