1. Thank you in advance for any help offered.
2. While I realize math is extremely important to know for programming, I am a beginning student at my school after graduating high school some 15 years ago. I am starting from scratch in math so I will have a solid foundation for learning trig and calc. The C class I am in has elementary algebra as it's prerequisite, however the instructor keeps giving us problems with higher levels of math such as statistics.
3. My code works and is doing what I am asking of it, so my problem must lie in a misunderstanding of the problem itself. I have tried learning the standard deviation function myself but I must be missing a key piece because my answers do not match the online calculators.
4. My assignment is to generate some random numbers, acquire the standard deviation and the mean. My issue is that the standard deviation function is not returning the correct answer. I have tried inserting printf statements to see where the math goes wrong. However since this level of math is beyond my abilities there is little I can do with that information. It seems that the answer goes askew after the line
sum_of_i2 = pow(sum_of_i + i, 2);
the portion of code that needs attention in my opinion is:
sum_of_i2 = pow(sum_of_i + i, 2);
temp = sqrt((sum_of_x2) - (sum_of_i2));
std_dev = temp / SIZE * (SIZE - 1);
However the whole code is posted below if needed.
Code://////////////////////////////////////////////////////// //Libraries #include <stdio.h> #include <ctype.h> #include <conio.h> #include <math.h> #include <stdlib.h> #include <time.h> //////////////////////////////////////////////////////// //CONSTANTS #define SIZE 20 #define WIDTH 5 #define LOW 0 #define HIGH 100 //////////////////////////////////////////////////////// //PROTOTYPES void generate_array(int array[SIZE]); double calculate_mean(int array[SIZE], double); double calculate_stand_dev(int array[SIZE], double); char get_order(char); void organize_array(int array[SIZE]); void display_array(int array[SIZE]); void display_info(double, double); //////////////////////////////////////////////////////// //Program int main() { //Do-While loop exit flag char exit; //Do-While loop for continuous repetition do { //////////////////////////////////////////////////////// //Program body //////////////////////////////////////////////////////// int array[SIZE] = {0}; int j_flag = 0; char order = ' '; double mean = 0.0; double std_dev = 0.0; srand(time(NULL)); generate_array(array); mean = calculate_mean(array, mean); std_dev = calculate_stand_dev(array, std_dev); display_array(array); order = get_order(order); organize_array(array); display_array(array); display_info(mean, std_dev); /////////////////////////////////////////////////////// //Do-While loop exit condition and instructions printf("\n\nWould you like to continue?\n\n"); printf("\nIf so press 'Y' and hit enter\n\n"); printf("\nOtherwise press 'N' to quit.\n\n"); fflush(stdin); scanf("%c",&exit); exit = toupper(exit); while (exit != 'Y' && exit != 'N') { //Validation for exit condition(valid inputs are y/n) printf("Invalid, try again\n"); fflush(stdin); scanf("%c",&exit); exit = toupper(exit); } } while (exit == 'Y'); return 0; } //////////////////////////////////////////////////////// //NUMBER GENERATOR void generate_array(int array[SIZE]) { int i, j, num; for (i = 0; i < SIZE; i ++) { num = rand() % (HIGH - LOW + 1) + LOW; array[i] = num; } } //////////////////////////////////////////////////////// //CALCULATE MEAN double calculate_mean(int array[SIZE], double mean) { double sum = 0.0; int i; for(i = 0; i < SIZE; i ++) { sum = sum + array[i]; } mean = sum / SIZE; return mean; } //////////////////////////////////////////////////////// //CALCULATE STANDARD DEVIATION double calculate_stand_dev(int array[SIZE], double std_dev) { double sum_of_x2 = 0.0; double sum_of_i = 0.0; double sum_of_i2 = 0.0; double temp, num; int i, j; for(i = 0; i < SIZE; i ++) { num = array[i]; sum_of_x2 = sum_of_x2 + pow(num, 2); sum_of_i = sum_of_i + i; } sum_of_i2 = pow(sum_of_i + i, 2); temp = sqrt((sum_of_x2) - (sum_of_i2)); std_dev = temp / SIZE * (SIZE - 1); return std_dev; } //////////////////////////////////////////////////////// //USER PREFERENCE OF DISPLAY ORDER char get_order(char order) { printf("\nHow would you like your numbers displayed?"); printf("\n\nPress A for ascending order."); printf("\nPress D for descending order.\n"); fflush(stdin); scanf("%c",&order); order = toupper(order); while (order != 'A' && order != 'D') { printf("Invalid, try again\n"); fflush(stdin); scanf("%c",&exit); order = toupper(order); } return order; } //////////////////////////////////////////////////////// //ORGANIZATION OF DATA void organize_array(int array[SIZE], char order) { int i,i2; for(i=0; i<SIZE; i++) { for(i2=0; i2<SIZE-1; i2++) { if (order == 'D') { if(array[i2]<array[i2+1]) { int temp = array[i2+1]; array[i2+1] = array[i2]; array[i2] = temp; } } else { if(array[i2]>array[i2+1]) { int temp = array[i2+1]; array[i2+1] = array[i2]; array[i2] = temp; } } } } } //////////////////////////////////////////////////////// //DISPLAY OF ARRAY void display_array(int array[SIZE]) { int i; int Crlf = 0; printf("\n"); printf("The test scores are as follows : \n"); for(i = 0; i < SIZE; i++) { printf("%d ",array[i]); Crlf ++; if(Crlf >= WIDTH) { printf("\n"); Crlf = 0; } } } //////////////////////////////////////////////////////// //DISPLAY OF MEAN AND STANDARD DEVIATION void display_info(double mean, double std_dev) { printf("\n\nThe standard deviation is: %.2f", std_dev); printf("\n\nThe mean is: %.2f", mean); } ///////////////////////////////////////////////////////// //END OF PROGRAM



LinkBack URL
About LinkBacks



ty all