views:

156

answers:

8

To access the array indice at the xth position we can use some sort of illustration as shown below

#include<iostream>
using namespace std;
int main(){
    float i[20];
    for(int j=0;j<=20;j++)
        i[j]=0;
}

However the following piece of code does not work

#include<iostream>

using namespace std;

float oldrand[55];
int jrand;

void advance_random(){
    int j1;
        float new_random;

    for(j1=0;j1<=23;j1++){
        int temp = j1+30;
        new_random = (oldrand[j1]) - (oldrand[temp]);
        if(new_random <0.0)
            new_random = new_random+1;
        oldrand[j1] = new_random;
    }
    for(j1=24;j1<=54;j1++){
        new_random[j1] = oldrand[j1] - oldrand[j1-23];
        if(new_random[j1]<0.0)
            new_random[j1] = new_random + 1;
        oldrand[j1]=new_random;
    }
}

I recieve the following error

ga.cpp:20: error: invalid types ‘float[int]’ for array subscript
ga.cpp:21: error: invalid types ‘float[int]’ for array subscript
ga.cpp:22: error: invalid types ‘float[int]’ for array subscript

I am not able to find a mistake in my code please help me

+7  A: 

new_random isn't declared as an array of floats, it's declared as a float. The compiler is trying to tell you you can't index into a float.

Blindy
A: 

oldRand is an array new_random is just a float so

new_random[j1]

is invalid.

Tamar
A: 

You declared new_random as a float, not an array of floats, and are trying to use the array method of accessing it (new_random[j1]).

Kevin Sylvestre
A: 

This part of your first snippet is wrong:

int main(){
    float i[20];
    for(int j=0;j<=20;j++)
        i[j]=0;
}

The last iteration of that loop will assign to i[20] which does not exist (because float i[20] defines an array of floats from i[0] to i[19]). The right way is:

int main(){
    float i[20];
    for(int j=0;j<20;j++)
        i[j]=0;
}

Note that j<20 and not j<=20

Eli Bendersky
Technically it does exist, it just isn't what he thinks it is.
Blindy
A: 

Because you are treating the variable new_random as an array when you have declared it as a normal float.

Isak Savo
Wow.. Seems the "this question has a new answer" banner didn't work this time :)
Isak Savo
A: 

new_random is not an array. You are trying to treat it as one in the following lines:

new_random[j1] = oldrand[j1] - oldrand[j1-23];
        if(new_random[j1]<0.0)
            new_random[j1] = new_random + 1;
mjmarsh
+5  A: 

As others have noted, new_random is not declared as an array, hence the compiler error.

Moreover, with this type of loop

float i[20];
for(int j=0;j<=20;j++)
    i[j]=0;

you are going to run out of the array bounds and get undefined behaviour. Proper form is

for(int j=0;j<20;j++)

This is because in C/C++, arrays are indexed from 0, thus an array of 20 elements contains elements indexed from 0 to 19.

Péter Török
A: 

These are the lines causing the problem:

for(j1=24;j1<=54;j1++){ 
    new_random[j1] = oldrand[j1] - oldrand[j1-23]; 
    if(new_random[j1]<0.0) 
        new_random[j1] = new_random + 1; 
    oldrand[j1]=new_random; 
} 

As has been said, the "new_random[j1]" is illegal - it should work correctly as soon as you remove the "[j1]":

for(j1=24;j1<=54;j1++){ 
    new_random = oldrand[j1] - oldrand[j1-23]; 
    if(new_random<0.0) 
        new_random = new_random + 1; 
    oldrand[j1]=new_random; 
}
Ziv