Alberto - 21 days ago 10
C++ Question

# Exercise: for cycle and array

I have to write a program that allows to calculate the arithmetic average of an arbitrary numbers of values (chosen by the user)

It will outputs:

Number: 34

Number: 36

Number: 44

Number: //and I choose to stop input pressing

//Outputs:

It was inserted 3 numbers and the avarage is: 38

Of course i've forgot to post what i've done:

``````for (int x = 0; x < 50; x++){
cout << "Number: ";
cin >> number[x];
cout << "You have inserted the " << x << " element of the array;" << endl;

sum += number[x];
avarage = sum / number[x];
nEelementi = number[x];}
``````

so I run the program, input some numbers, press something like ctrl+d or trying to add something to the code.. but it only goes from the first to the last element of the array with no values, becouse not entered, of course.. and then print absurd avarage and sum.

I know I don't need an array to do this but it's required from the exercise.. also the exercise only request to use for or while loop and arrays.
What I need is a way to stop the input and calculate the sum and avarage of only what I wrote.

edit1.
I've tried to dived by n writing for(x = 0; x < n, x++) because it made sense to me, but i think it "thinks" n, wrote like this, infinite, because the results is 0 (because the limit of a number divided by infinite is 0).. so i've started becoming mad.
Now i've started thinking that it would be easier to use while loop! and wrote

``````#include <iostream>
using namespace std;

int main() {

int num[50];
double sum = 0;
double average = 0;
int cont;
int end = 0;

while (cont < 50) {
cout << "num: ";
cin >> num[cont];
sum += num[cont];
cont++;
cout << "Want to continue 0 = sì, 1 = no";
cin >> end;

if (end == 1) {break;}
}

average = sum / cont;

cout << "You have insert " << cont << " elements" << endl;
cout << "LThe sum is: " << sum << endl;
cout << "The avarage is: " << average << endl;

return 0;
}
``````

BUT still doesn't work. My professor says you should be able to stop input number by pressing ctrl+d so I'm not doing good.
Sorry for late answer but i have also to translate the code.. hope all translation is good:)

edit2.

``````#include <iostream>

int main() {
int sum = 0;
int num;

while ( std::cin ) {

std::cout << "Number: ";
std::cin >> num;

}

if ( std::cin >> num ) {

sum += num;
num++;

}

else {

std::cin.clear();
std::cout << "Input interrupted" << std::endl;

}

std::cout << "Sum is " << sum << std::endl;
std::cout << "You have entered " << num << " numbers" << std::endl;

return 0;
}
``````

I love this new code, very simple and understandable to me, but I was not able to add sum operation, it only outputs 0! (leaving out average)
And also I was not able to determinate, and display, how many numbers I've entered. The last row of the code is just an example of what I want to do..

edit3.

``````#include <iostream>
using namespace std;

int main(){

double numero;
int index = 0;
double somma = 0.;

cout << "Inserire un numero: ";

while( cin )

{
if ( cin >> numero )

{
somma = somma + numero;
index++;
cout << "Inserire un numero: ";
}

else

{
cout << "Input interrotto" << endl;
}
}

cout << "Sono stati inseriti " << index << " numeri e la lora media è:
<< somma / index << endl;

return 0;
}
``````

Thanks so much!

There are a few problems here. One is that if the stream errors due to being closed or bad input, you don't recover and you just charge through your loop.

So first, make the loop terminate early if necessary. I'm also going to convert it to a `while` loop in preparation for the next part.

``````int x = 0;
while( std::cin && x < 50 )
{
std::cin >> number[x++];
}
``````

Now it terminates early if the stream errors. But what if the user typed in "hello"? You could ignore it and continue like this:

``````    if( std::cin >> number[x] )
{
x++;
}
else
{
std::cin.clear();
}
``````

Notice that I didn't compute the sum or anything inside the loop. There's no need, since you are already putting them in an array. You can just do it after the loop. Here, I'm using `std::accumulate`

``````double sum = std::accumulate( number, number + x, 0.0 );
double average = 0.0;
if( x > 0 ) average = sum / x;
``````

Now, you have also said you want an arbitrary number of values. Your original code allowed up to 50. Instead of storing them, you can instead just compute on the fly and discard the values.

``````double sum = 0.0;
int count = 0;

while( std::cin )
{
double value;
if( std::cin >> value )
{
sum += value;
count++;
}
else
{
std::cin.clear();
}
}

double average = 0.0;
if( count > 0 ) average = sum / count;
``````

If you still want to store the values along the way, you can use a vector.

``````std::vector<double> numbers;

//...

numbers.push_back( value );
``````

And if you want the user to choose the number of values:

``````std::cout << "Enter number of values: " << std::flush;
std::size_t max_count = 0;
std::cin >> max_count;

std::vector<double> numbers;
numbers.reserve( max_count );

while( std::cin && numbers.size() < max_count )
{
// ...
}
``````
Source (Stackoverflow)