0

the code returns error: invalid types 'int[int]' for array subscript| and everything else seems to run fine. What can I do about it ? (line 10,22,23)

#include <iostream>
using namespace std;

int n, x[50], y[50], z[50];

void citire(int &n, int v)
{
    int i;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cout << "v[" << i << "]=";
        cin >> v[i];
    }
}

void afisare(int n, int v[])
{
    int i;
    for (i = 1; i <= n; i++)
        cout << v[i] << " " << endl;
}

void s(unsigned n, int x[], int y[], int z[])
{
    int i;
    for (i = 1; i <= n; i++)
        z[i] = abs(x[i] - y[i]);
}

int main()
{
    cout << "n=";
    cin >> n;
    cout << "x[]:" << endl;
    citire(n, x);
    cout << "y[]:" << endl;
    citire(n, y);
    cout << "Elementele primului vector" << endl;
    afisare(n, x);
    cout << "Elementele celui de-al doilea vector:" << endl;
    afisare(n, y);

    s(n, x, y, z);
    cout << "z[]:" << endl;
    return 0;
}
2
  • Is there some newly instigated monetary penalty for whitespace and indentation to make code readable? I mean, I thought the days of line-editing decwriters was long gone (and good riddance), but I'm seeing this more and more. Without objection (or with), I'm formatting that.. thing. As for the code. I strongly advise you stop fighting 0-based indexing and start embracing it. Someone entering 50 for n at program start is going to invoke undefined behavior. Commented Mar 3, 2021 at 7:30
  • indeed, you should use 0 based indices. Not only you are wasting memory for the element at index 0 that you never use, but once you replace the c-arrays with std::vector or any other proper container, all your loops will cause undefined behavior Commented Mar 3, 2021 at 7:42

1 Answer 1

1

You are using v as an array, but it isn't an array, it is int. You probably want to use int* v.

void citire(int &n, int* v) {
// ...
cin>>v[i];
}
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.