Computing.Net > Forums > Programming > FOR loops in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

FOR loops in C

Reply to Message Icon

Name: paul.browne03
Date: November 4, 2003 at 03:59:03 Pacific
OS: Win ME
CPU/Ram: 2.3GHz/512MB
Comment:

I've been trying to get a for loop to display a pattern of decreasing, left-indented characters for about 3 days now. The assignment is to write a menu-driven program to offer the choice of seeing one of three patterns:
*****
****
***
**
*
, a diamond of similar construction or a number matrix. The assignment states that a while loop must be used for the menu & for loops for each of the patterns. The menu loop was no problem, same with the number matrix, both fairly easy to do. However, I cannot wrap my head around how to display a pattern of characters like the one above. How do you get the loop to both decrement the number of stars & increment the number of spaces?. Do I use two loops or one?. Any help would be much appreciated as this is really bugging me & any & all textbooks I've consulted deal only with numeric applications of For loops



Sponsored Link
Ads by Google

Response Number 1
Name: Davour
Date: November 4, 2003 at 04:51:30 Pacific
Reply:

Please dont ask for homework answers

My advise would be to post what you have been working on and what plusses/minusses your code has and allow us to help you tweak

So instead of "What is 2+2?"
try
"So i thought 2+2 was 5 but since i did a long study and realised 2+3 is five that didnt make any sense so is there anyone who can help me?"


0

Response Number 2
Name: SN
Date: November 4, 2003 at 05:08:18 Pacific
Reply:

I can't give you the answer, but I'll give you some hints since you seem to have given the problem a good effort:

Say we're doing the pattern above, which is actually supposed to look like:

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

I assume the pattern is supposed to be arbitrarily large, so I'll use a variable (int size) to represent the number of characters in a line (in the case above, size=5.)

So let's consider a for loop that prints out 5 lines:

int size=5;
for (int i=0; i<size; i++)
{
  //print a line of the pattern
}

Now if you stare at it long enough you notice something valuable: the counter variable i represents two things: The line number we're printing out, and also the number of spaces in the line we're printing out. For example, in line 0, we print 0 spaces, in line 1 we print 1 space, and so on, all the way up to "size".

So now all you need to know is how to print one line of the pattern, given the size of the line and how many spaces you're supposed to print. You need to print i spaces, and size-i asterisks. Two more for loops, embedded in the one above, should do this quite nicely.

-SN


0

Response Number 3
Name: Infinite Recursion
Date: November 4, 2003 at 06:31:40 Pacific
Reply:

Paul,

This question has been asked numerous times. You may want to try and do a search on it here at the forum. Its been asked, at least in some form, three times this month. Try to use nested for loops: one to track the number of spaces and one to track the amount of stars.

IR


0

Response Number 4
Name: Outlander
Date: November 4, 2003 at 09:10:57 Pacific
Reply:

Why dont you "answer" his question? I hated in school when someone would say "I can't give you the answer, but I'll give you a hint" That pissed me off to no end! Might be why I kept on failing math cause I got the same teacher and he did the same crap every year. Never told me "How?" or "Why?" a problem did what it did, or where the other variables came from, oh noooo... he just hinted at the answer... :(

If he dont know the answer, hinting to something he doesnt know wont help!

Give him the answer already!


0

Response Number 5
Name: paul.browne03
Date: November 4, 2003 at 09:56:26 Pacific
Reply:

I'm so tantalisingly close to it, thanks to SN's help, but maybe if some benevolent uber-programmer would see fit as to maybe show the code for something similar, not exactly the shape I'm looking for, but similar, I could maybe reverse-engineer it enough to understand the code at work. I've managed to get it to display

*
**
***
****

using a FOR loop inside a FOR loop. That's obviously the upside-down of what I'm trying to get. The code I used is;

for (i = 4; i >= 0; i--)
{
for (j = 4;j >= i; j--)
printf ("*");
printf ("\n");
}

So, any semi God-like coder out there who may be willing to help a poor plebe like me who is only taking programming for his Astrophysics degree?.


0

Related Posts

See More



Response Number 6
Name: paul.browne03
Date: November 4, 2003 at 10:13:14 Pacific
Reply:

OK, thanks to some experimenting, ruminating & a hell of a lot of trial & error I got the thing to show right way-up, BUT left-indented instead of right-indented, like so

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

whereas it should be

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

The code is this;

for (i = 4; i >= 0; i--)
{
for (j = 0;j <= i; j++)
printf ("*");
printf ("\n");
}

The aforementioned appeal to a higher being / more experienced programmer is still in effect.....


0

Response Number 7
Name: SN
Date: November 4, 2003 at 10:31:06 Pacific
Reply:

Outlander-
Of course you hated it...Everybody does. But I'm trying to actually help him learn the material, rather than just do it for him. If I "just give him the answer", then he'll be back in two weeks with a harder problem, and eventually he'll be too far behind to catch up.

What I gave is sufficient (and then some) to solve the problem. If anything, I said too much. Either way, my response was quite a bit more helpful than yours:-) If you want to help him out, give him the answer yourself. I can't stop you.

I also can't give him the complete answer because I don't know C.

paul-
Flattery will get you nowhere. But, in the absence of money, Its probably the best I'll get so I'll take it:-)

You're getting there, but you're doing a lot more work than you have to. It's much more intuitive than that:

for (int i=0; i<5; i++)
{
  //print out i spaces
  //print out 5-i asterisks
}

Each of the // stands for a for loop...You need two of them inside the original for loop, as IR said, one for the spaces, one for the asterisks. A total of 3 for loops. One for each line, one for each line's spaces, and the third for each line's asterisks.

you'll need a different counter variable (instead of i) for the inside for loops.
for (int j=0; j<(times you want to print character) j++)
{
  print out character
}

You should be able to do this without decrementing any variables (ie i-- or j--.) It's more intuitive that way.

This question is asked reasonably often, but last time I answered it the guy double posted and so my answer got deleted, so you may not find it on the search.

-SN


0

Response Number 8
Name: paul.browne03
Date: November 4, 2003 at 11:56:03 Pacific
Reply:

Finally got it!. Thanks for all your help SN, it really made the difference. I'm going to have to pore over my code for a long time to make sure I actually have it secure in my brain & can do it again if needed but at least I can dust off this assignment. Until the next one that is...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: FOR loops in C

For Loop in C www.computing.net/answers/programming/for-loop-in-c/17835.html

FOR Loop in Win2K Script www.computing.net/answers/programming/for-loop-in-win2k-script/9942.html

Solve Linear System (Ax = b) in C++ www.computing.net/answers/programming/solve-linear-system-ax-b-in-c/12284.html