Thread: Help with Standard deviation code

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Help with Standard deviation code

    Hello. I'm fairly new to programming and I've written some code for a program that uses a menu to prompt the user to choose options that correlate with the other. I am having some issue with getting my standard deviation to calculate correctly. Also, if someone could help me figure out a way to make it so the user cannot go straight to standard deviation without first getting the mean, I'd very much appreciate it!!

    Code:
    #include <stdio.h>
    #include <math.h>
    #ifndef FALSE //if not defined
    #define FALSE 0 
    #define TRUE !FALSE 
    #endif 
    
    
    void print_heading();
    
    
    void print_heading()
    {
       char name[32] = "Antonio Lee Jr";
       
       printf("%s\n", name);
       printf("CSC 130 Spring 2016\n");
       printf("Programming Project 5\n");
       printf("Arrays Project");
    }
    
    
    int main()
    {
       print_heading();
    
    
       int done;
       int choice;
       float average, sum = 0, sum2 = 0, std_dev, var;
       int n, c, d, temp, min, max;
       int x[100], b[100];
       
       printf("\n");
       
       printf("Program to process a list of numbers\n");
       
       average = FALSE;
       done = FALSE;
       while(!done) 
       {  /*Command Chooser*/
          printf("\nSelect the desired operation:\n");
          printf("\t0 - Enter list of numbers\n");
          printf("\t1 - Find the smallest number\n");
          printf("\t2 - Find the largest number\n");
          printf("\t3 - Find the average of the numbers\n");
          printf("\t4 - Find the standard deviation of the numbers\n");
          printf("\t5 - Reverse the list of numbers\n");
          printf("\t6 - Rotate the list of numbers to the right\n");
          printf("\t7 - Rotate the list of numbers to the left\n");
          printf("\t8 - Exit the program\n");
          do{
             printf("Choice? ");
             fflush(stdin); //flushes buffer
          } while(scanf("%d", &choice) < 1);
          
          
          switch(choice) /*Command Dispatcher*/
          {
             case 0:
                x[100];
                printf("How many numbers? ");
                scanf("%d", &n);
                for (c = 0; c < n; c++)
                {
                   printf("x[%d] = ", c);
                   scanf("%d", &x[c]);  
                }
                break;
             case 1:
                min = x[0];
                for( c = 0; c < n; c++)
                {
                   if( x[c] < min )
                   {
                      min = x[c];
                   }
                }
                printf("The smallest number is: %d\n", min);
                break;
             case 2:
                max = x[0];
                for( c = 0; c < n; c++)
                {
                   if( x[c] > max )
                   {
                      max = x[c];
                   }
                }
                printf("The largest number is: %d\n", max);
                break;
             case 3: 
                for( c = 0; c < n; c++)
                {
                   sum = sum + x[c];
                   
                }
                average = sum / (float) n;
                printf("The average is: %.2f\n", average);
                average = TRUE;
                printf("Average has been flagged");
                break;
             case 4: 
                for( c = 0; c < n; c++)
                {
                   sum2 = sum2 + pow((x[c] - average), 2);
                }
                var = sum2 / (float) n;
                std_dev = sqrt(var);
                printf("The standard deviation is: %.2f\n", std_dev);
                break;
             case 5:
                for (c = n - 1, d = 0; c >= 0; c--, d++)
                   b[d] = x[c];
                for (c = 0; c < n; c++)
                   x[c] = b[c];
     
                printf("Array is now:\n");
     
                for (c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 6:
                temp = x[n-1];
                for( c = n-1; c > 0; c--)
                {
                   x[c] = x[c-1];
                }
                x[0] = temp;
                
                printf("Array is now:\n");
                
                for( c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 7:
                temp = x[0];
                for( c = 0; c < n - c; c++)
                {
                   x[c] = x[c+1];
                }
                x[n-1] = temp;
                for( c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 8:
                printf("GOODBYE!!");
                done = TRUE;
                break;
             default:
                printf("invalid program option %d selected\n", choice);
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Are you computing the average beforehand? Do you reset the value of sum2? Also, the input to pow is a double, but that shouldn't be an issue at the moment.

    Can you also put a breakpoint or use printf to give you insight into the intermediate calculations?

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    In case 3, the average of the numbers entered by the user is supposed to be calculated. That part works perfectly, but after that when case 4 is chosen, the standard deviation is supposed to utilize the average from case 3 and calculate the standard deviation.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Hello,

    I was looking at the code and the first thing that I noticed as how long the main function is. Writing small meaningful function would save you a lot of time debugging and your code will be more readable. I did not change a lot in the code because I would like for you to understand and code it. Perhaps after this major refactoring is done the bug or the tricky logic you are having problems with will disappear.

    So this is my approach:
    Create a header file to out in there, constants, enumerators, function prototypes.
    Divide the code into smaller functions. Ex each command should be a function.

    So my header (called calculator.h) looks like this:
    Code:
    /* make sure all the items are defined*/
    #ifndef    CALCULATOR_H
    #define CALCULATOR_H
    
    #define FALSE 0 /*false has zero value */
    #define TRUE 1 /*true  has one value*/
    #define MAX_MENU_OPTIONS 8 /* maximum options from menu */
    #define MAX_NUMBERS = 100 /*maximum amount of numbers in array*/
    
    /* function prototypes */
    /* menu and header fucntions */
    void printHeading();
    void printMenu();
    int getMenuOption();
    
    /* program features*/
    void getListOfNumbers(int list[]);
    int getSmallest(int list[]);
    int getLargest(int list[]);
    double getAverage(int list[]);
    int getStandardDev(int list[]);
    void reverseList(int inList[], int outList[]);
    void rotateToRight(int list[]);
    void rotateToLeft(int list[]);
    
    /* auxiliary and other */
    void printList(int list []);
    #endif

    and the file for the mail program like this:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include "calculator.h"
     
    
    int main()
    {
       
       int done;
       int choice;
       float average, sum = 0, sum2 = 0, std_dev, var;
       int n, c, d, temp, min, max;
       int x[MAX_NUMBERS], b[MAX_NUMBERS];
        
       printHeading();
          
       //average = FALSE;
       done = FALSE;
       while(!done) {  
          printMenu();
          choice = getMenuOption();
           
          switch(choice) /*Command Dispatcher*/
          {
             case 0:
                getListOfNumbers(x);
                break;
             case 1:
                min = x[0];
                for( c = 0; c < n; c++)
                {
                   if( x[c] < min )
                   {
                      min = x[c];
                   }
                }
                printf("The smallest number is: %d\n", min);
                break;
             case 2:
                max = x[0];
                for( c = 0; c < n; c++)
                {
                   if( x[c] > max )
                   {
                      max = x[c];
                   }
                }
                printf("The largest number is: %d\n", max);
                break;
             case 3: 
                for( c = 0; c < n; c++)
                {
                   sum = sum + x[c];
                    
                }
                average = sum / (float) n;
                printf("The average is: %.2f\n", average);
                //average = TRUE;
                printf("Average has been flagged");
                break;
             case 4: 
                for( c = 0; c < n; c++)
                {
                   sum2 = sum2 + pow((x[c] - average), 2);
                }
                var = sum2 / (float) n;
                std_dev = sqrt(var);
                printf("The standard deviation is: %.2f\n", std_dev);
                break;
             case 5:
                for (c = n - 1, d = 0; c >= 0; c--, d++)
                   b[d] = x[c];
                for (c = 0; c < n; c++)
                   x[c] = b[c];
      
                printf("Array is now:\n");
      
                for (c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 6:
                temp = x[n-1];
                for( c = n-1; c > 0; c--)
                {
                   x[c] = x[c-1];
                }
                x[0] = temp;
                 
                printf("Array is now:\n");
                 
                for( c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 7:
                temp = x[0];
                for( c = 0; c < n - c; c++)
                {
                   x[c] = x[c+1];
                }
                x[n-1] = temp;
                for( c = 0; c < n; c++)
                   printf("x[%d] = %d\n", c, x[c]);
                break;
             case 8:
                printf("GOODBYE!!\n");
                done = TRUE;
                break;
             default:
                printf("invalid program option %d selected\n", choice);
          }
       }
    }
    
    
    /*Prints the header of the project */
    void printHeading()
    {
       char name[32] = "Antonio Lee Jr";
       
       printf("\n");   
       printf("%s\n", name);
       printf("CSC 130 Spring 2016\n");
       printf("Programming Project 5\n");
       printf("Arrays Project\n");
       printf("Program to process a list of numbers\n");
    }
    
    /*Prints the header of the project */
    void printMenu(){
    
       printf("\nSelect the desired operation:\n");
       printf("\t0 - Enter list of numbers\n");
       printf("\t1 - Find the smallest number\n");
       printf("\t2 - Find the largest number\n");
       printf("\t3 - Find the average of the numbers\n");
       printf("\t4 - Find the standard deviation of the numbers\n");
       printf("\t5 - Reverse the list of numbers\n");
       printf("\t6 - Rotate the list of numbers to the right\n");
       printf("\t7 - Rotate the list of numbers to the left\n");
       printf("\t8 - Exit the program\n");
    
    }
    
    /*gets an option from the menu */
    int getMenuOption(){
    
       int choice = 0;
    
       do{
          printf("%s", "Choice? ");
          fflush(stdin); //flushes buffer
       } while((scanf("%d", &choice) < 1));
    
       return choice;
    
    }
    
    /*gets a list of numbers from the user */
    void getListOfNumbers(int list[]){
    
       int total = 0;
       
       printf("How many numbers? ");
       scanf("%d", &total);
    
       int c;
       for (c = 0; c < total; c++){
          printf("x[%d] = ", c);
          scanf("%d", &list[c]);  
       }
    }
    I hope this enough to get you started

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard Deviation, etc.
    By wovenhead in forum C Programming
    Replies: 12
    Last Post: 02-15-2011, 08:15 PM
  2. Standard deviation
    By Dontgiveup in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 01:46 PM
  3. standard deviation in need help!!!
    By voltare in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 06:46 AM
  4. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM

Tags for this Thread