John
John

Reputation: 21

C++ Programming Help! It wont work?

The first column contains the numbers 1 through 5 Subsequent columns contain the result of multiplying the number in the First column by the numbers 0 through 9

Okay I got this far but its not working and i have no clue why! if you can point me in the right direction that would be very helpful :)

#include <iostream>
using namespace std;

int main()
{
    for ( int i=0 ; i < 5; i++)
    { 
        cout << " ";
        // end for

        for ( int j = 0; j>=i; j ++) 
            cout << endl;
    }  // end for

    return 0;
}   //end of main function

Upvotes: 0

Views: 169

Answers (3)

Maxpm
Maxpm

Reputation: 25612

Although you have the right idea with the nested loops, your current code does nothing but output a few spaces and newlines. Let's examine your code line-by-line:

for ( int i=0 ; i < 5; i++)

The iterator in this loop will start at zero and end at four. Although starting at zero is acceptable in many cases, it's a lot easier here to just start at 1. So, it should be changed to this:

for (int = 1; i <= 5; i++)

Now, the next line.

cout << " ";

All this does is print a space. You have the number, but you're not doing anything with it. You should print i before the space, like so:

cout << i << " ";

We run into trouble again at your second (inner) loop.

for ( int j = 0; j>=i; j ++) 
    cout << endl;

It is good that you initialize j to zero. However, your conditional, j >= i, means that the loop will continue executing as long as j is greater or equal to i. You want it to run as long as it's less than or equal to nine. Additionally, all you're doing inside the loop is printing a newline. You should change it to print the product of i and j:

for (int j = 0; j <= 9; j++)
    cout << (i * j) << " ";

When you put it all together, you should end up with something like the following code. Note that I added some extra formatting and I changed a few identifiers for clarity.

#include <iostream>
#include <iomanip> // Needed for std::setw, which makes sure everything lines up.

int main()
{
    for (int firstNumber = 1; firstNumber <= 5; firstNumber++)
    {
        std::cout << std::setw(3) << firstNumber << " | "; // Output the first number of the row.  This makes up the first column.

        for (int secondNumber = 0; secondNumber <= 9; secondNumber++)
        {
            std::cout << std::setw(3) << (firstNumber * secondNumber) << " "; // Output the rest of the numbers in the row.
        }

        std::cout << std::endl; // We're done with the row, so make a linebreak.
    }
}

Hope this helps!

Upvotes: 1

JMcCarty
JMcCarty

Reputation: 759

If i understand what your trying to do, your code will never work.

You want something like this:

for(int i=1; i <=5; i++)
{
   std::cout << i << " ";
   for(int j=0; j <=9; j++)
     std::cout << i*j << " ";

   std::cout << std::endl;
} 

The formating won't be perfect but its closer to what you seem to be looking for.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34655

The first column contains the numbers 1 through 5 Subsequent columns contain the result of multiplying the number in the First column by the numbers 0 through 9

Hhh..

  • In your snippet, the outer loop runs from 0 to 4. But the question is from 1 to 5.
  • Inner loop should run form 0 through 9, which means it is independent of i value of outer loop. So, the condition for the inner loop j>=i is wrong.

Upvotes: 3

Related Questions