Thread: FOR loop to populate a 2D array

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    FOR loop to populate a 2D array

    Hi,

    I am trying to populate a two dimensional array using a FOR loop.

    This is what I have made so far:-

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	int array2d[2][2];
    	int i = 0;
    	int j = 0;
    
    	for(i=0, j=0;i<4, j<4;i++, j++)
    	{
    		cout << "Outer loop " << endl;
    		cout << "Enter the number for array location " <<  i << j << endl;
    		for(j=0;j<4;j++)
    		{
    			cout << "Inner loop " << endl;
    			cout << "Enter a number for array location " << i << j << endl;
    			cin >> array2d[i][j];
    		}
    	}
    
    	cout << array2d[0][0] << endl;
    	cout << array2d[0][1] << endl;
    	cout << array2d[1][0] << endl;
    	cout << array2d[1][1] << endl;
    
    	system("pause");
    }
    I have two problems.

    The first is that when the contents of the array are 'cout'd', 2 'containers' seem to still hold the old 'nonsense' values.

    The second is that I am wanting to ask the user:-

    cout << "Enter the number for array location " << i << j;
    so that it says 0,0 then 0,1 then 1,0 then 1,1 but am a little confused on how to go about that. I have found that he other FOR loop is only iterated once when the program runs.

    The first problem is my main concern though, as I thought I had constructed the FOR loops correctly.

    Many thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst this is a correct for-loop construction, it's not doing what you want:
    Code:
    	for(i=0, j=0;i<4, j<4;i++, j++)
    For one thing, the "i < 4, j < 4" will do "compare i to 4, then throw away the value, then compare j to 4 and use this to finish the loop". That's highly likely NOT what you wanted to do.

    I think you should just remove all the "j" work in this line, and leave j to be looped around on in the inner loop.

    --
    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
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks matsp!

    I have removed the j++ from the outer for loop, and changed the outer FOR loop to only iterate once.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	int array2d[2][2];
    	int i = 0;
    	int j = 0;
    
    	for(i=0;i<1;i++)
    	{
    		cout << "Outer loop " << endl;
    		cout << "Enter the number for array location " <<  i << j << endl;//I know the variables are not incrementing correctly
    		for(j=0;j<4;j++)
    		{
    			cout << "Inner loop " << endl;
    			cout << "Enter a number for array location " << i << j << endl;//And this line also
    			cin >> array2d[i][j];
    		}
    	}
    
    	cout << array2d[0][0] << endl;
    	cout << array2d[0][1] << endl;
    	cout << array2d[1][0] << endl;
    	cout << array2d[1][1] << endl;
    
    	system("pause");
    }
    I've attached a screen shot of the debugger and am a little concerned at the values shown in the window which are still 'jumble'.

    Is this normal and would you say I have done what I wanted in the correct manner?

    Thank you.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The debug output looks OK to me.

    You are also misusing your indices for the array, it's a 2 x 2 array, and you are filling it as if it was a 1 x 4 array - which isn't a particularly good practice.

    --
    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.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    	int array2d[2][2];
    	int i = 0;
    	int j = 0;
    
    	for(i=0;i<1;i++)
    	{
    		// ...
    
    		for(j=0;j<4;j++)
    Red should match red and blue should match blue.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Daved you have no idea how helpful that is.

    I'm guessing that this 'red and blue' rule could be used with any two dimensional array in the manner I've used the two FOR loops.

    I now feel that the code is working as it should, I was a little confused by '1 x 4' mattsp kindly helped me with.

    Many thanks for making the post.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that you don't necessarily need to do like this. It's better to define the loop variables inside the loops, as they will go out of scope when the loop ends. If you need the loop numbers outside the loops, then it might be better to define them outside:

    Code:
    	for(int i = 0; i < 2; i++)
    	{
    		for(int j = 0; j < 2; j++)
    		{
    			cin >> array2d[i][j];
    		}
    	}
    Plus you also initialize the variables to 0 inside the loop, even though you already initialized them to 0 when defining them, which is a tad unnecessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I agree that declaring the variables in the for control is a better idea. However, note that even if you don't you still need to initialize j to 0 inside the for because after the first time through the outer loop it will be 2, not 0.

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. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM