0

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 ?

Edited by Nick Evan: Fixed formatting

4
Contributors
20
Replies
21
Views
8 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • 1
    ddanbe 2,577   8 Years Ago

    This is how I would do (and did) it in C# [CODE=csharp] int x; for (x = 0; x <= 8; x+=3) { Console.WriteLine("{0}\t{1}\t{2}\n", x, x+1, x+2); }[/CODE] This can very easily be translated to C++ but I have no C++ compiler to test it. Read More

0

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.

0

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

0

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?

0

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++;
}
0

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

1

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

Votes + Comments
Ah!
0

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

what should i do?

0

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

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++;
   }
   
}
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++;
   }
   
}

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

0

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.

0

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

1

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.

Votes + Comments
nice simplification
0

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";            
}
0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.