Code:#include <stdio.h> #include <math.h> #include <stdlib.h> int main () { /* main */ int initial_sum = 0; int first_element = 0; const int min_array_length = 1; const int program_failure_code = -1; const int program_success_code = 0; int number_of_means = 2; int harmonic_mean_one = 1; float sum_left; float sum_right; float arithmetic_mean_left; float arithmetic_mean_right; float arithmetic_mean_both; float root_mean_square_left; float root_mean_square_right; float root_mean_square_both; float harmonic_mean_left; float harmonic_mean_right; float harmonic_mean_both; int* left_list = (int*)NULL; int* right_list = (int*)NULL; int list_length; int element; /* INPUT */ /* list length */ printf("What is the array length?\n"); scanf("%d", &list_length); if (list_length < min_array_length) { printf("That input is not a valid array length! %d < %d\n", list_length, min_array_length); exit(program_failure_code); } /* if (list_length < min_array_length) */ /* left list */ left_list = (int*)malloc(sizeof(int) * list_length); if (left_list == (int*)NULL) { printf("ERROR: can't allocate an int array of length %d.\n", list_length); exit(program_failure_code); } /* if (left_list == (int*)NULL) */ for (element = first_element; element < list_length; element++) { scanf("%d", &left_list[element]); } /* for element */ /* right list */ right_list = (int*)malloc(sizeof(int) * list_length); if (left_list == (int*)NULL) { printf("ERROR: can't allocate an int array of length %d.\n", list_length); exit(program_failure_code); } /* if (right_list == (int*)NULL) */ for (element = first_element; element < list_length; element++) { scanf("%d", &right_list[element]); } /* for element */ /* CALC */ /* arithmetic mean left list */ sum_left = initial_sum; for (element = first_element; element < list_length; element++) { sum_left = sum_left + left_list[element]; } /* for element */ arithmetic_mean_left = sum_left / list_length; /* arithmetic mean right list */ sum_right = initial_sum; for (element = first_element; element < list_length; element++) { sum_right = sum_right + right_list[element]; } /* for element */ arithmetic_mean_right = sum_right / list_length; /* root mean square left list */ root_mean_square_left = sqrt(pow(sum_left, 2)) / (list_length); /* root mean square right list */ root_mean_square_right = sqrt(pow(sum_right, 2)) / (list_length); /* harmonic mean left list */ harmonic_mean_left = (list_length) / (sum_left); /* harmonic mean right list */ harmonic_mean_right = (list_length) / (sum_right); /* arithmetic mean of the arithmetic means */ arithmetic_mean_both = ((arithmetic_mean_left + arithmetic_mean_right) / (number_of_means)); /* root mean square of the root mean squares */ root_mean_square_both = sqrt((pow(root_mean_square_left, 2)) + (pow(root_mean_square_right, 2)) / number_of_means); /* harmonic mean of the harmonic means */ harmonic_mean_both = (number_of_means) / ( ((harmonic_mean_one) / (harmonic_mean_left)) + ((harmonic_mean_one) / (harmonic_mean_right)) ); /* OUTPUT */ printf("The %f values are:\n", list_length); printf("The %f values are:\n", list_length); printf("The arithmetic mean of the left list %f values is %f.\n", list_length, arithmetic_mean_left); printf("The arithmetic mean of the right list %f values is %f.\n", list_length, arithmetic_mean_right); printf("The root mean square of the left list %f values is %f.\n", list_length, root_mean_square_left); printf("The root mean square of the right list %f values is %f.\n", list_length, root_mean_square_right); printf("The harmonic mean of the left list %f values is %f.\n", list_length, harmonic_mean_left); printf("The harmonic mean of the right list %f values is %f.\n", list_length, harmonic_mean_left); printf("The arithmetic mean of the arithmetic means is %f.\n", arithmetic_mean_both); printf("The root mean square of the root mean squares is %f.\n", root_mean_square_both); printf("The harmonic mean of the harmonic means is %f.\n", harmonic_mean_both); free(left_list); left_list = (int*)NULL; free(right_list); right_list = (int*)NULL; } /* main */
The program it supposed to use a data file to input but for now i have a prompt so you can put in a simple one to atleast test it easily. It keeps printing out zeros and garbage. Any help?
thanks!