Closed Thread Icon

Preserved Topic: C++ help Pages that link to <a href="https://ozoneasylum.com/backlink?for=20594" title="Pages that link to Preserved Topic: C++ help" rel="nofollow" >Preserved Topic: C++ help\

 
Author Thread
Babamba
Paranoid (IV) Inmate

From: my mother
Insane since: Aug 2000

posted posted 02-01-2001 22:04

I am just starting to learn C++, and I'd like to know how to make a program using for loops that asks for a number, and then outputs X's in a triangle thing. For example, if a user inputs 5, I want it to look like this:

X
XX
XXX
XXXX
XXXXX

thanks, aaron

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 02-01-2001 22:17

That would be a nested for loop:

for (int i = 1;i<=user.input;i++){
for (int j = 0; j< i;j++) {
System.out.print("*");
}
System.out.println("");
}

That is java, I know the syntax for the system print is different, but I am sure you know how to do that.

So that should get you to it, the for statements should be the same in C++ as in Java, I am just not in the mood to brush up on my C++ skills.

-mage-

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 02-01-2001 22:25

Thinking again I should tell you how it works.

The user enters an input, say 5 again for consistancy.

The first for loop is counting the lines, notice the println statement at the end.
The nested loop is printing the data.

In the first loop we use from 1 until it equals the user input,
This is because we will ne to have something that the nested for loop can use.

Normally you would write For (int i=0;i<user.input;i++) because that works better for indexing arrays.

But in this case 1 is required becuase you want the nested for loop to run, on the first time through.

If it was written:

for (int i = 0;i<user.input;i++){
for (int j = 0; j< i;j++) {
System.out.print("*");
}
System.out.println("");
}

You would not initiate the nested loop the first run through, but using 1 and <= user.input you get the same number of runs, which in this case would be 5, but the nested loop has something to work with.

On the first run the nested loop is passed 1 as its value and it runs 1 time printing a single * because it is incemented to 1 after the run and terminates, and the orrigional for loop prints a line return and increments.

On the second run the nested loop is passed 2 as its value, then is initialized to 0 again and prints a * passes back 1 prints another * and then terminates.

See how it works.

*
**
***

and so on and so forth until it reaches the users defined block.

You will probabally also want input some exception handling in there, so to prevent anyting <= 0 from being entered.

-mage-

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 02-02-2001 00:48

Well, warmage gave a really good explanation so I don't have much to say. Here's the code in c++ :

cout << "enter input: ";
int user_input;
cin >> user_input;

for (int i = 1; i <= user_input; i++)
{
for (int j = 0; j < i; j++)
{
cout << "X";
}
cout << endl;
}

:edit:
By the way, you'll eventually learn recursion to do the same thing. Let me know if you need anymore help.
:edit:



[This message has been edited by silence (edited 02-02-2001).]

galaxal
Paranoid (IV) Inmate

From:
Insane since: Oct 2000

posted posted 02-04-2001 09:57

I don't think it's any point to use recursion, or maybe I am not smart enough to come up with a good recursion that can be shorter then a for loop.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-04-2001 10:57

Galaxal, one of the examples where recursion is good is when you don't know the final number of elements (so, for loop would be useless)...

warjournal
Maniac (V) Mad Scientist

From:
Insane since: Aug 2000

posted posted 02-04-2001 13:41

Ahh... recursion. The Nemesis has returned! I have seen too many people lock-up because they don't understand recursioin. Used to happen *all* the time in my Pascal class when we were learning about nodes.

That reminds me. I have a programming questions to ask. I'll post it seperately.

ratdoodoo
Bipolar (III) Inmate

From: where the snow falls like rain.
Insane since: Dec 2000

posted posted 02-04-2001 18:37

You guys,

He wants it to output in a triangle. The Board won't let him space the x's into the proper shape.

Wouldn't the code look more like this:

int num;
cout << "Enter number: ";
cin >> num;

for (int x = 1; x <= num; x++)
{
cout << setw(num - x);
for (int y = 0; y < i; y++)
{
cout << "X";
}
cout << endl;
}

I'm a bit rusty, but I think that setw is the function that sets the horizontal location of the output on the line. Perhaps my mop and I don't know our C++.

-rat

[This message has been edited by ratdoodoo (edited 02-04-2001).]

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 02-04-2001 21:23

Yeah, setw() will probably do the job, but if you're just learning, do it the hard way so that you can really learn your for loops.

Here's a modified version of the code that will print out the x's in a triangle form:

cout << "enter input: ";
int user_input;
cin >> user_input;

for (int i = 1; i <= user_input; i++)
{

for(int k = user_input - i; i > 0; i--)
cout << " "; // one space

for (int j = 0; j < i; j++)
{
cout << "X";
}
cout << endl;
}

And max is right, recursion is good when you don't know the number of elements. Also, when you're just learning, they make you use several different techniques do things so that you learn the concepts and techniques. You don't have to start worrying about code efficiency until you become a real code jockey.

ratdoodoo
Bipolar (III) Inmate

From: where the snow falls like rain.
Insane since: Dec 2000

posted posted 02-04-2001 23:19

FYI, that's the way I learned it.

-rat

Babamba
Paranoid (IV) Inmate

From: my mother
Insane since: Aug 2000

posted posted 02-05-2001 00:03

thanks for the help guys

Spam is yummy
~babamba

« BackwardsOnwards »

Show Forum Drop Down Menu