#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; //global const for rows
const int COL = 4; //global const for colums
void GenerateArray(int[][COL], int, int, int, int); //function prototype
void Print(int[][COL], int, int); //function prototype
bool AllPositive(int[][COL], int&, int&); //function prototype
void SubArray(int[][COL], int&, int&); //function prototype
int main(){
//declare and initialize array
int rows;
int cols;
int table[ROW][COL]; // sets table array
char yorn='y';
srand(time(NULL));
GenerateArray(table, ROW, COL, UP, LOW); //function call
cout << "The original table is: ";
Print(table, ROW, COL); //function call
rows=ROW; // set constant to variable
cols=COL; // set constant to variable
if(AllPositive(table, rows, cols)==false){
cout << "\n\nSome Elements in the orginal table are negative";
cout << "\n\nStart subarray processing? (Y/N): ";
cin >> yorn;
while((yorn == 'Y') || (yorn == 'y')){ // If they answer yes, it starts
while(AllPositive(table, rows, cols)==false){
SubArray(table, rows, cols);
Print(table, rows, cols);
if(AllPositive(table, rows, cols)==false)
cout << "\n\n Some Elements in the orginal table are negative\n";
cout << "\n\nContinue subarray proccessing? (Y/N) ";
cin >> yorn;
if((yorn !='y') || (yorn !='Y'))
return 0;
}
}
}
if(AllPositive(table, rows, cols)==true)
cout << "\n\n All elements in this subarray are positive.";
cout << endl;
return 0;
} // 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){
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 &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;
}
}