简体   繁体   English

使用 int 进行浮点除法 C++

[英]using int for float division c++

i'm new with c++, and i was messing around with the stuff i have learned thus far.我是 C++ 的新手,我一直在玩弄迄今为止我学到的东西。 I was messing with arrays and i came up with the following code我弄乱了数组,我想出了以下代码

#include <iostream>
using namespace std;

int main()
{

// initializes an array with user defined size
int array_size;
cout << "Enter the amount of number you wish to enter: ";
cin >> array_size;
int arr[array_size];

int i = 0;
int element = 0;
int sum = 0;
float average = 0;

// populates the array with user inputted elements
do
{
    cout << "Enter number " << i + 1 << ": ";
    cin >> element;
    arr[i] = element;
    sum = sum + element;
    ++i;
}
while(i < array_size);

int MAX = arr[0];
for(int i = 1; i < array_size; ++i)
    if(arr[i] > MAX)
        MAX = arr[i];

int MIN = arr[0];
for(int i = 1; i < array_size; ++i)
    if(arr[i] < MIN)
        MIN = arr[i];

for(int i = 0; i < array_size - 1; ++i)
    average = (average + arr[i])/array_size.0;

cout << "The biggest number that was entered was: " << MAX << endl;
cout << "The smallest number that was entered was: " << MIN << endl;
cout << "The sum of the numbers entered is: " << sum << endl;
cout << "The average of the numbers entered is: " << average << endl;

return 0;
}

i get an error on line 40: error: expected ';' before numeric constant我在第 40 行收到一个错误: error: expected ';' before numeric constant error: expected ';' before numeric constant i believe its because im trying to append a .0 for float division to a variable name, any way i can use array_size to perform float division? error: expected ';' before numeric constant我相信它是因为我试图将.0的浮点除法附加到变量名称,我可以使用array_size执行浮点除法的任何方式?

You can not append ".0" to a variable name.您不能将“.0”附加到变量名称。 To cast it to float, use casting syntax:要将其转换为浮动,请使用转换语法:

average = float(average + arr[i]) / float(array_size);

Be sure to cast early, so that you would not round the result as int:一定要尽早转换,这样你就不会将结果舍入为 int:

average = float(average + arr[i] / array_size); // might not work as expected

More on type casting: http://www.cplusplus.com/doc/tutorial/typecasting/更多关于类型转换: http : //www.cplusplus.com/doc/tutorial/typecasting/

As an aside, if you want to add a number to an average, you might want to use a different formula: https://math.stackexchange.com/a/957376顺便说一句,如果您想为平均值添加一个数字,您可能需要使用不同的公式: https : //math.stackexchange.com/a/957376

Just do the operation -- whenever you do arithmatic with float variables, the operands will automatcially be converted to double to do the operation.只需执行操作——每当您对float变量进行算术运算时,操作数将自动转换为double float来执行操作。 So you just need:所以你只需要:

for(int i = 0; i < array_size - 1; ++i)
    average = (average + arr[i])/array_size;

since average has the type float , it and arr[i] will be both be converted to double to do a (double precision) floating-point addition.由于average的类型为float ,因此它和arr[i]都将被转换为 double 以进行(双精度)浮点加法。 Then, since that result is double , array_size will also be converted to double and a floating point divide will be done.然后,由于该结果是doublearray_size也将被转换为double并进行浮点除法。 Finally, that double result will be converted back to float and stored in average最后, double float结果将被转换回float并存储在average

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM