Thread: Input and output problem with 2D array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Unhappy Input and output problem with 2D array

    What's wrong with this? I thought the code doesn't have any problem but it just doesn't output what I want.
    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
    	int a = 0, b = 0,size;
    	int array[10][10];
    	
    	cin>>size;
    	for(; a < size; a++) 
    		{
    	    for(; b < size; b++) 
    				{
    				cin>>array[a][b];//input entries
    				if ((b+1)%size==0)
    				cout<<endl;//end the line when a row is complete
    				}
    		}
    
    	for(; a < size; a++) 
    		{
    	    for(; b < size; b++) 
    				{
    				cout<<array[a][b];//output entries in a row
    				if ((b+1)%size==0)
    				cout<<endl;//end the line when a row is complete
    				}
    		}
    }
    If the size input is 2,then this is what I expected on the screen when input:
    Code:
    2
    1 2
    3 4
    And expected output:
    Code:
    1 2
    3 4
    This is what I actually got:
    Code:
    2
    1 2 3 4
    And there is no output.
    Plz help.
    Last edited by Roy01; 10-22-2006 at 07:00 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    	for(; a < size; a++) 
    		{
    	    for(; b < size; b++)
    You don't give an initial value to a and b in these loops. By the time you get to the similar loops again, a and b both will be equal to size, and the loop will never start. (You have incremented both before, how can you expect them to be 0 again, without setting them to 0?)

    Also, if user entered size bigger than 10, they would be in serious trouble.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming you enter an integer in the range [0,10) for the size, the input should not be a problem. The user can just enter the input - extra whitespace (i.e. the new lines) does not matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    I am not sure if I totally understood what you said.Sorry for my poor English.I modified it but still it is not exactly what I want.
    New version:
    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
    	int a,b,size;
    	int array[10][10];
    	
    	cin>>size;
    	for(a=0; a < size; a++) 
    		{
    	    for(b=0; b < size; b++) 
    				{
    				cin>>array[a][b];//input entries
    				if ((b+1)%size==0)
    				cout<<endl;//end the line when a row is complete
    				}
    		}
    	for(a=0; a < size; a++) 
    		{
    	    for(b=0; b < size; b++) 
    				{
    				cout<<array[a][b];//output entries in a row
    				if ((b+1)%size==0)
    				{
    					cout<<endl;//end the line when a row is complete
    				}
    				}
    		}
    }
    actual input
    Code:
    2
    1 2 3 4
    output
    Code:
    12
    34
    Last edited by Roy01; 10-22-2006 at 07:27 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, that looks okay. Still, void main() should be int main(), you might as well declare and initialise the array loop indices in the loops, and your formatting looks off.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int array[10][10];
    
        int size;
        cin >> size;
    
        // assume size is in the range [0,10)
        for (int a = 0; a < size; a++)
        {
            for (int b = 0; b < size; b++)
            {
                cin >> array[a][b]; //input entries
            }
        }
    
        for (int a = 0; a < size; a++)
        {
            for (int b = 0; b < size; b++)
            {
                cout << array[a][b]; //output entries in a row
                if ((b+1) % size == 0)
                {
                    cout << endl; //end the line when a row is complete
                }
            }
        }
    }
    Of course, you need to add spaces to get the output that you want.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems you are trying to format input? That may not be so easy as to be worth the trouble.

    As for outputting, output a space after each number you print.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thx, but my problem is the input.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As far as I know, you cannot force input to be formatted in such a way using only standard C++. Of course, as a user you can format in the desired format.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    So I just wasted my time to try to format input......I got it.Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM