我有一个问题:当我的程序执行时,我的int返回值太大。 该程序计算算术平均值。int返回垃圾值
例如:当我输入1,2,3,4,5时,它说。
/*
* File: main.cpp
* Author: Natch
*
* Created on 4 listopad 2012, 15:32
*/
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int n,x;
x = 0;
/*
* a - array
* x - repeat
* n - array fields
* suma - array fields sum
*/
cout << "Srednia liczb" << endl;
cout << "Program oblicza srednia z x liczb." << endl;
cout << "Podaj ilosc liczb do obliczenia sredniej:" << endl;
cin >> n;
int a[n];
while(x<n) {
x++;
cout << "Podaj " << x << " liczbe:" << endl;
cin >> a[x];
}
long int suma;
for (int i = 0; i < n; i++) {
suma += a[i];
}
int srednia = suma/n;
cout << "Srednia wynosi:" << endl << srednia << endl;
system("pause");
return 0;
}
对不起,我来自波兰。
您可以在Google翻译(pl-> en)中翻译couts。
请打开您的编译器的警告。 – Mat
编译器没有任何警告。 – sofalse
哦。您还需要启用优化才能获得它。 ('-O1 -Wall'就足够了。)(并且在你使用的时候加上'-std = C++ 0x -pendatic',这样你就知道你使用的是GCC扩展。) – Mat