0

getting an invalid array type in the for loop not allowing me to fill the array sivalues. it wont poulate the array with x

int main () {
    double a,b,increment,para;
    int N;
    cout<< "enter values for (a,b,N)";
    cin>>a;
    cin>>b;
    cin>>N;
    increment= (b-a)/(N-1);
    double sivalues[N];
    for (double x=a;x<=b;x+=increment){
        sivalues[x]=si(x);
        cout<<"si("<<x<<") = "<< sivalues[x];
    }

    system("PAUSE"); return 0;

}
3
  • Unless I'm blind, increment is never initialized, which might be a problem. Commented Apr 2, 2017 at 4:23
  • And what's the exact error? Commented Apr 2, 2017 at 4:24
  • i updated the code Commented Apr 2, 2017 at 4:31

1 Answer 1

2

When you write sivalues[x], you are using a double as an array index. This is not valid, as it would make no sense to get the 2.5th element of an array, for instance. You would therefore need to cast your double into a int.

You could use the default cast (for example : sivalues[int(x)]=si(x);. Do note that this cast does not round, so if x is 5.99, it will still be converted to 5 (see C++: How to round a double to an int?)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.