Thread: Program to determine whether numbers in an array are higher or lower

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

    Program to determine whether numbers in an array are higher or lower

    Program is supposed to put out messages saying this number is x lower than the average. This number is x larger than the average.

    Instead it puts out

    The value 1.00000 is 44.000000 smaller than the average (should be 4.50000000. Even for the larger numbers for eg value 8 it puts out that it is 36.000000 smaller despite the fact it is 4.5 bigger. What have I done wrong?

    Code:
    #include <stdio.h>
    
    void main()
    
    
    {
    
    
    float list[10];
    int count = 0;
    float difference = 0.0;
    float average;
    float value = 0;
    
    
    list[0]=1.0; list[1]=2.0; list[2]=3.0; list[3]=4.0; list[4]=5.0;
    list[5]=6.0; list[6]=7.0; list[7]=8.0; list[8]=9.0; list[9]=0.0;
    
    
    
    
    	while (count < 10)
    	{
    	
    	value = value + list[count];
    	
    	count++;
    
    
    	}
    	
    	average = value/10;
    
    
    	printf("The average value in the array is %f.\n", average);
    
    
    count=0;
    
    
    	while (count < 10)
    	{
    		
    		difference = value-list[count];
    		
    		if(value > list[count])
    					
    		printf("The value %f is %f smaller than the average %f.\n", list[count], difference, average);	
    
    
    		else
    		
    		printf("The value %f is %f greater than the average %f.\n", list[count], difference, average);	
    		
    		count++;
    
    
    	}
    
    
    
    
    return;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because you are subtracting from the total ("value"), not the average.

    The return type of main() must be int, not void.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Thanks, I see that now. Problem is, it doesn't recognise the else anymore (it says I'm using else without a previous if but there is an if there). Syntax?

    Code:
    #include <stdio.h>
    
    int main()
    
    
    {
    
    
    float list[10];
    int count = 0;
    float difference = 0.0;
    float average;
    float value = 0;
    
    
    list[0]=1.0; list[1]=2.0; list[2]=3.0; list[3]=4.0; list[4]=5.0;
    list[5]=6.0; list[6]=7.0; list[7]=8.0; list[8]=9.0; list[9]=0.0;
    
    
    
    
    	while (count < 10)
    	{
    	
    	value = value + list[count];
    	
    	count++;
    
    
    	}
    	
    	average = value/10;
    
    
    	printf("The average value in the array is %f.\n", average);
    
    
    count=0;
    
    
    	while (count < 10)
    	{
    		
    		if(average > list[count])
    
    
    		difference = average-list[count];		
    			
    		printf("The value %f is %f smaller than the average %f.\n", list[count], difference, average);	
    
    
    		else
    		
    		difference = list[count]-average;
    
    
    		printf("The value %f is %f greater than the average %f.\n", list[count], difference, average);	
    		
    		count++;
    
    
    	}

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    I moved the maths because it was producing x is -4.5000 greater than y etc.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your if else is illegal in C. Learn to indent your code so all the subordinate lines of code are indented by 2 to 5 spaces, and you'll see it immediately.

    In C, you can't do this:
    Code:
    if(someTest) 
       do something;
    print something;  <<== this is illegal. else statement must go here
    else 
      do something else
    
    
    But you can do this:
    if(someTest)  {
       do something;
       print something;  <<== this is now legal, inside the if {   }
    }
    else 
      do something else
    Which is perhaps, what you intended.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by Adak View Post
    Your if else is illegal in C. Learn to indent your code so all the subordinate lines of code are indented by 2 to 5 spaces, and you'll see it immediately.

    In C, you can't do this:
    Code:
    if(someTest) 
       do something;
    print something;  <<== this is illegal. else statement must go here
    else 
      do something else
    
    
    But you can do this:
    if(someTest)  {
       do something;
       print something;  <<== this is now legal, inside the if {   }
    }
    else 
      do something else
    Which is perhaps, what you intended.
    Perfect! Thank-you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-14-2011, 09:27 PM
  2. Replies: 11
    Last Post: 04-11-2011, 05:52 PM
  3. determine if numbers are prime
    By royaro in forum C Programming
    Replies: 6
    Last Post: 02-13-2011, 01:04 PM
  4. Simple program problem from upper to lower case
    By steals10304 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2009, 02:31 AM
  5. Finding numbers higher than the first element in array
    By sonict in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 10:00 AM