Thread: Need help with last portion of code.

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Need help with last portion of code.

    This is a program I am doing for my class it is supposed to take in 10 numbers from the user calculate the average and determine how many numbers were originally greater than the average and list them for the used. The program works fine except when I want it to spit back out the numbers that are greater than the average. What am I missing?

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    double num[10];
    double sum = 0;
    double averagenum;
    int greaternum = 0;
    for ( int i = 0; i < 10; i++ )
    {
    printf("Enter number ");
    scanf("%d", num);
    sum = sum + num[10];
    }
    averagenum = sum / 10;
    
    for ( int j = 0; j < 10; j++ )
    {
    if ( num[j] >= averagenum )
    greaternum++;
    }
    printf("%d",  greaternum);
    printf(" numbers are greater than the average.\n");
    
    for ( int k = 0; k < 10; k++ )
    {
    if (num[k] > averagenum)
    printf("%d" , num[k]);
    printf("\n");
    }
    system ("pause");
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CodeNLoad View Post
    What am I missing?
    You aren't counting how many are greater, you are just printing them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, let's clean up your code and take a look:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    	double num[10];
    	double sum = 0;
    	double averagenum;
    	int greaternum = 0;
    
    	for ( int i = 0; i < 10; i++ ){
    		printf("Enter number ");
    		scanf("%d", num);
    		getchar();
    		sum = sum + num[10];
    	}
    
    	
    	averagenum = sum / 10;
    	printf("%d", averagenum);
    
    	for ( int j = 0; j < 10; j++ ){
    		if ( num[j] >= averagenum )
    			greaternum++;
    	}
    
    	printf("%d",  greaternum);
    	printf(" numbers are greater than the average.\n");
    
    	for ( int k = 0; k < 10; k++ ){
    		if (num[k] > averagenum)
    			printf("%d" , num[k]);
    		printf("\n");
    	}
    
    	getchar();
    	return(0);
    }
    What exactly are you doing with your sum statement in red? I guarantee it is not what you think.
    Also, take a look at your scanf function in green. Where are those numbers going?
    Last edited by AndrewHunter; 07-02-2011 at 08:30 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Well, let's clean up your code and take a look:

    What exactly are you doing with your sum statement in red? I guarantee it is not what you think.
    Also, take a look at your scanf function in green. Where are those numbers going?
    You beat me to it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clueless for a portion of code.
    By shazwi in forum C Programming
    Replies: 5
    Last Post: 05-28-2010, 06:33 AM
  2. Trouble with private portion of Classes
    By SoBlue in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2007, 05:32 PM
  3. Problems Clearing Portion of Screen
    By Peter5897 in forum Windows Programming
    Replies: 2
    Last Post: 05-29-2006, 03:23 AM
  4. How to get only a certain portion of the string
    By chops11 in forum C Programming
    Replies: 13
    Last Post: 10-14-2005, 12:39 PM
  5. Deleting first portion of a string
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 02-27-2004, 03:56 PM