Thread: HW Help - Average & Total of six integers

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    HW Help - Average & Total of six integers

    I am trying to complete this assignment, but I'm stuck, can anybody help me fix my code?

    "Write a program that prompts the user for an integer number from the keyboard, and store it in a variable num. After each number is typed in, add num to a total.
    Do this six times, each time adding num to a total. (Be sure to reuse the same variable named num for each of the six numbers entered.) When all six numbers have been entered from the keyboard, calculate an average.
    Display the total and average calculated. "

    Here is what I have so far:

    Code:
    #include<stdio.h>
    
    int main()
    {
     int num, total1, total2, total3, total4, total5, total6, avg;
    
        printf("Enter first number:");
            scanf("%2d",&num);
    
     int total1 = 0 + num
    
        printf("Enter second number:");
            scanf("%2d",&num);
    
     int total2 = total1 + num
    
        printf("Enter third number:");
            scanf("%2d",&num);
    
     int total3 = total2 + num
    
        printf("Enter fourth number:");
            scanf("%2d",&num);
    
     int total4 = total3 + num
    
        printf("Enter fifth number:");
            scanf("%2d",&num);
    
     int total5 = total4 + num
    
        printf("Enter sixth number:");
            scanf("%2d",&num);
    
     int total6 = total5 + num
    
     avg = total6 / 6.0;
     
     printf("\nTotal: %d", total6);
     printf("\nAverage: %f", avg);
    
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You treat avg as a float variable (which is correct), but you have declare it as int one (which is not ok). Declare it as a double and report back.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    Post

    Code:
        int num=0;
        int avg,input;
     
        printf("Enter first number:");
            scanf("%d",&input);
        num+=input;
      /*    
    *
    *
    *
    **/
        avg=num/6;
        printf("\nTotal: %d",num);
        printf("\nAverage: %d",avg);
    
    return (0);
    }
    /*You can also do this using a loop to save time. Try it.*/
    Last edited by LTA85; 01-23-2013 at 12:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. read n numbers and find their total, min, max, average
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2011, 04:04 PM
  3. Integers Sums & Average Through Function
    By snayyer in forum C Programming
    Replies: 1
    Last Post: 03-09-2011, 06:01 AM
  4. average of total using array
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 05:27 PM
  5. Replies: 4
    Last Post: 03-03-2003, 03:52 PM