C++ Problem

This is a discussion on C++ Problem within the C++ Programming forums, part of the General Programming Boards category; hello can someone help me please with this exercise. i can't solve it. Write an averaging program. The program must ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Post C++ Problem

    hello
    can someone help me please with this exercise.
    i can't solve it.

    Write an averaging program. The program must prompt the user to enter a positive integer that specifies the number of values n the user
    wishes to average. The program must then ask the user to input these n
    values. The program should then compute the average of the values inputted and output it to the user.

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    What are you having trouble with? Here's some pseudo-code to get you going (which is exactly what you stated).

    Code:
    prompt user for n number of integers
    read in integers
    find average of integers
    cout the average

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    this is what i had so far, but i am totally confused.

    Code:
    #include <iostream> 
    #include <string>
    using namespace std;
    
    int main(){ 
    	
    	int nV;
    	cout << "Enter the number of values to average: "; cin >> nV;
    	int v[nV];
    
    	for (int cnt = 0; cnt != nV; cnt++){
    
    		cout << "[" << cnt << "] = "; 
    		cin >> v[cnt];
    		}
    
    	int avrg = 0;
    
    	while (nV != -1)
    		avrg += v[nV-1];
    
    	avrg /= nV;
    	cout << " Average = " << avrg;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what are you confused about? The better you ask questions, the better the answers will be.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You're going through an infinite loop here since nV isn't actually being decreased. You're just saying "value of nV minus one" but you're not actually changing nV.

    Code:
    while (nV != -1)
    		avrg += v[nV-1];

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    There's nothing saying you can't do what he did. I do that myself even, it's part of programming. In a console program it's not a big. In windows API I can see it becoming a problem in different areas, though.
    I guess my statement was vague, what I mean is that before you can use an array you have to declare the size first:

    Code:
    int v[ 10 ];

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    There's nothing saying you can't do what he did. I do that myself even, it's part of programming. In a console program it's not a big. In windows API I can see it becoming a problem in different areas, though.
    I guess my statement was vague, what I mean is that before you can use an array you have to declare the size first:

    Code:
    int v[ 10 ];

  8. #8
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    you don't of course have to retain all values in memory...
    All you have to retain is the sum of the values already entered and the number of values entered.
    The average can be simply computed as that sum divided by that number at any time.

    You'd probably want to use an unsigned long for the sum instead of an unsigned int of course.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Code:
    	int nV;
    	cout << "Enter the number of values to average: "; cin >> nV;
    	int v[nV];
    In C/C++ you should declare the size of the array first, you cannot just declare the size of the array in runtime

    Code:
     while (nV != -1)
    		avrg += v[nV-1];
    
    	avrg /= nV;
    As scwizzo stated, you're not actually decrementing the value of nV, so you will be having an infinite loop right there.

    let's say you're decrementing nV, that's still a logical error. Since you are decrementing the value of nV and using it to divide the total of avrg to have the average of the values, you will be having a negative value.

    it would be better if you could declare a variable that will traverse in the array for you to sum up all the values that you entered in the array.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by $l4xklynx View Post
    In C/C++ you should declare the size of the array first, you cannot just declare the size of the array in runtime
    There's nothing saying you can't do what he did. I do that myself even, it's part of programming. In a console program it's not a big. In windows API I can see it becoming a problem in different areas, though.

    As for that loop, I would probably use a for loop to go through the array, but you can do it however you feel fit.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    There's nothing saying you can't do what he did.
    Except for the current C++ standard, meaning that it is an extension, but really, who cares about standards?!

    Soma

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Fortunately you can avoid the whole "not defined" thing by doing this problem the easy way, without any arrays at all. (Why read and then process -- why not process while you read?)

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by scwizzo View Post
    There's nothing saying you can't do what he did. I do that myself even, it's part of programming. In a console program it's not a big. In windows API I can see it becoming a problem in different areas, though.

    As for that loop, I would probably use a for loop to go through the array, but you can do it however you feel fit.
    But C++ is no slouch. Even though it may not support C variable-sized arrays, it does support dynamic arrays:
    Code:
    std::vector v(vX);
    Or even
    Code:
    std::vector v;
    int x;
    for (int i = 0; i < vX; i++)
    {
        cin >> x;
        v.push_back(x);
    }
    Keep to the standard!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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