Jump to content

media com arrays duvida!


Devexz
 Share

Recommended Posts

tenho que fazer media de 20 notas , mas nao sei utilizar muito bem um array...

fiz assim mas nao funciona ,podem me dizer o que esta mal.

#include<iostream>
using namespace std;
int main () {
int n[20];
int numero_notas,soma=0;
double media;
cout<<"quantas notas quer \n\n";
cin>>numero_notas;
cout<<"introduza as notas\n";
for(int i=0;i<numero_notas;i++)
	cin>>n[20];

	soma=soma+n[20];
media=soma/numero_notas;
cout<<"a media é "<<media;
system("PAUSE");
return 0;
}

Contador de calorias: caloriaspordia.com

Link to comment
Share on other sites

Estás a ler tudo para o o ultimo elemento do array.

Deve ser assim:

   int n[20]; 
        int numero_notas,soma=0;
        double media;
        cout<<"quantas notas quer \n\n";
        cin>>numero_notas; //Atencao que tu limitastes o array a 20
        cout<<"introduza as notas\n";
	for(int i=0;i<numero_notas;i++){
		cin>>n[i];
		soma=soma+n[i];
	}
        media=soma/numero_notas;
        cout<<"a media é "<<media;
        system("PAUSE");
        return 0;
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.