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 close the input <ctrl+d> It was inserted 3 numbers and the avarage is: 38 Sorry for my bad english, forgive my mistakes!! Anyway, this is my code: #include <iostream> using namespace std; int main() { int numero[50]; double somma = 0; double media = 0; int nElementi; for (int x = 0; x < 50; x++){ cout << "Numero: "; cin >> numero[x]; cout << "Hai inserito l'elemento " << x << " dell'array;" << endl; somma += numero[x]; media = somma / numero[x]; nElementi = numero[x]; } cout << "Hai inserito " << nElementi << " elementi" << endl; cout << "La somma è: " << somma << endl; cout << "La media è: " << media << endl; return 0; } but when pressing ctrl+d it only runs the program to the last element of my arrays and then prints absurds print and avarage, of course.