|
-
March 21st, 2005, 08:52 PM
#1
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:
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
-
March 22nd, 2005, 02:52 AM
#2
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?
-
March 22nd, 2005, 11:40 AM
#3
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:
***** *****
***** *****
***** *****
*****
*****
*****
***** *****
***** *****
***** *****
Last edited by C++noobie; March 22nd, 2005 at 11:43 AM.
-
March 22nd, 2005, 11:43 AM
#4
Re: Need help with a simple program
hmm, ya that's not at all how I typed, why is it deleting my spaces?
-
March 22nd, 2005, 01:00 PM
#5
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;
}
-
March 22nd, 2005, 02:17 PM
#6
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;
}
-
March 22nd, 2005, 03:31 PM
#7
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<<' ';
}
-
March 22nd, 2005, 04:27 PM
#8
Re: Need help with a simple program
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
 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?
-
March 22nd, 2005, 04:42 PM
#9
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.
-
March 22nd, 2005, 04:48 PM
#10
Re: Need help with a simple program
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);
}
-
March 22nd, 2005, 04:52 PM
#11
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
-
March 22nd, 2005, 05:01 PM
#12
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.
-
March 22nd, 2005, 05:04 PM
#13
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.
-
March 22nd, 2005, 05:09 PM
#14
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;
}
-
March 22nd, 2005, 05:21 PM
#15
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:
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|