// Alex Launi // Assignment 1 // 1/23/2009 // // EMAIL THIS TOO c21070100@lucas.cis.temple.edu #include #define SIZE 10 void evenOnLeft(int[], int); void printA(int[], int); void reverse(int[], int); float average(int[], int); int* copy_array(int[], int); int main(int argc, char **argv) { int A[] = {9, 21, 321, 117, 27, 64, 13, 9, 12, 208}; printA(A, SIZE); printf("The average of A[] is %f\n", average(A,SIZE)); reverse(A, SIZE); printA(A, SIZE); evenOnLeft(A, SIZE); printA(A, SIZE); return 0; } /* prints A[] inside parentheses with each value separated by commas to stdout */ void printA(int A[], int length) { int i; printf("("); for(i = 0; i < length; i++) { if (i != length - 1) printf("%d, ", A[i]); else printf("%d", A[i]); } printf(")\n"); } /* returns the average of the items in A[] or 0.0 if A is empty */ float average(int A[], int length) { int i; int sum = 0.0; if (length < 1) return 0.0; for (i = 0; i < length; i++) { sum += A[i]; } return (float) sum/length; } /* Reverses the values in A[]. For example, if before the function, A[] contained [1,2,3,4,5], after the function, A[] contains [5,4,3,2,1] */ void reverse(int A[], int length) { int i, j; int* B; B = copy_array(A, length); for (i = length - 1, j = 0 ; i >= 0 && j < length; i--) { A[j] = B[i]; j++; } } /* Moves all of the even integers in A to the left hand side of A and the odd integers to the right hand side For example, if before the function, A[] contained [1,2,3,4,5], after the function A[] could contain [2,4,1,3,5] */ void evenOnLeft(int A[], int length) { int* B; int i1 = 0, i2 = length, j = 0; if (length < 1) return; B = copy_array(A, length); while (j < length) { if (B[j] % 2 == 0) { A[i1] = B[j]; i1++; } else { A[i2] = B[j]; i2--; } j++; } } // FIXME, there has got to be a better way int* copy_array(int A[], int length) { int i; int* B; for (i = 0; i < length; i++) { B[i] = A[i]; } return B; }