Thread: How to complete program. Any probs now?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    How to complete program. Any probs now?

    Hey, I am working on a program that takes in grades of students and find the mean and standard deviation, then prints a histogram. First in my post is the problem, then what i have now. Please help on what i need to add to complete this program to have it run smoothly. Thanks so much you guys.

    Problem:::::

    Background Information: This assignment pertains to statistical analysis for a given data sample. In statistics, a collection of data values is usually referred to as a distribution. A primary purpose of statistical analysis is to find ways to compress the complete set of data into summary statistics that express properties of the distribution as a whole.

    Mean:
    The most common statistical measure is the mean, which is simply the traditional average. For the distribution x1, x2, ¡K xn, the mean is usually represented by the symbol

    Standard Deviation:
    Another common statistical measure is the standard deviation, which provides an indication of how much the individual values in the distribution differ from the mean. To calculate the standard deviation whose elements are x1, x2, ¡K xn you need to perform the following steps:
    a. Calculate the mean of the distribution.
    b. Go through the individual data items in the distribution and calculate the square of the difference between each data value and the mean. Add all these values to a running total.
    c. Take the total from step b and divide it by the number of data items.
    d. Calculate the square root of the resulting quantity, which represents the standard deviation.
    In mathematical form, the standard deviation (ċ) is given by the following formula:
    ċ =

    Histogram:
    A histogram gives us a pictorial view of the data. Basically it is a frequency plot. For example given a histogram of a set of marks of students in a class, we can immediately see whether there were more students in the range 50-54 or 80-84 and so on. To draw a histogram, first decide upon the intervals. Let us say the interval is 5. Then count the number of students getting marks in the intervals 0-4, 5-9, 10-14, 15-19, 20-24, 25-29 and so on. The last interval will contain a single integer. Now you are ready to draw the histogram or the frequency plot. Here is an example of a histogram plot:

    0¡V 4|****************
    5- 9|***********************
    10-14|******************************
    15-19|*************************************
    20-24|**********************
    25-29|*************
    30-34|*********
    35-39|****
    40-44|**
    45-49|*
    50|**

    Description of the assignment: Your program should read in a set of grades (that are all integers) entered by the user. The user will signify the end of the list of grades by entering -999. Do NOT process this value. You can assume that the user will enter at least three grades and not more than 100 grades, all of which will be integers in between 0 and 100, inclusive. For the set of grades, your program should compute the minimum value, print this out to two decimal places and remove it from the data. Then your program should compute the maximum value, print it out to two decimal places and remove it from the data. Then, your program should calculate the mean and standard deviation for the remaining set of grades, and print these out to two decimal places. Finally, a histogram for this data (without the highest and lowest grades) should be printed out as well.

    Your program should include the following functions:

    void readGrades(double grades[], int *n);
    /* This function reads an unknown number of grades (maximum of 100) into the array grades and counts the data items that have been read. You are not supposed to prompt the user to enter the grades. Just use scanf to read in the grades. The end of the input is represented by a negative number ¡V999 */
    Preconditions: An empty array which is initialized to 0 must be passed to the function.
    Postconditions: Array holds the input values and n is the effective size of the array (i.e the number of data items read) */

    void frequency(double grades[], int n)
    /* Given an array of real numbers, this function finds out frequency (number of students) for each interval 0-4, 5-9,¡K..,95 ¡V 99, 100 prints out the frequency values and plots the histogram */



    int maximum(double grades[], int n);
    /* Given an array of real numbers and the effective size of the array (i.e. count of the numbers in the array), this function returns the location of the highest number in the array */

    int minimum(double grades[], int n);
    /* Given an array of real numbers and the effective size of the array (i.e. count of the numbers in the array), this function returns the location of the lowest number in the array */

    void deleteElement(double grades[], int *n, int loc);
    /* Given an array of real numbers, the effective size of the array and the location of the element to be deleted, this function deletes that element from the array and decrements the effective size, n, by one */

    double mean(double grades[], int n);
    /* Given an array of numbers and the effective size of the array this function computes and returns the mean */

    double StandardDeviation(double grades[], int n);
    /* Given an array of numbers and the effective size of the array, this function computes and returns the standard deviation as described in the previous section */


    What i have::::
    Code:
    void readGrades(double grades[], int *n);
    void frequency(double grades[]);
    int maximum(double grades[], int n);
    int minimum(double grades[], int n);
    void deleteElement(double grades[], int *n, int loc);
    double mean(double grades[], int n);
    double StandardDeviation(double grades[], int n);
    
    
      int main() {
    
        int n, loc, sum, i, min, max;
        double grades[100], mean, deviation;
    
      readGrades(grades, &n);
    
      min = minimum(grades, n);
       printf("The data has been adjusted by removing the minimum %d\n", min);   
    
      deleteElement(grades, &n, loc);
    
      maximum(grades, n);
      printf("The data has been adjusted by removing the maximum %d\n", max);
    
      deleteElement(grades, &n, loc);
    
      mean = mean(grades, n);
       printf("The adjusted mean is %lf\n", mean);
    
      StandardDeviation(grades, n);
       printf("the adjusted standard deviation is %lf\n", deviation);
    
      frequency(grades);
    
    }
    
      void readGrades(double grades[], int *n) {
         int i;
       for (i=0; i<100; i=i+1){
        scanf("%f", &n);
        grades[i] = *n;
        }
    
        }
    
      void frequency(double grades[])  {
       }
    
      int maximum(double grades[], int n)  {
         int i, j, max = 0;
           for (i = 0; i < n; i++ )
           for ( j = 0; j < n; j++ )
             if ( grades[i] > max ) 
               max = grades[j]; 
                return max; 
    
        }
    
      int minimum(double grades[], int n)  {
        int i, j, lowGrade = 100; 
          for ( i = 0; i < n; i++) 
          for ( j = 0; j < n; j++)
             if (grades[i] < lowGrade)
                lowGrade = grades[i]; 
                return lowGrade;
    
        }
    
      void deleteElement(double grades[], int *n, int loc)  {
        }
    
      double mean(double grades[], int n)  {
        int i, total = 0;
          for (i = 0; i <= n - 1; i++)
                total += grades[i];
            return (double) total/n;
    
        }
    
      double StandardDeviation(double grades[], int n) {
        }
    only error i have so far compiling is:::

    stat.c.32: called object is not a function.

    line 32 is where i have mean = mean(grades, n);
    in main

    thanks for all your help

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    It's a nice desciption of the problem given to you, but what exactly is your problem with the program that we can help you with. There are so many answers to "..please help on what I need to add".

    So, what is it that you really need help with? Are you having a problem printing the dist. graph, finding the std. dev, etc..

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You have a double mean and a double mean() & I guess the compiler doesn't allow that. Change one of them.

    Actually, I'm a little surprised that this is not allowed, since the function name has () & I would have thought that would allow the compiler to distinguish between them, but clearly the compiler is not allowing this duplication.

    On the other hand, using the same name for a variable and a function is confusing to a reader of the program so it would be better to avoid this practice anyway, even if the compiler allowed it.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    What i need help with.

    i need help with the standard deviation function, the deleteElement function (and make sure it works how it should in main), and having the frequency function work. The functions and descriptions of what they do are given. thanks so much guys

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    please help with those functions

    Please help me with the standard deviation function, the deleteElement function (and make sure it works how it should in main), and having the frequency function work. The functions and descriptions of what they do are given. thanks so much guys. This should be simple but what are they?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well since we don't have access to " this function computes and returns the standard deviation as described in the previous section" I suggest you go away and read it again.

    I'm curious as to how you've managed to produce this code and all of a sudden you're horribly stuck.

    Here's a couple of other points
    1. min = minimum(grades, n);
    deleteElement(grades, &n, loc);

    Perhaps you should do
    deleteElement(grades, &n, min);

    2. maximum(grades, n);
    Perhaps
    max = maximum(grades, n);

    3. this function returns the location of the highest number in the array
    Look at your code for maximum
    ...max = grades[j];
    What you should really be returning is j (the position in the array), not the value at that position.

    Also, your min/max loops are broken. It should be like this
    Code:
    max = grades[0];
    pos = 0;
    for ( i = 1 ; i < n ; i++ ) if ( grades[i] > grades[pos] ) pos = i;
    return pos;
    Deleting an element
    This is so blindingly easy, its not worth explaining how to do it, so I'll just show you
    Code:
    void deleteElement(double grades[], int *n, int loc) {
        grades[loc] = grades[(*n)-1];
        (*n)--;
    }
    1. Move the element from the end of the array to the indicated position
    2. decrement the count
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    What i have so far with errors.

    Hey here is what i have so far. There are no compiling errors. but when i run the file if i enter a whole number like 2 i get an error right away that says "Segmentation fault (coredump)". if i enter a float number like 2.2 i get this error "Bus Error(coredump)". What do these mean.
    Also what is a code to find the statndard deviation. given the array and the number of items in the array? thanks so much for helping me get this far u guys. Also anyone know how to make the histogram that was described in my first post??

    Code:
    /*  
        Program Number: 6
        Title: Statistical analysis
        Date:  11-7-03
    */
    
    #include <stdio.h>
    
    void readGrades(double grades[], int *n);
    void frequency(double grades[]);
    int maximum(double grades[], int n);
    int minimum(double grades[], int n);
    void deleteElement(double grades[], int *n, int loc);
    double mean(double grades[], int n);
    double StandardDeviation(double grades[], int n);
    
      int main() {
    
        int n, loc, sum, i, min, max;
        double grades[100], mainmean, deviation;
      
      readGrades(grades, &n);
      
      min = minimum(grades, n);
       printf("The data has been adjusted by removing the minimum %d\n", min);   
      
      loc = min; 
    
      deleteElement(grades, &n, loc);
    
      max = maximum(grades, n);
       printf("The data has been adjusted by removing the maximum %d\n", max);
    
      loc = max;
     
      deleteElement(grades, &n, loc);
    
      mainmean = mean(grades, n);
       printf("The adjusted mean is %lf\n", mainmean);
    
      StandardDeviation(grades, n);
       printf("the adjusted standard deviation is %lf\n", deviation);
     
      frequency(grades);
    
    }
    
      void readGrades(double grades[], int *n) {
       int i;
          for (i=0; i<100; i++){
        scanf("%lf", &n);
        grades[i] = *n;
        }
    
     
    }
      void frequency(double grades[])  {
        }
    
      int maximum(double grades[], int n)  {
            int i, pos;     
            pos = 0;
            for ( i = 0 ; i < n ; i++ )
            if ( grades[i] > grades[pos] ) 
            pos = i;
            return pos;
    
        }
      
      int minimum(double grades[], int n)  {
        int i, pos;
         pos = 0; 
          for ( i = 0; i < n; i++) 
          if (grades[i] < grades[pos])
                pos = i; 
                return pos;
    
        }
    
      void deleteElement(double grades[], int *n, int loc)  {
        grades[loc] = grades[(*n)-1];
        (*n)--;
        }
      
      double mean(double grades[], int n)  {
        int i, total = 0;
          for (i = 0; i <= n - 1; i++)
                total += grades[i];
             return (double) total/n;
    
        }
      
      double StandardDeviation(double grades[], int n) {
        }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Get a better compiler - one which warns you when you make a mess of printf and scanf

    scanf("%lf", &n);
    grades[i] = *n;


    Should be

    scanf("%lf", &grades[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM