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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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?
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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]
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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!
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
I dont see what you mean by reversing them
Reversing them, ie. for (int index=1; index<=9; index++) .
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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 7Notice 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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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;
}
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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).
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Just replace the y in cout with your character.
char mychar = '*' ; // this is how we declare and initialize characters
// your loops here
cout << mychar ;
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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;
}
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
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;
}
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
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;
}
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2