#include <iostream>
#include <iomanip>
#include <cstdlib> // for rand()
#include <ctime>
using namespace std;
const int LOW =-5; //global const for the lower limit
const int UP = 15; //global const for the upper limit
const int ROW = 5;
const int COL = 4;
//int rows;
//int cols;
void GenerateArray(int[][COL], int, int, int, int); //function prototype
void Print(int[][COL], int, int);
bool AllPositive(int[][COL], int, int, int&, int&);
void SubArray(int[][COL], int, int, int&, int&);
void main(){
//declare and initialize array
int rows;
int cols;
int table[ROW][COL];
char yorn;
srand(time(NULL));
GenerateArray(table, ROW, COL, UP, LOW); //function call
cout << "The original table is: ";
Print(table, ROW, COL);
rows=ROW; // set constant to variable
cols=COL; // set constant to variable
if(AllPositive(table, ROW, COL, rows, cols)==false){
cout << "\n\nStart subarray processing? (Y/N): ";
cin >> yorn;
if((yorn == 'Y') || (yorn == 'y')){ // If they answer yes, it starts
while(AllPositive(table, ROW, COL, rows, cols)==false){
cout << "\n\nSome Elements in the orginal table are negative";
SubArray(table, ROW, COL, rows, cols);
}
}
}
if(AllPositive(table, ROW, COL, rows, cols)==true)
cout << "\n\n All elements in this subarray are positive.";
cout << endl;
} // end program
void GenerateArray (int a[][COL], int row, int col, int upper_limit, int lower_limit){
for (int r = 0; r < row; r++)
for (int c = 0; c < col; c++)
a[r][c] = rand()%(upper_limit - lower_limit + 1) + lower_limit;
}
void Print(int a[][COL], int row, int col){
for ( int r = 0; r < row; r++ ) {
cout << endl;
for ( int c = 0; c < col; c++ )
cout << setw(5) << a[r][c];
}
}
bool AllPositive(int a[][COL], int ROW, int COL, int &r, int&c){
for ( int r = 0; r < ROW; r++ )
for ( int c = 0; c < COL; c++ )
if(a[r][c] < 0)
return false;
return true;
}
void SubArray(int a[][COL], int ROW, int COL, int &rows, int&cols){
// int rows;
// int cols;
cout << "\nEnter # of rows: ";
cin >> rows;
while(rows > ROW){
cout << "ERROR! Should be at most 5. Reenter: ";
cin >> rows;
}
cout << "Enter # of columns: ";
cin >> cols;
while(cols > COL){
cout << "ERROR! Should be at most 4. Reenter: ";
cin >> cols;
}
Print(a, rows, cols);
}