Created
August 6, 2014 17:29
-
-
Save hedgerh/2dd7496d9319b22531fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*A survey data analysis program that computes the mean, median and mode of the data*/ | |
| #include <stdio.h> | |
| //Function Prototypes | |
| void mean (const int answer[], const int size); | |
| void median(int answer[],const int size); | |
| void mode(int data[], const int size); | |
| void mergeSort(int a[],const int size); | |
| void initializeArray(int array[],const int size); | |
| void printArray(int A[], int size) | |
| { | |
| int i; | |
| for (i=0; i < size; i++) | |
| printf("%d ", A[i]); | |
| printf("\n"); | |
| } | |
| int main(void) { | |
| int size; | |
| printf("Please enter the number of data\n"); | |
| scanf("%d",&size); | |
| /*Initialize the size of the array data*/ | |
| int data[size]; | |
| initializeArray(data,size); | |
| //Process responses | |
| mean(data,size); | |
| median(data,size); | |
| return 0; | |
| } | |
| void mean (const int answer[],const int size) { | |
| int i; | |
| int sum = 0; | |
| //Sum of the data | |
| for (i = 0; i < size; i++) { | |
| sum = sum + answer[i]; | |
| } | |
| //Final answer | |
| //printf("the sum is %d\n", sum); | |
| double mean = sum/ size; | |
| printf("The mean is %.2f\n", mean); | |
| } | |
| //Will call merge sort to sort the array and calculate the median | |
| void median(int answer[],const int size) { | |
| mergeSort(answer,size); | |
| //testing the sorted array | |
| int i; | |
| for ( i = 0 ; i < size; i++) { | |
| printf("The sorted array %d\n",answer[i]); | |
| } | |
| //the median | |
| int median = answer[size/2]; | |
| printf("The median is %d\n",median); | |
| } | |
| void mergeSort(int a[],const int size){ | |
| //if the size is 1 then exit the function | |
| if (size == 1){ | |
| return; | |
| } | |
| //Copy | |
| else { | |
| //Calculating the spliting point | |
| int length1 = size /2; | |
| int length2 = size - length1; | |
| //create the array | |
| int array1[length1]; | |
| int array2[length2]; | |
| int i = 0; | |
| while (i < length1) { | |
| array1[i] = a[i]; | |
| i++; | |
| } | |
| while (i < size) { | |
| array2[i - length1] = a[i]; | |
| i++; | |
| } | |
| //Recursively Sort and merge | |
| printf("Before:\n"); | |
| printArray(array1, length1); | |
| printArray(array2, length2); | |
| mergeSort(array1,length1); | |
| mergeSort(array2,length2); | |
| printf("After:\n"); | |
| printArray(array1, length1); | |
| printArray(array2, length2); | |
| void merge( int array1[], int array2[],int length1, int length2); | |
| return merge(array1,array2,length1,length2); | |
| } | |
| } | |
| void merge( int array1[], int array2[], int length1, int length2) { | |
| //Creating new array size | |
| int newLength1 = length1; | |
| int newLength2 = length2; | |
| int newArray[newLength1 + newLength2]; | |
| //Iterators | |
| int i = 0; | |
| int j = 0; | |
| int k = 0; | |
| while ( ( i < newLength1) && ( j < newLength2) ) { | |
| if ( array1[i] <= array2[j] ) { | |
| newArray[k] = array1[i]; | |
| i = i + 1; | |
| } | |
| else { | |
| newArray[k] = array2[i]; | |
| i = i + 1; | |
| } | |
| k = k +1; | |
| } | |
| } | |
| void initializeArray(int array[], int size) { | |
| int i = 0; | |
| printf("Please enter the data\n"); | |
| for ( i = 0; i < size; i++) { | |
| scanf("%d", &array[i]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment