Thread: program prints random numbers not the sum

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    program prints random numbers not the sum

    I don't understand why my program outputs a random negative number.

    Code:
    #include<stdio.h>
    
    int main(void) {
    
    
        int array[10];
        int i, sum=0;
    
    
       printf("Enter an integer for each element:\n");
    
    
       for (i=0;i<10;i++){
          scanf("%f", &array[i]);
          sum+=array[i];
    }
        
        printf("\n");
        printf("sum = %d\n", sum);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Probably because you used the incorrect format specifier. The "%f" format specifier is for a float, not an int. Perhaps you need to find and read the documentation for that function?

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    wow, i feel stupid. :P
    thanks.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by jimblumberg View Post
    Probably because you used the incorrect format specifier. The "%f" format specifier is for a float, not an int. Perhaps you need to find and read the documentation for that function?

    Jim
    brings me to my next question..
    I'm trying to find out how many integers were entered that are greater than the average

    Code:
    #include<stdio.h>
    
    int main() {
    
    
        int array[10];
        int i, num;
        int sum=0;
        double avg;
       printf("Enter an integer for each element:\n");
    
    
       for (i=0;i<10;i++){
          scanf("%d", &array[i]);
          sum+=array[i];
       }
        printf("\n");
        printf("%d\n", sum);
    
    
        avg=(double)sum/10;
        printf("average= %.2lf\n", avg);
    
    
        for (i=0;i<10;i++){
        array[i]>=avg;
        num++;
        }
        printf("%d", num);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup how to write an if statement in C.

    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

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Also (apart from the missing if statement), num is not initialised.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 42
    Last Post: 01-21-2015, 06:22 PM
  2. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  3. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  4. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  5. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM