Thread: Finding the mean

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    Finding the mean

    Hi
    im new to programming, and have been trying to create a small program that can find the mean of a set of numbers from an array, the problem i face is that when i run the program instead of the correct answer appearing, a random set of numbers appears usually something like -827427429 etc, does anyone know what i have done wrong, if it helps im using visual express c++ 2010

    Code:
    #include <stdio.h>
    #define Size 11
    
    
    int main(void)
    {
    int y;
    int i;
    int average = 0;
    int sum = 0;
    int mean[Size]={13, 36, 16, 21, 24, 16, 21, 10, 18, 16};
    
    
    for (i=0; i<Size; i++);
        {sum += mean[i];
        average=sum/10;
        printf("%d\n",average);}
    
    
    scanf("%d",&y);
    
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    You have an extra semi-colon on this line:
    Code:
    for (i=0; i<Size; i++);
    Furthermore, you probably only want to compute and print the mean after the loop.

    You should indent your code properly. Instead of defining Size as a macro set to 11, you could write:
    Code:
    int mean[] = {13, 36, 16, 21, 24, 16, 21, 10, 18, 16};
    const size_t size = sizeof(mean) / sizeof(mean[0]);
    This way, you don't have to worry about Size being set to a wrong value.

    (Oh, and mean is a strange name for the array, unless it is really an array of mean values for which you want to compute the overall mean.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by openwindow View Post
    Hi
    im new to programming, and have been trying to create a small program that can find the mean of a set of numbers from an array, the problem i face is that when i run the program instead of the correct answer appearing, a random set of numbers appears usually something like -827427429 etc, does anyone know what i have done wrong, if it helps im using visual express c++ 2010

    Code:
    #include <stdio.h>
    #define Size 11
    
    
    int main(void)
    {
    int y;
    int i;
    int average = 0;
    int sum = 0;
    int mean[Size]={13, 36, 16, 21, 24, 16, 21, 10, 18, 16};
    
    
    for (i=0; i<Size; i++);
        {sum += mean[i];
        average=sum/10;
        printf("%d\n",average);}
    
    
    scanf("%d",&y);
    
    
    return 0;
    }
    Move the closing brace for your loop from line 17 to the end of line 15.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 11:27 AM
  2. help finding a bug
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-22-2005, 02:15 AM
  3. Need help finding bug
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2003, 02:21 AM
  4. finding a job!
    By nima_ranjbar in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2002, 12:00 PM
  5. Finding a Key - C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-30-2001, 07:19 AM