hello.. i need tips to solve the problem. The situation is like this:

I have randomly generated 20 (pop) array of length = board. The output is this:

1 3 6 4 1 4 6 6
2 0 1 1 1 3 1 3
3 6 3 4 7 4 6 1
4 6 5 4 6 7 7 6
3 2 5 0 3 3 3 6
7 3 2 5 1 0 5 7
5 4 3 4 6 5 5 3
3 5 1 2 4 3 6 2
0 2 0 6 0 0 6 5
2 1 2 6 6 5 1 4
5 7 4 2 4 0 6 0
3 4 0 7 2 3 1 2
6 2 1 5 1 3 7 2
0 2 1 2 1 2 5 1
4 0 6 5 1 5 1 0
2 2 1 4 3 7 7 6
7 4 6 6 5 4 6 6
3 7 7 1 1 7 1 7
1 5 1 6 5 4 6 2
2 6 5 0 4 7 2 7

I also have created an area matrix (of size board x board) with initial value=0.

Next:
I want to fill the area matrix with value=1, but the position of the value is according to the number generated earlier, first row only. Means that...for

1 3 6 4 1 4 6 6

each column will be the position of value=1 for each row in the area matrix, so that the output will be like this:

0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0

Can someone give me any HINTS?
The code is shown below:

#include <iostream>
#include <stdlib.h>

int main()
{

const board=8;
const pop=20;
using std::cout;
using std::endl;
int table [30][1000];
int area[board][board];


int r;
int c;


// Initialize random number generator.

for(r=0; r<=pop-1; r++)//row
{
	for(c=0; c<board; c++)
	{
		table[r][c] = rand()%board;
	} 
}

for(r=0; r<=pop-1; r++)//row
{
	cout << endl;	
	for(c=0; c<board; c++)
	{
			cout << "\t" << table[r][c]; // print out the numbers
	} 

	cout << endl ;
}

system("pause");
cout << "\n";

// intialize the area matrix equal to 0
for(r=0; r<board; r++)
{
	for(c=0; c<board; c++)
	{
		area[r][c]=0;
		cout << "\t" << area[r][c];
	}
	cout << endl << endl;
}
for(r=0; r<board; r++)
    {
        //iterate through all elements from chosen row of board array
        //r is your row number in area array
        //value of current element from board array is an index of element in r-st row of area array and this element should be set to 1
    }

What's more at line #7 and #8 You missed the type. Also your using declaration inside main function looks odd for me.

Edited 6 Years Ago by pecet: n/a

oh? alright, I have put the "using" outside the main.
and the function like this right?

for(r=0; r<board; r++)
{
// I only want from first row (row=0) from table[][]
	area[r][table[0][c]]=1; 
}

then, when I tried to print out the new matrix, it was force to terminated. any idea?

coz I use

table[r][c]

at the beginning when I generated the initial random arrays, where r=row and c=column.

so in

table[0][c]

, row=0 and I just left the c with no values coz I dunno what to put. Is it wrong?

Edited 6 Years Ago by amel036yippee: wrong spelling

Yes, it's wrong cause You have to iterate through all elements in table[0] . So you have to use the same variable which is used in your for loop.

ahaha!! now I understand! the code is like this:

for(r=0; r<board; r++)
{
	area[r][table[0][r]]=1;
}

//print out the new matrix
for(r=0; r<board; r++)
{
	for(c=0; c<board; c++)
	{
		cout << "\t" << area[r][c];
	}
	cout << endl << endl;
}

and... tada! the output is CORRECT!!! thank you very much pecet~ :)

Yup, that's correct:)
Just one thing more. You said: "I have randomly generated 20 (pop) array of length = board." Now, look at your line #11:

int table [30][1000];

and think about it.

hurm... it was defined before I define pop and board.
so... it should be

int table [pop][board];

right? or still wrong..?

Edited 6 Years Ago by amel036yippee: nothing

haa?? waaiit! I got one more question. The code we solved just now is only for row=0. What about if I want to print out for the whole 20 rows?
I mean... there will be 20 output matrices.

Edited 6 Years Ago by amel036yippee: nahhh

Fill area array with zeros, and use your loop for table[1], then again, fill with zeros, and iterate through table[2]... Probably the best idea to do it is using another loop.
If You don't want to fill area array with zeros over and over again, you need to add one more dimension to this array.

int area[pop][board][board];

I see... thanks. I will try to work it out later. It's 2.30 a.m. now at my place.... haha! and only one week until final exam.

thanks again~ :)

This question has already been answered. Start a new discussion instead.