Hi guys, Im taking an intro to programming course, because I've always been good at working with computers, unfortunatley I suck!!!! at programming, and thus I suck at this course.
I need help with a homework assignment very badly. Im not asking for you to do my homework, but any hints are very very welcome.
The assignment is as follows:
Write a program that prints a checkerboard of given dimensions. The provided main function prompts the user for the size of the board (note that x and y dimensions do not have to be equal) and for the size of individual squares (again, the dimensions of a square do not have to be equal). The following is an example of 8 x 8 board made out of 5 x 3 squares:
( I couldn't get the chest board he made to copy/paste properly)
The printout must include centered top and side labels for columns and rows. The size of the board will not exceed 9 x 9 and both dimensions of a square must be odd.
The program must check whether the parameters entered by a user are valid (e.g., greater than zero, a board size greater than 9, or a square dimension is not odd). If any of the parameters is invalid, the program should print an error message and exit.
Add your code to the provided main function. Except for adding your code, you must not modify the given code. You can either download the code or copy it to you account using the following Unix command (note the dot at the end of the line):
The code that he provides us is
#include <iostream>
using namespace std;
// prototype
int printBoard(int x, int y, int xSq, int ySq);
// Prints x times y "chess" board using xSq times ySq squares.
// Returns a nonzero value if an invalid param specified
// zero otherwise.
// your code goes here
int main () {
int x, y, xSq, ySq;
cout << "Enter horizontal dimension of the board : ";
cin >> x;
cout << "Enter vertical dimension of the board : ";
cin >> y;
cout << "Enter horizontal size of a square : ";
cin >> xSq;
cout << "Enter vertical size of a square : ";
cin >> ySq;
if (printBoard(x,y,xSq,ySq))
cout << "Invalid parameter\n";
return 0;
}
Once again, Im not asking for anyone to do my work(though I wouldn't mind ) ,I just need any hints or advice about how to tackle this. I just don't have the right kind of my mind to be a programmer.
Thanks,
Josh/C++ noob
cilu
March 22nd, 2005, 01:52 AM
What compiler do you use. What kind of application? A console one? The user must provide the dimension of the squares. How are the squares supposed to be drawn?
What part exactly you have problems with?
C++noobie
March 22nd, 2005, 10:40 AM
We use g++ as the compiler.
I really don't know how to go about writing the program. I just can't figure out how to write this.
These user inputs the square dimensions, say 5x3
a 5x3 square would look like with a 3x3 checkerboard would look like:
hmm, ya that's not at all how I typed, why is it deleting my spaces?
C++noobie
March 22nd, 2005, 12:00 PM
I think I'm getting close to writing this correctly
#include <iostream>
using namespace std;
// prototype
int printBoard(int x, int y, int xSq, int ySq);
// Prints x times y "chess" board using xSq times ySq squares.
// Returns a nonzero value if an invalid param specified
// zero otherwise.
// your code goes here
int main () {
int x, y, xSq, ySq, boardrow;
cout << "Enter horizontal dimension of the board : ";
cin >> x;
cout << "Enter vertical dimension of the board : ";
cin >> y;
cout << "Enter horizontal size of a square : ";
cin >> xSq;
cout << "Enter vertical size of a square : ";
cin >> ySq;
{
for (boardrow=1, boardrow <= y, boardrow++)
void printBoard(int x, int y, int xSq, int ySq)
{
for(int k=0; k<y; k++)
{
for(int j=0; j<ySq; j++)
{
for(int i=0; i<x; i++)
{
printLine(xSq, (i+k)%2?' ':'X');
}
cout << endl;
}
}
}
int main()
{
printBoard(3,3,5,4);
return 0;
}
C++noobie
March 22nd, 2005, 02:31 PM
I think the way you wrote that might be a little advanced =)
I think this is close, but I get a TON of syntax errors when I try and compile. Im sure im just doing something real stupid.
int printBoard(int x, int y, int xSq, int ySq);
// Prints x times y "chess" board using xSq times ySq squares.
// Returns a nonzero value if an invalid param specified
// zero otherwise.
I think this is close, but I get a TON of syntax errors when I try and compile. Im sure im just doing something real stupid.
Are you refering to my code? Obvious. You must include a header:
#include <iostream>
using namespace std;
// the rest
I think the way you wrote that might be a little advanced =)
Well, not really. You just hve to pay attention. Think about it: you must display xSq 'X' chars or ' ' (spaces) ySq times to create a square of the board. The board has x times y squares.
My first function printLine displays a line of a square
Notice that you can use anything instead of space (i sued 'O' to format it better). printLine() prints the part you see highlithed in blue. Of course, this function gets called many times, for each such line in the board.
The board has x squares on horizontal, so you must call this printLine in a loop:
for(int i=0; i<x; i++)
{
printLine(xSq, i%2?' ':'X'); // no k so far
}
At this point you have something like:
XXXXXOOOOOXXXXX
thus, a single line. A cell has ySq lines, so you must put the code above in another loop:
for(int j=0; j<ySq; j++)
{
for(int i=0; i<x; i++)
{
printLine(xSq, i%2?' ':'X'); // still no k
}
cout << endl;
}
selects the char to use for displaying. with some basic math skills you should figure this out.
Does it seem so hard to understand now?
C++noobie
March 22nd, 2005, 03:42 PM
I meant that your code seems to cover some stuff we haven't learned yet, and when I said that I was getting alot of syntax errors it was in the code that followed.
cilu
March 22nd, 2005, 03:48 PM
I meant that your code seems to cover some stuff we haven't learned yet
Like what? cout? Replace it with printf. The ?: operator? Replace it with if-else.
I have a couple of paramaters that I still need to work ( input must be greater than zero, a board size no greater than 9, or square dimension's odd. If any of the parameters is invalid, the program should print an error message and exit, and I have to make it so that the board is numbered.)
BUt I can't ask you to help anymore, unless you're really really bored. Thanks so much for the help again.
-Josh
C++noobie
March 22nd, 2005, 06:40 PM
int main (){
int x, y, xSq, ySq;
cout << "Enter horizontal dimension of the board : ";
cin >> x;
cout << "Enter vertical dimension of the board : ";
cin >> y;
cout << "Enter horizontal size of a square : ";
cin >> xSq;
cout << "Enter vertical size of a square : ";
cin >> ySq;
{
if(x<=0)
cout<<"Dimensions must be greater than 0\n";
if(y<=0)
cout<<"Dimensions must be greater than 0\n";
if(ySq<=0)
cout<<"Dimensions must be great than 0\n";
if(xSq<=0)
cout<< "Dimensions must be greater than 0\n";
if(x>9)
cout<< "Board dimensions must be less than 9\n";
if(y> 9)
cout<< "Board dimensions must be less than 9\n";
if(xSq%2==0)
cout<<"Square dimensions must be odd\n";
if(ySq%2==0)
cout<<"Square dimensions must be odd\n";
exit(1);
Now I know this isn't the most efficient way to write this, but it seems to work, except Im having trouble with the else statement. Says there is a syntax error before it, but I don't see anything.
And now my checkerboard isn't printing =(. Still haven't worked out how to number them yet either.
HighCommander4
March 22nd, 2005, 08:02 PM
Now I know this isn't the most efficient way to write this, but it seems to work, except Im having trouble with the else statement. Says there is a syntax error before it, but I don't see anything.
Remove the 'else'. It's not doing anything. And wouldn't it make more sense to have printBoard() do the error checking since that's what needs to return nonzero if there's an invalid number, or zero otherwise?
C++noobie
March 22nd, 2005, 08:04 PM
It probably would be more efficient, would it work if I just moved all those if statements to the printboard function?
C++noobie
March 22nd, 2005, 08:14 PM
Will this work? besides the top while loop, that's a work in progress.
{
{
int boardrow=1;
while (boardrow<=y){
cout<<boardrow;
boardrow++;
}
}
if(x<=0)
cout<<"dimensions must be greater than0\n";
if(y<=0)
cout<<"dimensions must be greater than 0\n";
if(ySq<<=0)
cout<"dimensions must be greater than 0\n";
if(xSq<=0)
cout<<"dimensions must be greater than 0\n";
if(x>9)
cout<<"board dimensions must be less than 9\n";
if(y>9)
cout<<"board dimensions must be less than 9\n";
if(ySq%2==0)
cout<<"Dimensions of square must be odd\n";
if(xSq%2==0)
cout<<"Dimensions of square must be odd\m";
return 1;
else
return 0;
}
}
HighCommander4
March 22nd, 2005, 08:25 PM
Three things wrong with your code:
1) If the program foudn one error, it will need to quit anyways, so why have it check for all the other errors? Instead of having a list of 'if'-s, all except the first one should be 'else if'-s so if any one error is found, it won't checkl for the other errors.
2) The entire error-checking thing should be before the actual loops in printBoard(). After all, if there's an error it shouldn't print anything, should it?
3) You either have to put a 'return 1' after each and every if statement, or put one at the very end of the function.
Here's your revised code:
int printBoard (int x, int y, int xSq, int ySq)
{
if (x <= 0)
cout << "dimensions must be greater than0\n";
// note the use of if else as opposed to just if
else if (y <= 0)
cout << "dimensions must be greater than 0\n";
else if (ySq <<= 0)
cout < "dimensions must be greater than 0\n";
else if (xSq <= 0)
cout << "dimensions must be greater than 0\n";
else if (x > 9)
cout << "board dimensions must be less than 9\n";
else if (y > 9)
cout << "board dimensions must be less than 9\n";
else if (ySq % 2 == 0)
cout << "Dimensions of square must be odd\n";
else if (xSq % 2 == 0)
cout << "Dimensions of square must be odd\m";
// no error, display the board
else
{
int boardrow = 1;
while (boardrow <= y)
{
cout << boardrow;
boardrow++;
}
cout << endl;
for (int boardrow = 1 ; boardrow <= y ; boardrow++)
{
for (int squarerow = 1 ; squarerow <= ySq ; squarerow++)
{
for (int boardcol = 1 ; boardcol <= x ; boardcol++)
{
for (int squarecol = 1 ; squarecol <= xSq ; squarecol++)
{
if ((boardrow + boardcol) % 2 == 0)
cout << '*';
else
cout << ' ';
}
}
cout << endl;
}
} // end of last for loop
return 0; // SUCCESS
} // end of else statement
return 1; // FAILURE
}
C++noobie
March 22nd, 2005, 08:47 PM
That works!! Very nice, thanks.
Last problem I have( I swear). I have to number the rows and columns, but I can't figoure out how to space the numbers out accordingly.
int boardrow = 1;
while (boardrow <=y)
{
cout<<boardrow;
boardrow++;
}
cout<<endl;
int boardcol = 1;
while (boardcol <=x)
{
cout<<boardcol<<endl;
boardcol++;
}
I can't figure out how to space them correctly and can't figure out how to get them to go vertical spaced and in front of the boxes=(
I know it has to do with the fact that the boxes must be odd, that way I at least always have a center where the number should be, how I apply this I don't know.
C++noobie
March 22nd, 2005, 10:59 PM
can anyone help with this last problem =/
cilu
March 23rd, 2005, 02:37 AM
To clarify the if-else problem:
if(xSq%2==0)
cout<<"Dimensions of square must be odd\m";
return 1;
else
return 0;
You cannot put 2 instructions here without defining a new block:
if(xSq%2==0)
{
cout<<"Dimensions of square must be odd\m";
return 1;
}
else
return 0;
That is why it is considered a good practice to use {} even if you have a single instruction:
if(condition)
{
// single or multiple instruction(s) here
}
else
{
// single or multiple instruction(s) here
}
I have to number the rows and columns, but I can't figoure out how to space the numbers out accordingly.
You have to explain what this means. I don't understand.
HighCommander4
March 23rd, 2005, 08:06 PM
Last problem I have( I swear). I have to number the rows and columns, but I can't figoure out how to space the numbers out accordingly.
Well, if you've already displayed the board and then want to number the rows and column, you'd have to use an OS-specific function to move the cursor back to previous lines and write in the numbers...
An alternative is to output the info into a 2D array as opposed to directly to the screen (you'd need to allocate an extra row and column in teh array for the numbers), then add in the numbers into the array afterward. Then display the final array with cout.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.