This is the question I'm stuck on!
Write a C++ program that is calling a function named AllPositive that returns true if all the values of a certain subarray of a 2 dimensional array are all positive, and returns false otherwise. Your program should also call a function to generate the elements of the 2-dimensional array (the method used is your choice), a function to print a 2-dimensional array (call it first to display the original table and call it again after each function call to AllPositive to display the subarray checked)
The screen dialogue should appear as follows (sample output):
The original table is:
1 2 3 6
4 6 11 10
9 12 -14 7
-1 5 -3 15
8 -3 10 1
Some elements in the original table are negative.
Start subarray processing? (Y/N): Y
Enter # of rows: 6
ERROR! Should be at most 5. Reenter: 3
Enter # of columns: 5
ERROR! Should be at most 4. Reenter: 4
The subarray is:
1 2 3 6
4 6 11 10
9 12 -14 7
Some elements in this subarray are negative.
Continue subarray processing? (Y/N): Y
Enter # of rows: 3
Enter # of columns: 2
The subarray is:
1 2
4 6
9 12
All elements in this subarray are positive.
Continue subarray processing? (Y/N): N
Press any key to continue
Now I haven't finished yet, but I'm getting really confused there appears to be all this nesting going on and I can't seem to get anywhere. First of all it won't take my y or n for the sub processing. And second what am I suppose to do if there is only one positive number and it's like in the middle of the array?
This is what I have so far
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int ROW = 4;
const int COL = 3;
const int LOW = -50;
const int UP = 50;
void GenerateArray(int[][COL], int, int, int, int);
int CheckArray(int[][COL], int, int);
void PrintArray(int[][COL], int, int);
void Sub(int[][COL], int, int);
int main()
{
srand(time(NULL));
int table[ROW][COL] = {0};
int value = 0;
char yon;
GenerateArray(table, ROW, COL, LOW, UP);
PrintArray(table, ROW, COL);
value = CheckArray(table, ROW, COL);
cout << "\n\n";
if (value = 1)
{
cout << "Your array has negatives in it!" << endl;
cout << "Start sub processing (y/n)" << endl;
cin >> yon;
while (yon != 'y' || yon != 'n')
{
cout << "Invalid entry try again: " <<endl;
cin >>yon;
}
if (yon == 'n')
cout << "Sub array processing is complete" << endl;
else
{
int n_row = 0;
int n_col = 0;
cout << "Please enter # of rows: " << endl;
cin >> n_row;
while (n_row >= ROW)
{
cout << "New amount is too big! Please re-enter: " <<endl;
cin >> n_row;
}
cout << "\n\n Please enter # of cols: " << endl;
cin >> n_col;
while (n_col >= COL)
{
cout << "New amount is too big! Please re-enter: " <<endl;
cin >> n_col;
}
Sub(table, n_row, n_col);
}
}
else
{
cout << "Your array has no negatives!" << endl;
}
} //END OF MAIN
void GenerateArray(int t[ ][COL], int row, int col, int lower_limit, int upper_limit){
for (int r = 0; r < row; r++)
for (int c = 0; c < col; c++)
t[r][c] = rand()%(upper_limit - lower_limit + 1) + lower_limit;
}
void PrintArray(int t[][COL], int row, int col){
for (int r = 0; r < row; r++){
for (int c = 0; c < col; c++)
cout << setw(5) << t[r][c];
cout << endl;
}
}
int CheckArray(int t[ ][COL], int row, int col){
for (int r = 0; r < row; r++)
for (int c = 0; c < col; c++)
{
if (t[r][c] < 0)
return 1;
else
return 2;
}
}
void Sub(int t[][COL], int row, int col)
{
for (int r = 0; r < row; r++){
for (int c = 0; c < col; c++)
cout << setw(5) << t[r][c];
cout << endl;
}
}