Averaging code

This is a discussion on Averaging code within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using std::cout; using std::endl; using std::cin; using std::string; int main() { const int AVERAGE = ...

  1. #1
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37

    Averaging code

    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::cin;
    using std::string;
    
    int main()
    {  
       
       
       const int AVERAGE = ;
       int number;
        
         
        cout << "Pick a number!: ";
        cin >> number;
        cout << "Another number: ";
        cin >> number;
        cout << "Last number!!: ";
        cin >> number;  
       
        cout << "\nThe avereage is: " << (AVERAGE) << "\n";
        
         
          
           
        system("\nPAUSE");
        return 0;
    
    
    
    }
    I dont know how to get the three numbers totaled, and how to divide them with the constant. help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    One approach is to initialise the sum and count to 0. Whenever you read a number from the user, add it to the sum, then increment the count. When you are done, divide the sum by the count, and you have the average (arithmetic mean).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by dead_captain View Post
    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::cin;
    using std::string;
    
    int main()
    {  
       
       
       const int AVERAGE = ;
       int number;
        
         
        cout << "Pick a number!: ";
        cin >> number;
        cout << "Another number: ";
        cin >> number;
        cout << "Last number!!: ";
        cin >> number;  
       
        cout << "\nThe avereage is: " << (AVERAGE) << "\n";
        
         
          
           
        system("\nPAUSE");
        return 0;
    
    
    
    }
    I dont know how to get the three numbers totaled, and how to divide them with the constant. help.
    First of all, you need to assign the const int a value, in this case you could make it 3, and divide the total of the inputted numbers to get the average value. But you don't really need it i guess.

    Second of all, you only have 1 variable for storing numbers,and you use it 3 times, each time the value in the variable gets overwritten. What you want is 3 doubles, which you then ask the user to input a number into,for each of them. Then to calculate the average you add up all the numbers (Possibly in a fourth variable, or you could use one of the existing ones to hold the total value), and divide by the number of inputted values, in this case 3...
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37
    Code:
       
       
       const int AVERAGE = 3;
       int number, number1, number2, add;
        
         
        cout << "Pick a number!: ";
        cin >> number;
        cout << "Another number: ";
        cin >> number1;
        cout << "Last number!!: ";
        cin >> number2;  
       
        cout << "\nThe avereage is: " << (AVERAGE) << "\n";
    I still don't know how to get the total of all three numbers. I added add to my ints, but do not know how to use it. Now the average is 3 everytime. lol

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dead_captain View Post
    I still don't know how to get the total of all three numbers.
    Are you serious, or is this some kind of troll? There is a key on your keyboard, looks like this: + You may want to look into it. You've already figured out =. Seriously: read a thread on here that you didn't start. You could even search the board for the word "average", since we had somebody else last week ask pretty much the same question. Or find a thread where someone does a calculation, we've got lots of those, and look at what a calculation looks like in C.

  6. #6
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37
    I dont like our attitude

  7. #7
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37
    Sorry if the question is to "simple" but I cant figure it out. So im asking. Help, how do I add the three numbers, and divide them.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    Quote Originally Posted by dead_captain View Post
    I dont like our attitude
    You may. But instead of posting meaningless comments - better try to follow the advice and show the effort
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I still don't know how to get the total of all three numbers.
    If you want to use the approach described by Neo1 then you should use an array instead.

    With what I described, and using a fixed total count, I would expect something along these lines:
    Code:
    const int count = 3;
    double sum = 0.0;
    int count = 0;
    
    double number;
    cout << "etc";
    cin >> number;
    sum += number;
    
    // ...
    
    cout << (sum / count);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dead_captain View Post
    I dont like our attitude
    I don't particularly like our attitude either. But still: look around! I admit that only six of the twenty threads on the front page had calculations in them; but when you see things like
    Code:
    p=a*a*a*a-6*a*a*b*b+b*b*b*b;
    (taken from another thread), it shouldn't be much of a stretch to figure out how to add three numbers. Tell you what: if you give me a good reason why you couldn't find how to add numbers in a C++ programming book, or why you didn't read LESSON 1: INTRO TO C++ on this very site, then I'll apologize.

  11. #11
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37
    Code:
       const int AVERAGE = 3;
       int number, number1, number2, add;
        
         
        cout << "Pick a number!: ";
        cin >> number;
        cout << "Another number: ";
        cin >> number1;
        number + number1 =;
        cout << "Last number!!: ";
        cin >> number2;  
        number + number1 + number2 =;
        cout << "\nThe avereage is: " << (AVERAGE) << "\n";
    I am reading the lesson one. I am not sure where the "double" comes in. sorry if i am dumb.
    I am getting compile error with the code now.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I am not sure where the "double" comes in.
    You might find it necessary if you want a more precise average, but perhaps it is best left for you to discover after you have fixed your current problem.

    I am getting compile error with the code now.
    When you wrote this piece of code:
    Code:
    number + number1 =;
    and then this line:
    Code:
    number + number1 + number2 =;
    What did you think they would do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Let's just give some hints:
    What operator do you use normally when you want to add? we all know that's the + operator.
    How do you assign a variable a value in C++? try to find that out (you should know already). A special operator is used.
    Now, if you know how to do what's above then this should be easy. To calculate the average, just get the total of all the numbers (which in this case would be the sum of the three numbers), then divide that by how many numbers you have (in this case: 3).

    Still need some help?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Here's what I think.
    dead_captain needs to get a book or find some online tutorials and actually learn the language before trolling us with poor questions that's easily learned from a good book that covers the language.
    You won't get anywhere if you don't know the language. You'll keep coming back asking us what's this and what's that, even though you could solve it yourself, because it's the same code with a slightly different method of doing it.
    So get a good book. Read it. Understand the language. Then go back and look over your code and see if it makes sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Learning C++ is too difficult to do by guessing. Find a reference source of some kind.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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