# C++ Problem

• 12-03-2008
RainbowStar
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.

• 12-03-2008
scwizzo
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```
• 12-03-2008
RainbowStar
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; }```
• 12-03-2008
matsp
So what are you confused about? The better you ask questions, the better the answers will be.

--
Mats
• 12-03-2008
scwizzo
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];```
• 12-03-2008
\$l4xklynx
Quote:

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 ];`
• 12-03-2008
\$l4xklynx
Quote:

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 ];`
• 12-03-2008
jwenting
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.
• 12-03-2008
\$l4xklynx
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.
• 12-04-2008
scwizzo
Quote:

Originally Posted by \$l4xklynx
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.
• 12-04-2008
phantomotap
Quote:

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-04-2008
tabstop
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?)
• 12-04-2008
Elysia
Quote:

Originally Posted by scwizzo
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!