Momo Pomo
Momo Pomo

Reputation: 265

Division of array items with an integer?

I am trying to divide the items of an array with "1000", and I think my syntax is wrong, kindly help!

data[99] contains the values from 1-100 while two[99] is empty.

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

Upvotes: 3

Views: 442

Answers (3)

Jean
Jean

Reputation: 7673

You have a zero-based indexes array so you need:

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

I added the (float) conversion to make sure you get the expected values, since we don't know the type of data[…].

Upvotes: 1

masoud
masoud

Reputation: 56479

Define two like this:

float two[100]; // 99 + 1

Arrays start from 0 in C/C++, so two[99] refers to 100th item of two.

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258608

The syntax is okay, the logic is wrong. float two[99]; has 99 items - 0 through 98 - two[99] is illegal.

Upvotes: 2

Related Questions