I made a program which allows the user to enter the dimension of an array and the array values itself. The code runs fine however whenever the first row of the array seems to repeat itself.
E.g For the A[n][n] Array when n = 2, and I enter values for array such as 1,6,4 and 3, the code outputs an array of [1,6][1,6].
Might be easier to understand if you ran the code yourself:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
void v_in();
// void v_out;
void v_in()
{
int i,j,n;
int A[n][n];
cout << "Enter the dimension of your matrix. Enter a value between 1-20.\n";
cin >> n;
if(n < 1)
{
cout << "\nValue enter is out of range. The value of n is now 1.\n";
n = 1;
}
else if(n > 20)
{
cout << "\nValue enter is out of range. The value of n is now 20.\n";
n = 20;
}
cout << "Enter values the array A[" << n << "][" << n << "].\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin >> A[i][j];
}
}
cout << "\n\nA = \n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout << A[i][j] << "\t";
}
cout << "\n";
}
}
int main()
{
string answer;
cout << "\nWould you like to run the matrix program?\n";
cin >> answer;
if(answer == "yes" || answer == "Yes" || answer == "YES")
{
v_in();
cout << "\nEnd of program.\n";
}
else
{
cout << "\nEND.\n";
}
return 0;
}