Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner C++ Array

Tags:

c++

arrays

I am new to C++. Below is code to input and display an array -- a simple thing to understand how things work.

int main() {
  int N, i;
  int A[N];
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (i = 0; i < N; i++) {
    cout << A[i];
  }
  return 0;
}

The input is:

4
1 2 3 4

and output is:

12

I have corrected my code to:

int N, i;
cin >> N;
int A[N];

This correctly displays the array.

What exactly happened? Should N have a value before initializing A[N]? But then why didn't my initial code work? Is this something to do with Hierarchy? If so, how can I get my priorities right?

If N is initialized as a large value, N has an input of specific number next, if so how come even if N=9, the output remains same: 12?

like image 259
Scarlett Avatar asked Nov 01 '21 04:11

Scarlett


3 Answers

Should N have a value before initializing A[N]

Yes. It should. It should even have a value before declaring A[N]. How else would the runtime know how much space to allocate for A?

like image 111
Bill Lynch Avatar answered Oct 06 '22 19:10

Bill Lynch


To answer your question.

int N, i;

Here, you declare N, but because you don't assign a value to N, N is uninitialized and can hold any value (probably some old program's value.) And you create an array with N, which will lead to some unexpected behavior. (for example, a stack overflow could happen when N is too big)

And:

The size of arrays in C++ has to be known at compile-time. That means when you compiler compiles the program, the array's size has to be known.

In your case, you know n (the size) at runtime, which means only when you run the program, you know the value of n, which is the size.

Your code run because (probably) gcc compiler does have an extension for this to happen, but because this is not standard C++, I would recommend not using it.

C++ has a solution for runtime array, and that is std::vector in the <vector> header.

You just need to change:

cin >> N;
int A[N];

to:

std::cin >> n;
std::vector<int> A(N);

note: using namespace std; is bad, so don't use it.

like image 30
justANewb stands with Ukraine Avatar answered Oct 06 '22 19:10

justANewb stands with Ukraine


In C++, the size of an array must be a compile time constant. So the following code is incorrect:

int N = 10;
int arr[N]; //incorrect 

The correct way to write this would be:

const int N = 10;
int arr[N]; //correct

For the same reason the following code is incorrect in your given code snippet:

int N, i; //both N and i are **unitialized**(meaning they have **garbage value**)
cin >> N; //this is fine. This takes input from user and put it in N
int A[N];  //incorrect because N must be a constant expression

Also note that your original question used the garbage value of N to create an array. This is why it is advised that always initialize built in type in block/local scope.

like image 1
Anoop Rana Avatar answered Oct 06 '22 18:10

Anoop Rana