Array problem averaging numbers

This is a discussion on Array problem averaging numbers within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm new on here and iv searched through the forum for something to fix my problem but alas ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Array problem averaging numbers

    Hi guys, I'm new on here and iv searched through the forum for something to fix my problem but alas i havnt found anything!I am trying to write a program using arrays and loops to determine the average of a set of numbers and then comparing each individual value of the array to the average to see which is greater. I have attached comments to say what happens when it compiles.

    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
    
    
    int i, array[10]={0,1,2,3,4,5,6,7,8,9,};
    
    
    i= 0;
    
    
    float average,sum;
    
    
    average=sum=0.0;
    
    
    while(i<10)
    {
    
    
    	sum=sum+array[i];
    	
    	i++;
    }
    
    
    average=sum/i;
    
    
    printf("The average is %f\n", average);
    
    
    i=0;
    
    
    while (i<10)
    {
    	if (array[i]> average){
    	
    	printf("%d\n is greater than average %f", array[i],average);
    	
    	i++; 
    	
    	}
    	
    
    
    	
    	if	(array[i]< average){
    	
    	printf("%d\n is less than average %f", array[i],average);
    	
    	i++;
    	} /*when compiled i get (a blank space) is less than the average 4.5 and so on */
    }
    		
    return 0;
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by Thedon
    when compiled i get (a blank space) is less than the average 4.5 and so on
    You are probably just misinterpreting your output because you insert a newline in a strange place. Try changing this:
    Code:
    printf("%d\n is less than average %f", array[i],average);
    to:
    Code:
    printf("%d is less than average %f\n", array[i], average);
    similiarly, change the printf for "greater than".

    By the way, you should indent your code properly. It makes it easier to read and hence understand your program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thank you for your help although it hasnt seem to change the outcome. I have indented my code to what i hope is better if it makes it easier to read!

    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
    
    
    int i, array[10]={0,1,2,3,4,5,6,7,8,9,};
    
    
    i= 0;
    
    
    float average,sum;
    
    
    	average=sum=0.0;
    
    
    	while(i<10)
    	{
    
    
    		sum=sum+array[i];
    	
    			i++;
    	}
    
    
    average=sum/i;
    
    
    	printf("The average is %f\n", average);
    
    
    i=0;
    
    
    		while (i<10)
    	{
    		if (array[i]>average){
    	
    			printf("%d is greater than average %f", array[i],average);
    	
    				i++; 
    	}
    	
    	
    		if	(array[i]<average){
    	
    			printf("%d is less than average %f", array[i],average);
    	
    				i++;
    	} /*when compiled i get (a blank space) is less than the average 4.5 and so on */
    
    
    	}
    		
    return 0;
    
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Better, but here is how I might format your code:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i, array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,};
        float average, sum;
        average = sum = 0.0;
    
        i = 0;
        while (i < 10)
        {
            sum = sum + array[i];
            i++;
        }
        average = sum / i;
        printf("The average is %f\n", average);
    
        i = 0;
        while (i < 10)
        {
            if (array[i] > average) {
                printf("%d is greater than average %f\n", array[i], average);
                i++;
            }
    
            if (array[i] < average) {
                printf("%d is less than average %f\n", array[i], average);
                i++;
            }
        }
    
        return 0;
    }
    I have taken the liberty of moving the first i = 0; to just before the while loop that uses it. Also, I have added in the newlines as I demonstrates to make the output more readable. Other than that, your logic is entirely the same. Compile and run the program. You might notice something strange. Trace through the flow of control in your program to find out why it happens.

    In particular, observe when you increment i in the second while loop. Are you doing it at the right place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I have done all that you said and the program works fine!im pretty sure the second i is in the right place because it needs to be reset for the second loop. Thanks so much for your help!also as regards my indenting i have been losing marks all semester from it but you are the first to properly help me with it so thank you!

    Also can you tell me how to edit this thread title so its solved?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by Thedon
    im pretty sure the second i is in the right place because it needs to be reset for the second loop.
    I get an extra line of output because of that. The thing is, you only need to increment once in the loop, hence you should write:
    Code:
        while (i < 10)
        {
            if (array[i] > average) {
                printf("%d is greater than average %f\n", array[i], average);
            }
     
            if (array[i] < average) {
                printf("%d is less than average %f\n", array[i], average);
            }
            i++;
        }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Yeah i did that tidied up the program a bit too thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-06-2010, 07:03 PM
  2. Averaging a "column" in a 2d array
    By gadgetman525 in forum C Programming
    Replies: 3
    Last Post: 11-18-2010, 02:18 AM
  3. quick prob w/summing and averaging the elements of an array
    By pinkfloyd4ever in forum C Programming
    Replies: 6
    Last Post: 12-01-2009, 10:46 PM
  4. averaging numbers?
    By rock4christ in forum C Programming
    Replies: 12
    Last Post: 09-09-2006, 07:38 PM
  5. averaging random numbers
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-27-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21