Does anyone know how to writea program to print
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 and so on until it is down to one.


Anyone know how to right a function that perform this with a knowledge up to only loops not recursive:
===========
* *
* *
* *
* *
* *
===========

Anyone know how to right a function that performs thiswith a knowledge of only loops not recursive:

***********
*********
*******
*****
***
*

Does anyone know how to writea program to print

Yup. Next question :)

create two loops -- outer loops counts backwards from 10 to 1, the inner loop counts forwards from 1 to the value of the outer loop counter. Then print the value of the inner loop counter.

this is what i have, how do i fix it

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
for(int x =1;x<10;x--)
{
for(int y=x;y=x;y++)
cout<<x;
cout<<endl;
}


system("Pause");
return 0;
}

Take a pencil and paper and write out what your for loops are doing and see if it sounds like what Ancient Dragon posted.

The second program requires two nested loops. The body of the inner loop can use an if/else statement to determine what to display where. I suspect the diagram is supposed to be the outline of box and that there are space char between the asterisks to placethem in the appropriate spot.

The third program is basically the same as the first, except you output asterisks, not a digit.

To maintain indentation, spacing, etc. when posting code or diagrams you should use code tags as discribed in the sticky notes at the top of the board.

It's felt to be better form to pause the program with an input statement rather than with system("Pause"); to keep the console window open for viewing.

Do you really need iomanip?

This is what I have but doesnt count down the right way and I cant figure it out
#include<iostream>
using namespace std;
int main()
{
for(int x = 9;x>=1;x--)
{
for(int y=x;y>=1;y--)
cout<<x;
cout<<endl;
}


system("Pause");
return 0;
}

Print out the value of y instead of x, and you'll get the desired results, except backwards. To fix this, just reverse the loops (forward instead of backwards).

The other ones that you asked about should be easy once you've figured this one out.

[edit]And PLEASE use [code][/code] tags!!![/edit]

Edited 3 Years Ago by Dani: Formatting fixed

It's better, but I don't think you used pencil and paper to write out what the loops are doing, did you? Even speak out loud what the second loop is doing and you should be able to see your problem.

PLEASE use code tags!

I dont see what you mean by reversing them

Reversing them, ie. for (int index=1; index<=9; index++) .

Okay lets put it this way:

  • You need an output like:

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7

  • Notice that the ending number of each line keeps on decrementing each row. For eg. its 9 for the first row, 8 for teh second row and so on.
  • So.. use a for loop which will count from 9 down to 1 (keep the variable of this for loop as outer.
  • Use a second for loop to control what gets printed. Keep the initial value of that for loop as 1 since 1 is common to each column that is printed. Name this for loop variable as "inner". Keep the terminating condition of this for loop to be less than or equal to "outer".
  • This way, your outer for loop takes care of the number of columns that should be printed while the inner for loop actually prints them.

There's often more than one way to solve a programming problem. joeprogrammer is sending you down one road, and Ancient Programmer down the other. Unless you understand what the loops are doing however, it won't matter which road you take.

In your second program you don't want to print out x, that will just give you 9s on the first line, 8s on the second etc. Instead you want to print from 1 to x, which could be the value y, if you set up the second loop right by understanding what you are trying to do, by writing it down, etc.

Comments
well said - ~s.o.s~

yeah I got how to reverse the first loop it was the second one that is confusing me still. I should made that clear when I posted

You only need to reverse the second loop. (Which is sort of what I was trying to say before.) Thus, your loops would look like this:

for(int x = 9;x>=1;x--)

{

for(int y = 1;y<=x;y++) 

cout<<y;

cout<<endl;

}
}

Ok now i see....so if i want to make *'s appear now i should have to change the int to char but then i dont see how you get them to print because it isn't the same as with the numbers is it.

yeah I got how to reverse the first loop it was the second one that is confusing me still. I should made that clear when I posted

Like Mr. Lerner said there, unless you yourself understand what looops are and how they function, it would be a tough time for you.

Just keep in mind that the inner for loop is executed outer for loop number of times.

For each iteration of outer loop,
Run the entire inner loop.

I gave you the answer to one of your homework questions. Now, can't you figure out how to do the rest? Loops are difficult, and it's because they execute a lot of instructions in a small amount of code.

~s.o.s~ explained loops very well, and if you want to know more, you should read up on them (use Google).

Just replace the y in cout with your character.

char mychar = '*' ; // this is how we declare and initialize characters

// your loops here
cout << mychar ;

Here's the code for the first problem:

#include <iostream>

using namespace std;

int main()
{
    int num;
    do
    {
        cout << "Enter a number > 0: ";
        cin >> num;
    } while (num <= 0);


    for (int j=num; j>0; j--)
    {
        for (int i=1; i<=j; i++)
            cout << i << " ";
        cout << endl;
    }

    cout << endl;
    return 0;
}

The second one:

#include <iostream>

using namespace std;

int main()
{
    for (int i=0; i<11; i++)
        cout << '=';
    cout << endl;

    for (i=0; i<5; i++)
    {
        for (int j=0; j<2; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
    
    for (i=0; i<11; i++)
        cout << '=';
    cout << endl;

    return 0;
}

And third:

#include <iostream>

using namespace std;

int main()
{
    int num;
    cout << "Enter a number > 0: ";
    cin >> num;

    for (int j=num; j>0; j--)
    {
        for (int i=j; i>0; i--)
            cout << "*";
        cout << endl;
    }

    cout << endl;
    return 0;
}
Comments
this is what i was looking for thank you very much
This question has already been answered. Start a new discussion instead.