[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
homework help C++
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /wsr/ - Worksafe Requests

Thread replies: 11
Thread images: 2
File: catglasses.jpg (577 KB, 1920x1200) Image search: [Google]
catglasses.jpg
577 KB, 1920x1200
I need to make a chess board in C++ what i have so far is this
#include <iostream>
#include <string>
using namespace std;
const string BLACK= "********";
const string WHITE= " ";
int main()
{string whiteRow;
string blackRow;
whiteRow= WHITE+BLACK+WHITE+BLACK+WHITE+BLACK+WHITE+BLACK;
blackRow= BLACK+WHITE+BLACK+WHITE+BLACK+WHITE+BLACK+WHITE;
cout<<whiteRow<<endl;
return 0;
}
what i want to do is to have the cout replicate itself five times in that one row
but i dont know how to do that
>>
>declaring variables without defining them
>not knowing for loops
>>
>>42402
i just started the proggraming class i dont know how to do that yet
>>
File: 1452467923285.jpg (80 KB, 576x972) Image search: [Google]
1452467923285.jpg
80 KB, 576x972
>>42405
Better learn fast.
>>
>>42405
First, you should get into the habit of initializing your variables on the same line you declare them. This way you can always be sure that they have legitimate values; the compiler won't automatically set them to 0 or NULL or "" or whatever unless you tell it to. Since you already know what you're going to put in them, you can skip setting them to empty and just initialize them with the values you'll be using.

Second, I can't believe they haven't taught you loops? Loops are how you do things multiple times. For example, cout-ing over and over. C++ has three kinds of loops: 'while...', 'do..while,' and 'for...'. Of these, 'for' is probably the most common/versatile (preparing for replies in 3...2...)

A basic 'for' loop has 4 parts:
> for ( A; B; C) {
>> D;
> }

First, the statement "A" will run. One of the ways you can use this is to set up a variable that keeps track of how many times the loop has gone around. Next, the condition "B" will be evaluated, and if it's true, the "D" code block, then the "C" statement, will be run before checking again. If it's still true, "D" and "C" will be run again before checking once more, and so on, until "B" is finally false.

Hopefully this isn't too confusing
>>
>>42428
i appreciate it!
>>
>>42428
>First, you should get into the habit of initializing your variables on the same line you declare them. This way you can always be sure that they have legitimate values; the compiler won't automatically set them to 0 or NULL or "" or whatever unless you tell it to.
This is a bad habit: the compiler also won't warn you that it hasn't been initialised properly because it sees the first "to be sure" initialisation and assumes that's what you want. It'll thwart your use of static analysis/verification tools for the same reason.

Only read your variables after they've been written.
Only write your variables with values they're actually supposed to have.
Never write some arbitrary value in just to get the compiler to shut up.
>>
>>42396
#include <iostream>
#include <string>
using namespace std;

int isWhite(int x,int y) {
int oddx = (x%16 >= 8);
int oddy = (y%16 >= 8);
return (oddx == oddy);
}

int main()
{
int x,y;
for (y=0;y<64;y++)
{
for (x=0;x<64;x++)
{
cout<<(isWhite(x,y)?" ":"#");
}
cout<<endl;
}

}
>>
>>42548
This also generalises:

#include <iostream>
#include <string>
using namespace std;


const int squareLength = 1;
const int squareCount = 8; // chessboards are 8x8

int isWhite(int x,int y) {
int oddx = (x%(squareLength*2) >= squareLength);
int oddy = (y%(squareLength*2) >= squareLength);
return (oddx == oddy);
}

int main()
{
int x,y;
for (y=0;y<(squareLength*squareCount);y++)
{
for (x=0;x<(squareLength*squareCount);x++)
{
cout<<(isWhite(x,y)?" ":"#");
}
cout<<endl;
}

}
>>
>>42551
I thought you could do something like
int iswhite(int x, int y)
{ return (x/squareLength + y/squareLength + 1)%2;}
>>
void rowWhite(){
for (y=1; y<6; y++){
cout<<whiteRow<<endl;
}
}

//in main()...
for (row=1; row<9; row++){
if (row%2) rowWhite();
else rowBlack();
}
Thread replies: 11
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.