-
Need help with a simple program
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:
Quote:
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
Quote:
#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
-
Re: Need help with a simple program
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?
-
Re: Need help with a simple program
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:
***** *****
***** *****
***** *****
*****
*****
*****
***** *****
***** *****
***** *****
-
Re: Need help with a simple program
hmm, ya that's not at all how I typed, why is it deleting my spaces?
-
Re: Need help with a simple program
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++)
for (squarerow=1, squarerow <= ySq, ySq++)
for (boardcol=1, boardcol <= x, boardcol++)
for (squarecol=1, squarecol <= xSq, squarecol++)
if(x%2==1 && y%2==1)
cout<<'*'
if(x%2==0 && y%2==0)
cout<<'*'
else
cout<<' '
if (printBoard(x,y,xSq,ySq))
cout << "Invalid parameter\n";
return 0;
}
-
Re: Need help with a simple program
Code:
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
OOOOXXXXXXOOOO
OOOOXXXXXXOOOO
OOOOXXXXXXOOOO
OOOOXXXXXXOOOO
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
Code:
void printLine(int len, char ch)
{
for(int i=0; i<len; i++)
cout << ch;
}
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;
}
-
Re: Need help with a simple program
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.
// your code goes here
{
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( x%2 == 1 && y%2 == 1)
cout<<'*';
if(x%2 == 0 && y%2 == 0)
cout<<'*';
else
cout<<' ';
}
-
Re: Need help with a simple program
Quote:
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:
Code:
#include <iostream>
using namespace std;
// the rest
Quote:
Originally Posted by C++noobie
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
Code:
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
XXXXXOOOOOXXXXX
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:
Code:
for(int i=0; i<x; i++)
{
printLine(xSq, i%2?' ':'X'); // no k so far
}
At this point you have something like:
thus, a single line. A cell has ySq lines, so you must put the code above in another loop:
Code:
for(int j=0; j<ySq; j++)
{
for(int i=0; i<x; i++)
{
printLine(xSq, i%2?' ':'X'); // still no k
}
cout << endl;
}
But the table has y rows. A new loop is needed:
Code:
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;
}
}
this part:
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?
-
Re: Need help with a simple program
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.
-
Re: Need help with a simple program
Quote:
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.
This is C code:
Code:
#include <stdio.h>
#include <conio.h>
void printLine(int len, char ch)
{
for(int i=0; i<len; i++)
printf("%c",ch);
}
void printBoard(int x, int y, int xSq, int ySq)
{
char ch = 0;
for(int k=0; k<y; k++)
{
for(int j=0; j<ySq; j++)
{
for(int i=0; i<x; i++)
{
if((i+k)%2 == 0) ch = 'X';
else ch = ' '; // a space here
printLine(xSq, ch);
}
printf("\n");
}
}
}
void main()
{
printBoard(3,3,5,4);
}
-
Re: Need help with a simple program
heh, fixed most of it now, just needed to remove a semi colon.
So, I fixed my syntax errors, but now I get
hw4.C:37: warning: control reaches end of non-void function
not sure what this means
-
Re: Need help with a simple program
Post the code for which you get that warning. Not only the line 37, but the entire function. And please wrap your code between CODE tags.
-
Re: Need help with a simple program
Code:
// 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
{
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( x%2 == 1 && y%2 == 1)
cout<<'*';
if(x%2 == 0 && y%2 == 0)
cout<<'*';
else
cout<<' ';
return 0;
}
}
}
}
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;
}
Sorry, didn't know about the [CODE] command. Thanks so much for the help you're giving me, really saving me.
-
Re: Need help with a simple program
Ypu put a ; after the most inner for. And the if-else is not good:
Code:
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
{
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;
}
}
return 0;
}
-
Re: Need help with a simple program
I feel like I'm so close. When I compile with the inputs of an 8x8 board and 5x3 squares I get
Code:
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
-
Re: Need help with a simple program
It works perfectly for me:
Code:
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
***** ***** ***** *****
The code (don't forget the headers):
Code:
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
{
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;
}
}
return 0;
}
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;
}
-
Re: Need help with a simple program
Code:
#include <iostream>
using namespace std;
// prototype
This is the header Im using, is that right?
-
Re: Need help with a simple program
I've compared my code to yours about 10 times, I just can't see any differences.
Code:
#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
{
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;
}
}
return 0;
}
-
Re: Need help with a simple program
Yes. Still not working? Are you inputing the correct values? 8, 8, 5, 3?
-
Re: Need help with a simple program
Positive, this is the output
Code:
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
-
Re: Need help with a simple program
YOU ARE NOT LISTENING! Didn't I tell you about the ; after the most iner for? Of course it doesnt work. Remove that ';'.
Code:
for(int squarecol=1; squarecol <= xSq; squarecol++);
-
Re: Need help with a simple program
I <3 you =) .
That did it, you are my hero.
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
-
Re: Need help with a simple program
Code:
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);
else
if(printBoard(x,y,xSq,ySq))
cout << "Invalid parameter\n";
return 0;
}
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.
-
Re: Need help with a simple program
Quote:
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?
-
Re: Need help with a simple program
It probably would be more efficient, would it work if I just moved all those if statements to the printboard function?
-
Re: Need help with a simple program
Will this work? besides the top while loop, that's a work in progress.
Code:
{
{
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;
}
}
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;
}
}
-
Re: Need help with a simple program
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:
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
}
-
Re: Need help with a simple program
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.
Code:
int boardrow = 1;
while (boardrow <=y)
{
cout<<boardrow;
boardrow++;
}
cout<<endl;
int boardcol = 1;
while (boardcol <=x)
{
cout<<boardcol<<endl;
boardcol++;
}
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;
}
}
return 0;
}
return 1;
}
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.
-
Re: Need help with a simple program
can anyone help with this last problem =/
-
Re: Need help with a simple program
To clarify the if-else problem:
Code:
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:
Code:
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:
Code:
if(condition)
{
// single or multiple instruction(s) here
}
else
{
// single or multiple instruction(s) here
}
Quote:
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.
-
Re: Need help with a simple program
Quote:
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.