1

I need to add either +0.25 or -0.25 to all elements within an array. Here is what I have so far. Any help would be appreciated.

int main() {

double i;


// Arrays
double x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double x2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};



for(i=1; i<=10; i++) {
    const double pM[2] = {-1, 1};
    int randoid = rand() % 2;
    for(i=1; i<=10; i++){
        x2[i] = x[i] + pM[randoid]*0.25; //Error Line
    }
}

cout << x;
cout << x2;
}

I get this error at the marked line: "invalid types 'double[10][double] for array subscript"

7
  • Everything looks ok except for your output. Try for (int i = 0; i < 10; i++) cout << x2[i]; instead of cout << x2; Commented Nov 9, 2015 at 4:41
  • Also, your i is of type double. Change it to int i. Arrays have integer indices. Commented Nov 9, 2015 at 4:41
  • The type of i must be an integer. You've declared it as a double. Commented Nov 9, 2015 at 4:46
  • Thanks both of you. The problem is fixed, but the output is still broken. I get this as the output: "2.253.253.754.756.256.757.758.759.75-0.250x24fdc02". All the numbers not spaced with some weird hex at the end. Any help? Commented Nov 9, 2015 at 4:54
  • Your code doesn't compile (even with the fix). Did you have some #include and some using lines that you forgot to paste? Commented Nov 9, 2015 at 5:08

3 Answers 3

3

The problem is that i is a double. Then you write x2[i].

It's not a very good error message; however with the [] operator, one of the operands must be a pointer and the other must be an integer. There is no implicit conversion of floating-point to integer when using this operator.

To fix this change double i; to int i;


Another issue is that your code accesses out of bounds of the arrays. double x2[10] means that there are 10 elements whose indices are 0 through 9. But your loop tries to write to x2[10]. This causes undefined behaviour, which could explain your strange output.


There is also a potential logic error. Maybe you meant to use a different variable for the inner loop than the outer loop. As it stands, the inner loop will take i to 11 (or 10 if you fix the code) and then the outer loop will be complete and not execute any more iterations.

Based on your description though, perhaps you only meant to have one loop in the first place. If so, remove the outer loop and just leave the contents there.

Also you do not need two separate arrays, you could just perform the addition in-place.


Regarding the output, cout << x and cout << x2 will output the number of the memory address at which the array is located. To output the contents of the array instead you will need to write another loop, or use a standard library algorithm that iterates over containers.

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

Comments

1

I see 3 issues -

  1. Change the type of i to int.
  2. x and x2 are arrays of size 10. You need to loop from i = 0 to i = 9. But you are looping from i = 1 to i = 10. x[10] is out of bounds since arrays are 0 indexed.
  3. cout << x - This is a wrong way to print an array. You need to loop through the array and print - e.g. -

    for(i = 0; i < 10; i++)
        cout << x[i] << " ";
    

Comments

0

Try this, it works, I converted to C

int main(  )
{

  int i = 0;
// Arrays
  double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  double x2[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  for ( i = 1; i < 10; i++ )
  {
    const double pM[2] = { -1, 1 };
    int randoid = rand(  ) % 2;
    for ( i = 1; i <= 10; i++ )
    {
      x2[i] = x[i] + pM[randoid] * 0.25;    //Error Line

      printf( "\nx[%d]==%2.2f", i, x[i] );
      printf( "\nx2[%d]==%2.2f", i, x2[i] );

    }
  }

}

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.