Write a program that asks the user to input the dimension (n) of the square (n x n) array, and then asks the user to input the values 1 row at a time. For example:
“Enter the size of a 2D array: ”
“Enter the values in the array for row 1, separated by a space, and press enter: ”
- Limit the size of the array to maximum 10 x 10 and check for errors.
Once the array is initialized, check if there is any negative element in the array and display the result:
If there is no negative value: “All values are non-negative!”
If there are # negative values: “There are # negative values!” … where # is the number of negative values found.
Example runs:
Enter the size of a 2D array: 4
Enter the values in the array for row 1, separated by a space, and press enter: 1 5 6 3
Enter the values in the array for row 2, separated by a space, and press enter: -5 6 -12 5
Enter the values in the array for row 3, separated by a space, and press enter: 9 4 -3 1
Enter the values in the array for row 4, separated by a space, and press enter: 7 5 -3 9
There are 4 negative values!Enter the size of a 2D array: 12
ERROR: your array is too large! Enter 1 to 10
Here is what I have so far but I can't figure how to enter the info one row at a time. Do I enter the values of into two separate arrays then try to combine them? But the values need to stay in their receptive rows. Thanks
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
cout << "please enter the size of the 2D array" << endl;
cin >> num;
while (num < 1 || num > 10)
{
cout << "You have entered an invalid number that is less than 10"<< endl;
cout << "Enter a new # " << endl;
cin >> num;
}
cout <<"Enter a sequence of numbers separated by spaces" << endl;
int c = 0;
int arr[num][num];
for(int i = 0; i < num; i++)
{
for(int j = 0; j < num; j++)
{
cin >> arr[i][j];
if(arr[i][j] < 0 )
{
c = c+1;
}
}
}
for(int i=0; i < num; i++)
{
for(int j=0; j < num; j++)
{
cout << arr[i][j] << endl;
}
}
cout << c << endl;
return 0;
}