用整数除数组项

Division of array items with an integer?

本文关键字:数组 整数      更新时间:2023-10-16

我试图用"1000"来划分数组的项目,我认为我的语法是错误的,请帮助!

data[99]包含1-100的值,而two[99]为空。

float two[99];
for(int x=0; x<100; x++)
{
  two[x]=data[x]/1000;
}

这样定义two:

float two[100]; // 99 + 1

数组从C/c++中的0开始,因此two[99]表示two的第100项。

语法正确,逻辑错误。float two[99];99项- 098 - two[99]为非法

你有一个从零开始的索引数组,所以你需要:

float two[100];
for(int x=0; x<100; x++)
{
   two[x]=(float)data[x]/(float)1000;
}

我添加了(float)转换,以确保您得到预期的值,因为我们不知道数据的类型[…]