GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Feb-2009, 12:11
Psychomachy Psychomachy is offline
New Member
 
Join Date: Feb 2009
Posts: 4
Psychomachy is on a distinguished road

Need Help With Arrays!!!


Okay, this is my second semester in a C++ class, so I'm still new at this, and we're on multidimensional arrays.

Here's the problem:

The array is 120 by 120, and I need to put values in each one of those spots, of which are two numbers, I'll call them x and y.

Another factor is the 120 by 120 are divided into columns and rows, I'll call them i and j.

The array will look something like this:

x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x
y y y x x x y y y x x x y y y
y y y x x x y y y x x x y y y
y y y x x x y y y x x x y y y
y y y x x x y y y x x x y y y
x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x
x x x y y y x x x y y y x x x

..... so like a checkerboard...
120 of these across, in this case, 120/40 = i across
and 120/30 = j
x x x
x x x
x x x
x x x

I hoooopppeee all this makes sense , sorry....(please help!)

The problem I am having is inserting the x's and the y's into the array, and how they go in the correct location, I also do not know the numbers that i and j represent.
  #2  
Old 03-Feb-2009, 13:22
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need Help With Arrays!!!


You could create nested loops:

CPP / C++ / C Code:
int i, j;
char board[120][120];

for(i=0;i<120;++i)
{
  for(j=0;j<120;++j)
  {
    if( (i%6 <  3 && j%6 <  3) || 
        (i%6 >= 3 && j%6 >= 3) 
      ) board[i][j] = 'x';
    else board[i][j] = 'y';
  }
}
  #3  
Old 03-Feb-2009, 13:31
Psychomachy Psychomachy is offline
New Member
 
Join Date: Feb 2009
Posts: 4
Psychomachy is on a distinguished road

Re: Need Help With Arrays!!!


Quote:
Originally Posted by fakepoo
You could create nested loops:

CPP / C++ / C Code:
int i, j;
char board[120][120];

for(i=0;i<120;++i)
{
  for(j=0;j<120;++j)
  {
    if( (i%6 <  3 && j%6 <  3) || 
        (i%6 >= 3 && j%6 >= 3) 
      ) board[i][j] = 'x';
    else board[i][j] = 'y';
  }
}


why the 6s and 3s?
  #4  
Old 03-Feb-2009, 14:04
Psychomachy Psychomachy is offline
New Member
 
Join Date: Feb 2009
Posts: 4
Psychomachy is on a distinguished road

Re: Need Help With Arrays!!!


I implemented that code into my program, and the output was a bit different than I needed, maybe a mistake, it was like this

xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy

rather than:

xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
yyyxxxyyyxxxyyyxxx
yyyxxxyyyxxxyyyxxx
yyyxxxyyyxxxyyyxxx
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
xxxyyyxxxyyyxxxyyy
  #5  
Old 03-Feb-2009, 14:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need Help With Arrays!!!


Based on your pattern...
Code:
x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x y y y x x x y y y x x x y y y y y y x x x y y y x x x y y y y y y x x x y y y x x x y y y y y y x x x y y y x x x y y y x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x x x x y y y x x x y y y x x x
...you need to check to see which column and row an 'x' would exist. When we take the row and divide by 6, we need to know what the remainder is and that is our offset from position 0. We know that offsets of 0, 1, or 2 will be the first group and 3, 4, or 5 will be the second group.
Code:
x x x y y y x x x y y y x x x Notice that the bolded 'x' in the above row has an index of 7. It's offset would then be 7%6 which is 1. Because 1 is less than 3, it goes in the first group. This group could be an 'x' or a 'y' depending on the group from the column.

Does this make sense?
  #6  
Old 03-Feb-2009, 14:17
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need Help With Arrays!!!


CPP / C++ / C Code:
#include <iostream>
using namespace std;

#define ROWS 20
#define COLS 20

int main()
{
	int i, j;
	char board[ROWS][COLS];

	for(i=0;i<ROWS;++i)
	{
	  for(j=0;j<COLS;++j)
	  {
		if( (i%6 <  3 && j%6 <  3) || 
			(i%6 >= 3 && j%6 >= 3) 
		  ) board[i][j] = 'x';
		else board[i][j] = 'y';
	  }
	}

	// Print the board
	for(i=0;i<ROWS;++i)
	{
	  cout << endl;
	  for(j=0;j<COLS;++j)
	  {
		cout << board[i][j];
	  }
	}
	
	getchar();
	return 0;
}
I used this exact code and it worked exactly as expected.
  #7  
Old 03-Feb-2009, 14:48
Psychomachy Psychomachy is offline
New Member
 
Join Date: Feb 2009
Posts: 4
Psychomachy is on a distinguished road

Re: Need Help With Arrays!!!


Thank you, I think I understand now
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Arrays as function arguments - return arrays from functions pisuke C Programming Language 2 25-Jul-2007 12:03
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 16:54
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.