The Question is Write a program that output the numbers 0 to 8 in three columns, where the width of each of them 3. The output should be as follows:

0    1       2
3    4       5
6    7       8

so what i've done so far is :

#include<iostream.h>
void main()
{
int x;
for (x=0;x<=8;x++)
cout<<x<<"t"<<x++<<"t"<<x++<<"n"<<endl;
}

my problem is that my output is the oppisite like the following :

2    1    0
5    4    3
8    7    6

can someone tell me what i'm doing wrong ??
or can you give me a hint ?

Answered by ddanbe 2,724 in a post from

Don't mess with the loop variable in a for loop.
Copy it and mess with the copy.

Answered by ddanbe 2,724 in a post from

Well your loop counter x is counting from 0 to 8 in the for loop. Fine!
You can use this counter in the for loop anyway you please, but never change it!
You do it if you do x++ .
Strange things might happen.
Set i = x and change …

oh boy

along with ddanbe, if you need to "change" the value do not modify the loop

cout << x << endl;
cout << x + 1 << endl;
cout << x << endl; // x is the same
cout << x++ << endl; //uh oh …

and btw, i left the \n endl because you had it in there

normally you would use one or the other, but for this i would say drop the \n and stick with the endl

Answered by ddanbe 2,724 in a post from

This is how I would do (and did) it in C#

int x;
for (x = 0; x <= 8; x+=3)
{
Console.WriteLine("{0}\t{1}\t{2}\n", x, x+1, x+2);
}

This can very easily be translated to C++ but I have no C++ compiler to test it.

All 20 Replies

Don't mess with the loop variable in a for loop.
Copy it and mess with the copy.

What do you mean ????

sorry i'm new in C++

Well your loop counter x is counting from 0 to 8 in the for loop. Fine!
You can use this counter in the for loop anyway you please, but never change it!
You do it if you do x++ .
Strange things might happen.
Set i = x and change the variable i instead.

but like this i'll get the output only 3 numbers ??!!

oh boy

along with ddanbe, if you need to "change" the value do not modify the loop

cout << x << endl;
cout << x + 1 << endl;
cout << x << endl; // x is the same
cout << x++ << endl; //uh oh we incremented
cout << x << endl;//still the increment

i wouldn't suggest using a for loop on this one, are you able to use a while or does the program specify for?

i'm required to use for!

for it is then

int col = 1;
for (x=0; x<=8; x++)
{
cout << x;
if(col == 3)
{
col = 1;
cout << "\n" << endl;
}
else
{
cout << "\t";
}
col++;
}

but like this i'll get the output only 3 numbers ??!!

That usually happens when you do one cout with 3 numbers, so I don't quite follow

and btw, i left the \n endl because you had it in there

normally you would use one or the other, but for this i would say drop the \n and stick with the endl

commented: Ah! +1

ok i like the idea but when i try it i get an error ( unexpected end of file found )

what should i do?

oh ok i found my mistake !!
but the output is 0 !!!!!!

my bad misplaced col++, was in a rush, but you still shouldn't be getting 0

int col = 1;
for (int x=0; x<=8; x++)
{
cout << x;
if(col == 3)
{
col = 1;
cout << endl;
}
else
{
cout << "\t";
col++;
}

}

my bad misplaced col++, was in a rush, but you still shouldn't be getting 0

int col = 1;
for (int x=0; x<=8; x++)
{
cout << x;
if(col == 3)
{
col = 1;
cout << endl;
}
else
{
cout << "\t";
col++;
}

}

Thank YOU :)
but can you explain to me how col works?? i mean in the if statement

Thank YOU :)
but can you explain to me how col works?? i mean in the if statement

Every time you output a number, you increment col. When col is 3, 3 number have been displayed (3 cols) so you drop to the next line and reset col to count the next 3 values output.

nice explanation waltp

the variable col is "column"

as waltp said when you are at position(column) 3 which will be the numbers(2, 5, 8), it will break the line, otherwise it will add tab

This is how I would do (and did) it in C#

int x;
for (x = 0; x <= 8; x+=3)
{
Console.WriteLine("{0}\t{1}\t{2}\n", x, x+1, x+2);
}

This can very easily be translated to C++ but I have no C++ compiler to test it.

commented: nice simplification +2

Nice one ddanbe

Here's c++ version, i stayed with separating couts rather than using formatting

for (int x = 0; x <= 8; x+=3)
{
cout << x << "\t" << x+1 << "\t" << x+ 2 << "\n";
}

He dickersonka if SQ89 isn't happy now i'll eat my shoes!!!
Separating couts is a mess, but so is formatting...

Myself, I wouldn't normally go the formatting route for examples just for readability, very nice touch though.

You're right the at least in my example the variables are separated from the formatting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.