Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this part of the lab, we want to check how many negative values are in a two-

ID: 3575911 • Letter: I

Question

In this part of the lab, we want to check how many negative values are in a two- dimensional (square) array, if any. Write a program that asks the user to input the dimension (n) of the square (n times 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 times 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 (input is in italic and bold): Enter the size of a 2D array: 4 Enter the values in the array for row 1, separated by a space, and press enter: 15 63 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: 94-31 Enter the values in the array for row 4, separated by a space, and press enter: 75-39 There are 4 negative values! Enter the size of a 2D array: 12 ERROR: your array is too large! Enter 1 to 10.

Explanation / Answer

#include <iostream>
using namespace std;

int main()
{
   int n;
   cout<<"Enter the size of a 2D array:";
   cin>>n;
   if(n<1 || n>10)
   {
       cout<<"ERROR:your array is too large! Enter 1 to 10."<<endl;
       return 0;
   }
   int arr[n][n];
   int count=0;
   for(int i=0;i<n;i++)
   {
       cout<<"Enter the values in the array for row "<<i+1<<", separated by a space, and press enter:";
       for(int j=0;j<n;j++)
       {
           cin>>arr[i][j];
           if(arr[i][j]<0)
           {
               count++;
           }
       }
   }
   if(count==0)
   {
       cout<<"All values are non-negative!"<<endl;
   }
   else
   {
       cout<<"There are "<<count<<" negative values!"<<endl;
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote