【发布时间】:2012-11-04 15:35:31
【问题描述】:
我有一个问题:当我的程序执行时,我的 int 返回的值太大。 该程序计算算术平均值。
例如:当我输入 1,2,3,4,5 时,它会显示 2293393。
/*
* 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 translate(pl->en) 中翻译 couts。
【问题讨论】:
-
请打开编译器的警告。
-
编译器没有任何警告。
-
哦。您也需要打开优化才能获得它。 (
-O1 -Wall就足够了。)(并在使用时添加-std=c++0x -pendatic,这样您就知道您正在使用 GCC 扩展。)
标签: c++ netbeans int mingw average