0

I am getting this

'invalid types int*[std::ios_based&(std::ios_base&)]' for array subscript  in line                   
output[left]=input[i];  

Can't able to figure out the solution to it. here output is the array in which i want my answer

#include <bits/stdc++.h>
using namespace std;
void recover(int input[], int output[], int n)
{
    int i = 0;
    int mid = n / 2;
    if (n % 2 != 0)
    {
        output[mid] = input[i];
        int left = mid - 1;
        int right = mid + 1;
        i++;
    }
    else
    {
        int left = mid - 1;
        int right = mid;
    }
    while (i < n)
    {
        output[left] = input[i];
        left--;
        i++;
        output[right] = input[i];
        right++;
        i++;
    }
}

int main()
{
    int input[100], output[100];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> input[i];
    }
    recover(input, output, n) for (int i = 0; i < n; i++)
    {
        cout << output[i] << " ";
    }
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
yolon bsn
  • 89
  • 1
  • 1
  • 4
  • 3
    `int left=mid-1;` goes out of scope before it is used here `output[left]=input[i];`, same for `right`. Fix these and compile again. Typo? – Richard Critten Oct 02 '19 at 13:49

1 Answers1

0

First of all you have a scope problem, as Richard Critten pointed out in comments. right and left do not have the definition in the while loop that you gave them in the if and else statements.

Compounding this, you use #include <bits/stdc++.h> and using namespace std;. These are not causing your issue, but they are making it harder to understand.

Using #include <bits/stdc++.h> is a bad idea. It includes the entirety of the C++ standard library, which is lazy, unnecessary, and increases compile times. It's also not portable.

Using using namespace std; is a bad idea. It makes everything in namespace std available without qualification. This means any common names like left and right may match something you didn't intend. This can lead to bugs that are horrendously difficult to find and fix.

Using both together is really awful, because now you have the entirety of the std namespace, with lots and lots of commonly used names, colliding with the names you want to use in your code.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174