#include <stdio.h> #define NMAX 10 void intSwap(int *x, int *y); int getIntArray(int a[], int nmax, int sentinel); void printIntArray(int a[], int n); void reverseIntArray(int a[], int n); int main(void) { int x[NMAX]; int hmny; hmny = getIntArray(x, NMAX, 0); printf("The array was: \n"); printIntArray(x,hmny); reverseIntArray(x,hmny); printf("after reverse it is:\n"); printIntArray(x,hmny); } void intSwap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } void printIntArray(int a[], int n) { int i; for (i=0; i<n; ){ printf("\t%d ", a[i++]); if (i%5==0) printf("\n"); } printf("\n"); } int getIntArray(int a[], int nmax, int sentinel) { int n = 0; int temp; do { printf("Enter integer [%d to terminate] : ", sentinel); scanf("%d", &temp); if (temp==sentinel) break; if (n==nmax) printf("array is full\n"); else a[n++] = temp; }while (1); return n; } void reverseIntArray(int a[], int n) { int i; for(i=0;i<n/2;i++){ intSwap(&a[i],&a[n-i-1]); } }
Reversing An Array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment