OK, I've got a [90][10] 2d array. I have a set of pointers that point to the values in the array by column. each column holds different info. It's a payroll file so the zero column of my array corresponds to an employee number. What I want to do is get my pointer array (which has already been initialized to its corresponding employee number in the 2d array) to point to a random employee number. Thanks.

while (getline(report, next))
	{
		int i = 0, j = 0;
		
		for (i = 0; i < R_MAX; i++) 
			for (j = 0; j < C_MAX; j++) 
			{
				report >> payroll[i][j];
			}
		report.close();

	}	
	
	for (i = 0; i < R_MAX; i++) 
		for (j = 0; j < C_MAX; j++) 
		{
			
		cout << setw(12) << payroll[i][j];
			if((j % 9 == 0)&&(j != 0))
			{
				cout << endl;
			}
		}	
	
	for (i = 0; i < R_MAX; i++) 
		for (j = 0; j < C_MAX; j++) 
{
	
	if(j == 0)
	{
		string temp = payroll[i][j];
		string *employee_ptr = &temp;
		cout << *employee_ptr << endl;
		employee_ptr++;
	}
	
		
	if((j % 5 == 0)&&(j != 0))
	{
	string temp = payroll[i][j];
	string *reg_ptr = &temp;
	cout << *reg_ptr << endl;
	reg_ptr++;
	}
	
	if((j % 6 == 0)&&(j != 0))
	{
	string temp = payroll[i][j];
	string *ot_ptr = &temp;
	cout << *ot_ptr << endl;
	ot_ptr++;
	}
	
	if((j % 7 == 0)&&(j != 0))
	{
		string temp = payroll[i][j];
		string *gros_ptr = &temp;
		cout << *gros_ptr << endl;
		gros_ptr++;
	}	
	
	if((j % 8 == 0)&&(j != 0))
	{
		string temp = payroll[i][j];
		string *tax_ptr = &temp;
		cout << *tax_ptr << endl;
		tax_ptr++;
	}	
	
	if((j % 9 == 0)&&(j != 0))
	{
		string temp = payroll[i][j];
		string *net_ptr = &temp;
		cout << *net_ptr << endl;
		net_ptr++;
	}	
		
		
}

void randemployee(string *employee_ptr[], const int C_MAX, const int R_MAX)
{
	int i , r;
	
	for (i = 0; i < R_MAX; i++) 
		{
			r = i + (rand() % (R_MAX - i));						// Random remaining position.
			
			string temp = *employee_ptr[i]; *employee_ptr[i] = *employee_ptr[r]; *employee_ptr[r] = temp;
		}
	cout << "shuffled values follow\n";
	
	for (i = 0; i < R_MAX; i++)
		{
			cout << *employee_ptr[i];
			cout << endl;
		}
}

Recommended Answers

All 3 Replies

If i read your question correctly then you can just do this :

int twoD[90][10];
//initialize twoD here 

int * randPtr = 0;
int row = random(0,90); //some random function that returns a random number from [0,90)
int col = random(0,10);
randPtr = &twoD[row][col];

I'm looking to shuffle the values, more than randomize I suppose. The pointer array is initialized to the 90 rows of the 0 column of my 2d array. This column contains "employee number" which has been read from a file. I want these initialized values in the pointer array to point to different indexes, with no index being pointed to by the same pointer. I then need to display the shuffled employee numbers.

Thanks so much.

there is a random shuffle function in the algorithm header that might work for you. not sure if it can be used on 2d arrays though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.