d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Help > For Loops
Add Reply New Topic New Poll
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Oct 2 2013 04:12pm
Goals: Developing problem-solving skills, using nested loops

Problem: You are to create a program that will ask the user to enter a number, then uses a nested for loop that will produce output similar to that shown below.

User entered 5 User entered 6 User entered 8 User entered 4
5***** 6****** 8******** 4****
4**** 5***** 7******* 3***
3*** 4**** 6****** 2**
2** 3*** 5***** 1*
1* 2** 4****
1* 3***
2**
1*

Your program should confirm that the user enters a positive number. Aside from the validation, your program should only contain 4 cout statements: one that asks the user for a positive integer, one that displays the numbers, one that displays a single asterisk, and one that will change lines.


My question is: How do I make columns? I'm kind of lost here.

Here's my code so far:


#include <iostream>

using namespace std;

int main()
{
int sum;

cout << " Enter a value \n" << endl;
cin >> sum;

for(int i = sum; i > 0; i = i - 1){

cout << i << endl;

for(int i = sum;)


}



}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 2 2013 04:17pm
just cout some spaces (or asterisks?)
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Oct 2 2013 06:43pm
use setfill and setw
Member
Posts: 1,087
Joined: May 2 2005
Gold: 75.00
Oct 2 2013 09:44pm
There are a few ways I thought about approaching the problem as soon as I read it.

get input is obviously a first step you can use an unsigned
or various other methods to determine if positive

you need a loop that tests if there is additional input

you need an inner loop that uses the input value taken in during first step
to determine how long the loop shall run for the stars.

Have an output inside the inner loop that prints the stars

you need either some form of a counter that is reduced increments to reduce the
amount of stars or you can use various other methods to test the duration of the
output against the previous for duration


I'm not going to tell you where each item needs to specifically go nor will I write
the code but I will state that if you have an outer loop and an inner loop you can
place a cout << endl or a cout << "\n" to go to the new line which creates the columns
you seek.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll