AhmedO
AhmedO

Reputation: 165

why the output is different and what is the wrong in this code?

so i have been trying to get the input of array's size and its elements then display the elements to the screen but when i for example put array's size : 7 array's elements : 1 2 3 4 5 6 7 output is:

1
2
3
4
5
6
6

The code :

#include <iostream>

using namespace std;

int main () {    
    int n , Arr[n];    
    cout << "please put the size of the array " ;    
    cin >> n;    
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
}

Upvotes: 0

Views: 91

Answers (3)

Joseph D.
Joseph D.

Reputation: 12174

From dcl.init#12:

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced ([expr.ass]).

unsigned char c;
unsigned char d = c;        // OK, d has an indeterminate value
int e = d;                  // undefined behavior

Thus, in your code:

int n , Arr[n]; 

n has an indeterminate value until it's assigned in cin >> n;

using n with this indeterminate value (not value-/zero-/default-initialized and not assigned), can lead to undefined behavior.

Upvotes: 0

gchen
gchen

Reputation: 1173

As many others have mentioned in the comment section, another way (in case you want to stick with C array) would be to allocate the array dynamically on the heap.

#include <iostream>

using namespace std;

int main () {    
    int n;    
    cout << "please put the size of the array " ;    
    cin >> n; 
    int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
    delete [] Arr; //make sure to clean up the heap memories
}

Upvotes: 1

bolov
bolov

Reputation: 75688

int Arr[n] where n is not a compile time constant is illegal C++ code. Some compilers allow it as an extension (Variable Length Array).

Even with VLA extension, the code is invalid because n is uninitialized when used in your code.

First the real solution:

Use std::vector (tadaaa):

#include <iostream>
#include <vector>

int main () {    
    int n;
    std::vector<int> arr;

    std::cout << "please put the size of the array " ;    
    std::cin >> n;

    arr.reserve(n); // optional

    std::cout << "please enter array's elemets ";
    for (int k=0; k<n ; k++) {
        int elem;
        std::cin >> elem;
        arr.push_back(elem);
    }

    for (auto e : arr) {
        std::cout << e << std::endl;    
    }    
}

If you need to compile against C++98 (wow):

for (std::vector<int>::iterator it = arr.begin(); it != arr.end(); ++it) {
    std::cout << *it << std::endl;    
}

or just:

for (std::size_t i = 0; i < arr.size(); ++i) {
    std::cout << arr[i] << std::endl;    
}

If you insist on using VLA (I recommend against it):

int n;
cout << "please put the size of the array " ;    
cin >> n;    
int Arr[n];    

cout << "please enter array's elemets ";    
for (int k=0; k<n ; k++) {    
    cin >> Arr[k];    
}    
for (int i=0;i<n;i++){    
    cout << Arr[i] << endl;    
}

Upvotes: 5

Related Questions