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.