Hi all,

This is my maiden post on not only this forum, but in fact any programming forum so please bear with me. I must say that this question does relate to a piece of homework, but it is the final member function in a series of about 8 and I will be posting all of my own code for you.

OK. The specs required me to read in a file which consisted of a number of rows and a number of columns, as well as a series of 0s and 1s (stored as bools). I was provided with a header file which contained a series of member functions to be implemented as well as three private data members:

1 - 2 variables to store rows and columns, respectively
2 - a 2d vector of vector< bool> - grid <vector <bool> >

The final member function rotates a "bitmap", which for the purposes of this assignment is a very simple piece of ascii art. Here is the problem:

*Everything* works, except for the fact that when I try to copy the contents of a temporary 2d vector I have created to manipulate the 1d vector, it fails and throws a range error. Let me show you the code:

void bitmap::rotate()
{

vector<vector <bool> > Vec;	// vector created for the purpose of rotating contents of grid 	

vector <bool> Vec1;		// 1d vector for pushing each column of original vector onto                Vec temporarily before assigning back to grid
		
	for (int i = 0; i < numcols; i++)		// outer loop controls column number which      will stay the same since we are cycling through the rows in this case
	{
		for (int j = 0; j < numrows; j++)	// inner loop cycles through rows, keeping column the same
		{
			Vec1.push_back(grid.at(numrows - 1 - j).at(i));			// push the value at the bottom left of original vector onto top left of the new vector
		}
		
		Vec.push_back(Vec1);		// push entire new 1d vector onto Vec
		Vec1.clear();				// clear Vec1 so that it is ready for next iteration
	}
	
	for (int k = 0; k < numcols; k++)   // this is simply for debugging to demonstrate that Vec contains the correctly rotated image
	{
		for (int l = 0; l < numrows; l++)
		{
				if (Vec.at(k).at(l) == 0)
					cout << " ";
				else
					cout << "*";
		}
		
		cout << endl;
	
	}
	
	grid = Vec;
							
}

I am really at the end of my tether because I can not think of a single thing to try. I have tried clearing grid and resizing and then copying the individual elements over. This also did not work. Any help would be extremely greatly appreciated. If I have posted too much information or in an annoying format, please tell me so that I do not make the same mistake twice.

Kind regards,

Daniel

Recommended Answers

All 3 Replies

void bitmap::rotate()
{

vector<vector <bool> > Vec;	// vector created for the purpose of rotating contents of grid 	

vector <bool> Vec1;		// 1d vector for pushing each column of original vector onto                Vec temporarily before assigning back to grid
		
	for (int i = 0; i < numcols; i++)		// outer loop controls column number which      will stay the same since we are cycling through the rows in this case
	{
		for (int j = 0; j < numrows; j++)	// inner loop cycles through rows, keeping column the same
		{
			Vec1.push_back(grid.at(numrows - 1 - j).at(i));			// push the value at the bottom left of original vector onto top left of the new vector
		}
		
		Vec.push_back(Vec1);		// push entire new 1d vector onto Vec
		Vec1.clear();				// clear Vec1 so that it is ready for next iteration
	}
	
	for (int k = 0; k < numcols; k++)   // this is simply for debugging to demonstrate that Vec contains the correctly rotated image
	{
		for (int l = 0; l < numrows; l++)
		{
				if (Vec.at(k).at(l) == 0)
					cout << " ";
				else
					cout << "*";
		}
		
		cout << endl;
	
	}
	
	grid = Vec;
							
}

How far do you get before you get the out-of-range error (i.e. what line number above, what value of i and j, and what are numcols and numrows)? Do you get the range error when numcols and numrows are the same or only when they are different? If you don't get the error when numrows = numcols, but you do get it when they are not equal, chances are that somewhere you have mixed up numrows and numcols, or somewhere you have mixed up i and j. And I assume that numrows and numcols are set correctly and correctly reflect the data in the grid 2-dimensional vector?

Thank you so much!!! It's funny how sometimes you need someone to change the way you are looking at the problem. I realised after I thought about a number of the possibilities you raised what the problem was. Whilst I had made the new 2d vector the right size I had not changed the data members to reflect this so that when another one of my functions tried to display the contents of grid vector it threw a range error. It always seems childishly simple (which to you guys it is!) when you work it out. Thank you so much for your help. This web site is incredible. I cannot believe how quickly you helped me resolve my problem. Thanks again!!!
Daniel

You're welcome, I'm glad you were able to solve your problem, and welcome to Daniweb.

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.