Thread: Array average help

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    6

    Array average help

    I have been trying to get this working for the past 3 hours, trolling on forums and I am in fact lost. Any help would be appreciated...I think I got lost with all my stupid variables. I am trying to get the average for my arrays, I got the ascending order to work finally and do not really want to go back because that took me forever...All I want to do is to be able to take the numbers from within the array and output them with an average...any help is MUCH appreciated, thanks again for your time.

    Code:
    Code:
    #include <conio.h>
    #include <stdio.h>
    int main()
    {
        int data[100],i,n,step,temp,sum;
        printf("Enter the number of elements to be sorted: ");
        scanf("%d",&n); //scans input and puts into integer n
        for(i=0;i<n;++i)
        {
            printf("%d. Enter element: ",i+1);
            scanf("%d",&data[i]);
        }
    
    
        for(step=0;step<n-1;++step)
        for(i=0;i<n-step-1;++i)
        {
            if(data[i]>data[i+1])  //sorts in ascending order
            {
                temp=data[i];
                data[i]=data[i+1];
                data[i+1]=temp;
            }
        }
        printf("In ascending order: ");
        for(i=0;i<n;++i)
             printf("%d  ",data[i]);
    
    
        sum=0;   //this is the part that I amm lost on...I understand its wrong my brain is just fried and need a second set of eyes..
        for(i=0;i<n;++i);
            sum=sum+i/n;
            printf("Average is %d\n", sum);
    
    
    
    
        getch(); //
        
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Hint: if I declare an int as

    Code:
    int  x = 3/4;
    
    printf("%d",x);
    What value would be printed out?
    Last edited by gemera; 03-22-2014 at 03:03 PM.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    3/4?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by internalbalance View Post
    3/4?
    You have no idea what the value of 3/4 is in C, do you?

    Hint: The two best guesses are 0 or 1; what do you think it is?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    No, i dont know what that is...I only have access to a book and it is super confusing which is why I reached out for help. I know very little about programming. I replied 3/4 because he set x=3/4 and the next line he said print(and %d scans said integer x)...I truly would like some help as it is obvious I dont know much. I apologize.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by internalbalance View Post
    3/4?
    You do realise you could just write a small test program to find out if you didn't know?

    You must have the ' float ' type covered in your book.

    Mathematically is 3/4 an integer value?

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Mathmatically no it is not...and it seems like common sense now looking back. However, everything that I thought would make sense hasn't worked very well so far.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    for(i=0;i<n;++i);
            sum=sum+i/n;
            printf("Average is %d\n", sum);
    The ';' at the end of the "for" loop stops the next line from being ran.

    And the indentation of your project is bad - The snip I took from above is a bit too "gotofail" for me. It should look more like this:
    Code:
    for(i=0;i<n;++i)
    {
      sum=sum+i/n; // Another Hint: This does not compute the sum nor average of your ARRAY
    }
    
    printf("Average is %d\n", sum);
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by internalbalance View Post
    Mathmatically no it is not...and it seems like common sense now looking back. However, everything that I thought would make sense hasn't worked very well so far.
    Mathematically, assuming the result had to be stored as an integer, what would you expect 3/4 to produce? That is common sense (the only decision needed is whether to round up or down). In C, the literal value 3 has type int, the literal value 4 has type int, so 3/4 is an int.

    It is not exactly common sense to expect to produce an integral value that is equal to 0.75.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Click_here View Post
    Code:
    for(i=0;i<n;++i);
            sum=sum+i/n;
            printf("Average is %d\n", sum);
    The ';' at the end of the "for" loop stops the next line from being ran.

    And the indentation of your project is bad - The snip I took from above is a bit too "gotofail" for me. It should look more like this:
    Code:
    for(i=0;i<n;++i)
    {
      sum=sum+i/n; // Another Hint: This does not compute the sum nor average of your ARRAY
    }
    
    printf("Average is %d\n", sum);
    Agreed. My company's coding standards prohibit any use of loops or if statements without braces - one-line statements always get them, for exactly the reason that it makes the "goto fail" kind of error less likely.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Agreed. My company's coding standards prohibit any use of loops or if statements without braces - one-line statements always get them, for exactly the reason that it makes the "goto fail" kind of error less likely.
    [A bit off topic, but...]
    One bug that changed the way that I program that was along the lines of this sort of thing -> In embedded programming it is common to see this sort of statement waiting for a register to set/clear
    Code:
    while (!REGISTER.bitx);
    Which is fine - Until you put it into a do/while loop that is very long

    Code:
    do
    {
       /* Lots of sending/receiveing for a few pages */
      /* Lots of indentating... */
      {
        {
        }
      }
    
      { 
        { 
        }
      }
      while(something);
    
      /* Lots of indentating... */
      {
        {
        }
      }
    
      { 
        { 
        }
      }
    }
    while (somethingelse);
    Needless to say that it was heartbreaking to see that the code was... well... confused.

    Now I always type out my wait while statements like this:
    Code:
    while (!REGISTER.bitx)
    {
      continue;
    }
    It is very easy to see that it is not the end of a do/while statement.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe the real problem was that the loop body was too long
    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

  13. #13
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Quote Originally Posted by Click_here View Post
    Code:
    for(i=0;i<n;++i);
            sum=sum+i/n;
            printf("Average is %d\n", sum);
    The ';' at the end of the "for" loop stops the next line from being ran.

    And the indentation of your project is bad - The snip I took from above is a bit too "gotofail" for me. It should look more like this:
    Code:
    for(i=0;i<n;++i)
    {
      sum=sum+i/n; // Another Hint: This does not compute the sum nor average of your ARRAY
    }
    
    printf("Average is %d\n", sum);
    How can I get the values to add up? Example if I enter, 1 2 and 4 my 'n' would be 3, so obviously I would divide by that to get my average correct? However, how can I add the 1 2 and 4??

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by internalbalance View Post
    How can I get the values to add up? Example if I enter, 1 2 and 4 my 'n' would be 3, so obviously I would divide by that to get my average correct? However, how can I add the 1 2 and 4??
    Let's call 'n' the number of items and lets call 'sum' the current running total. Then to add 1, 2 and 4 into sum, you do something like this in a loop

    Code:
    sum = 0;
    n = 0;
    
    // User enters 1 and you store it in 'x'
    sum += x;
    n++;
    
    // User enters 2 and you store it in 'x'
    sum += x;
    n++;
    
    // User enters 4 and you store it in 'x'
    sum += x;
    n++;

  15. #15
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Figured it out, guess it just took a few hours of reading up on float averages. Appreciate the hint in the right direction Click_here. All it took was for me to shut up and actually do some more reading on my own. Thanks for not just solving my problem 100% because now I actually no more about it. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average of an Array
    By Peteski in forum C Programming
    Replies: 19
    Last Post: 01-05-2012, 04:38 PM
  2. Moving Average (from and into an array)
    By browser in forum C Programming
    Replies: 4
    Last Post: 01-24-2010, 03:36 PM
  3. average of an array
    By mrsirpoopsalot in forum C Programming
    Replies: 20
    Last Post: 09-16-2006, 04:32 PM
  4. average of array elements?
    By swapnil_sandy in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 12:16 PM
  5. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM