2 arrays are becoming equal

  arrays, c++

I need to do 2 arrays (arr1 and arr2) which size must be from value 3-10 only. To do that I set the range of both of my arrays to var size( as seen in the code). However, after I’ve inputted the values for arr1 and arr2 and then print them, the value of arr1 became the same as arr2.

#include<iostream>
using namespace std; 

int main()
{ 
    int size, a = 0, b = 0, c = 0; 
    int arr1[size], arr2[3]; 
    
    while(a = 1)
    { 
        cout << "Enter desired size of the array: "; 
        cin >> size; 

        if(size >= 3 && size <= 10)
        {  
            break; 
        } 
        a= 1;   
    }
    
    cout<<"nEnter value for Array 1: " << endl;
    for(int i = 0; i < size; i++)
    {
        cin >> arr1[i]; 
    }
    
    for(int i = 0; i < size; i++)
    {
        cout << arr1[i]; 
    }
    
    cout<<"nEnter value for Array 2: " << endl;
    for(int i = 0; i < size; i++)
    {
        cin >> arr2[i]; 
    }
    
    for(int i = 0; i < size; i++)
    {
        cout <<"Array 1: " << arr1[i] << " Array 2: " << arr2[i] << endl; 
    }
}

output:

Enter desired size of the array: 3

Enter value for Array 1:
1
2
3
123
Enter value for Array 2:
5
4
3
Array 1: 5 Array 2: 5
Array 1: 4 Array 2: 4
Array 1: 3 Array 2: 3

When I change or set the range of my arr2 (example arr2[3]) the problem disappear. However, I need both arr1 and arr2 to have the same size and the size must come from the user. What could be the solution?

Source: Windows Questions C++

LEAVE A COMMENT