/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* reverse2.c (Chapter 8, page 174) */ /* Reverses a series of numbers using a variable-length array - C99 only */ #include int main(void) { int i, n; printf("Reverses a series of numbers using a variable-length array - C99 only"); printf("\nHow many numbers do you want to reverse? "); scanf("%d", &n); int a[n]; /* C99 only - length of array depends on n */ printf("Enter %d numbers: ", n); /* The following code fragment is similar to the slides. for (i = 0; i < n; i++) scanf("%d", &a[i]); */ /* Since a is the address of the 0-th element of the array, (a+i) is the address of the (i-1)th element of the array. Therefore, the following code fragment works similar to code that we commented out above. */ 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; }