1
#include<iostream>
using namespace std;
void arrayin(int x[], int n);
void arrayout(int x[], int n);
main()
{
    int n, x[n];
    cout << "Please enter the number of elements in the array: " << endl;
    cin >> n;
    cout << "Please enter the elements: " << endl;
    arrayin(x,n);
    cout << "Array is of " << n << " elements."<< endl;
    cout << "Elements are as follow :" << endl;
    arrayout(x,n); 
}
void arrayin(int x[],int n)
{
    for (int i = 0; i < n; i ++)
    {
        cin >> x[i];
    }
}   
void arrayout(int x[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << x[i] << "\t";
    }
}

I'm new to programming. It crashes for more than 8 elements, if n > 8 crashes.. but for n<8 works fine.. Dont know why!

7
  • 3
    int n, x[n]; ---> undefined behavior Commented Dec 14, 2016 at 11:06
  • I don't know, C++ supports VLA? I thought it was more of a C thing, but I may be wrong. Commented Dec 14, 2016 at 11:06
  • 2
    Are you compiling it as C++? (Not that that really matters since you declare x[n] before you know n). Variable length arrays are a C thing - in C++ I'd use a vector. Commented Dec 14, 2016 at 11:08
  • @SouravGhosh It's a GCC extension Commented Dec 14, 2016 at 11:08
  • C and C++ are quite different with respect to arrays. You should decide which one you have before asking here. From the view of it you are trying to use C concepts within C++. Don't do that, you'll never know what you get. Commented Dec 14, 2016 at 11:09

4 Answers 4

5

Here is the problem:

 int n, x[n]; // It is undefined behaviour
 cout << "Please enter the number of elements in the array: " << endl;
 cin >> n;

The correct way is (on your compiler with the variable-size-array extension):

 int n;
 cout << "Please enter the number of elements in the array: " << endl;
 cin >> n;
 int x[n];

The correct way using C++ is to use std::vector instead:

 int n;
 cout << "Please enter the number of elements in the array: " << endl;
 cin >> n;
 std::vector<int> x(n);

and you have to make some other changes to adapt std::vector.

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

Comments

3

The problem is here:

int n, x[n];  // <-- n is not yet initialized
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
cout << "Please enter the elements: " << endl;
arrayin(x,n);

You need this:

int n;
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int x[n];   // << now n has been initialized
cout << "Please enter the elements: " << endl;
arrayin(x,n);

BTW: VLA (or dynamic arrays as you call them) are non standard in C++, but gcc (and possibily clang) has them as extension.

Comments

2

int n, x[n]; is the problem

You are declaring n that will have an indeterminate value. With this value you are declaring an array that will have an indeterminate size.

You are using C++, so use new keyword to create your array, after user input:

cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int *x = new int[n];
// your stuff
delete x;

Comments

0

Declare your array after taking input for n.

#include<iostream>
using namespace std;
void arrayin(int x[], int n);
void arrayout(int x[], int n);
main()
{
    int n;
    cout << "Please enter the number of elements in the array: " << endl;
    cin >> n;
    int x[n];
    cout << "Please enter the elements: " << endl;
    arrayin(x,n);
    cout << "Array is of " << n << " elements."<< endl;
    cout << "Elements are as follow :" << endl;
    arrayout(x,n);
}
void arrayin(int x[],int n)
{
    for (int i = 0; i < n; i ++)
    {
        cin >> x[i];
    }
}
void arrayout(int x[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << x[i] << "\t";
    }
}

This code works. The problem is when you declare array with no value for n the array will be initialized with whatever is on stack from past. Variable length arrays require the value. So declare the array later.

Someone downvoted without any reason.

Edit: Variable length arrays are not part of ISO C++. To write standard compliant C++ code you should use -pedantic flag with g++ or clang.

You have two simple choice. Either use std::array for fixed length arrays or std::vector for dynamic arrays.

4 Comments

Or.. use std::vector<int>
I downvoted WITH a reason.. your question, before editing, consisted of "Declare your array after taking input for n.". this is not a real answer, it is a comment. After your edit I will withdraw my downvote. But IMO this answer is not useful since you did not mentioned about the nonstandard extensions. or even a real C++ solution
That is okay. There is no need to strain your voicebox. A programmer is supposed to use compiler well. The answer should also contain that for g++ one should use -pedantic -Wall for seeing this.
Yeah calm down @HumamHelfawi, you sound pretty stressed.

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.