Thread: Sub Array Processing

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Sub Array Processing

    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;
            }
        }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean it always enteres and never leaves this loop:
    Code:
    		while (yon != 'y' || yon != 'n')
    		{
    			cout << "Invalid entry try again: " <<endl;
    			cin >>yon;
    		}
    If we write it out in english, maybe it becomes clearer:
    while ( yon is not equal to 'y' OR yon is not equal to 'n' )
    // do stuff

    For which values is the red part true:
    yon = 'y'
    yon = 'n'
    yon = 'a'

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Ok I had to change the sign to and. The only prob now is that the sub array won't keep looping. And I don't think I'm looking for the negative the right way. When I changed the value to make them all positive the program still says they're is a negative when there isn't!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if (value = 1)
    To compare values, you need to use ==.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (value = 1)
    See the problem?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Dang stupid mistake!!!! I hate when I make those. Sorry everyone i'm kinda a noob. I really appreciate the speedy help it has really helped me learn a lot from using this site. So I think all I need to do is loop the sub processing until the array is broken down and has no negatives but what am I suppose to do if I have only like 2 positive values and they're no where close to each other. Is there any easy way to implement a loop now with what I have or should I start over?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by GCNDoug View Post
    Dang stupid mistake!!!! I hate when I make those. Sorry everyone i'm kinda a noob. I really appreciate the speedy help it has really helped me learn a lot from using this site. So I think all I need to do is loop the sub processing until the array is broken down and has no negatives but what am I suppose to do if I have only like 2 positive values and they're no where close to each other. Is there any easy way to implement a loop now with what I have or should I start over?
    That particular problem is easy to solve by using "1 == y" instead of "y == 1", because then the compiler will tell you that you are doing it wrong when you miss out an equal sign.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Anyone know how I can make the sub processing loop until I select n? Thanks!! That's all I have left I think.
    Last edited by GCNDoug; 11-28-2007 at 04:20 PM.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Hmmm still can't figure out the loop issue any ideas?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Replies: 2
    Last Post: 07-22-2004, 02:25 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM