Programing question

This is a discussion on Programing question within the C Programming forums, part of the General Programming Boards category; I am taking my first programming course. I am frustrated with my current assignment which is to creat a program ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    Programing question

    I am taking my first programming course. I am frustrated with my current assignment which is to creat a program that adds three numbers and finds their average. My problem is how to define "sum" and "average." This is, in part, my program:

    int first_number
    int second_number
    int third_number

    printf("Enter first_number;second_number, and third_number: ");
    scanf("%d%d%d", &first_number, second_number, and third_number);

    When I get to this part how do I "define" sum and average?

    Thanks for your help. P.S. I am using the Borland Command Prompt programming software.

    Mart00

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you're simply entering integers, the sum can also be an integer. (Assuming you don't enter numbers so large that combine they won't fit in an integer.) If you want the average to use decimal places for some precision, then you'll want a floating point number.

    If you plan on posting further here, you will need to go to the top of the forum and read the Announcements, which will instruct you on the proper way to post code as well as cover a few other forum guidelines.

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

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    you can define the sum to be an int type of variable, however, the average would have to be a float so as to include possibilty of decimal places, but you will have to typecast on the RHS during calculating average , as float.

    something like

    Code:
     int num1,num2,num3;
     float avg;
     avg=((float)num1+num2+num3)/3;
    i suggest u pick up a good book on C and go through it.. this kind of example is very common in those books

    the tutorial section of the site is worth a read too..try it out
    - PING
    Last edited by PING; 01-29-2005 at 11:31 AM. Reason: spelling error
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    Just thought you might like to know your scanf should be
    Code:
    scanf("%d%d%d", &first_number, &second_number, &third_number);
    hobo

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I tried to make things as readable as possible:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
       // _1, _2, _3 = input numbers from the user
       // We use these numbers to find the average of them
    
       int _1, _2, _3;
    
       // t = Total of all of the 3 input numbers added together
       // a = Average of the input numbers
    
       int t;
       int a;
    
       // Ask the user for the numbers seperated by a space
       
       printf( "Enter all three numbers seperated by a space: " );
       scanf( "%d %d %d", &_1, &_2, &_3 );
       
       // Calculate the total
    
       t = _1 + _2 + _3;
    
       // Get the average: TOTAL / TOTAL_NUMBERS
    
       a = t / 3;
    
       // Output average
    
       printf( "%d\n", a );
    
       // Exit success
    
       return 0;
    }
    Some example runs:
    Code:
    Shiva:/home/kleid/Programming/Laboratory# ./a.out
    Enter all three numbers seperated by a space: 3 5 2
    3
    
    Enter all three numbers seperated by a space: 1 5 10
    6
    
    Enter all three numbers seperated by a space: 10 30 80
    37

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > int _1, _2, _3;
    All symbols beginning with an underscore are reserved.

    Why not use an array?
    Then it's easy to extend to more than 3.

    Not that you even need to store all the numbers just to compute an average.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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