By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Loop Help!

P: 1
I'm a C++ noob and I'm having trouble with:

Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j .

I started a little code for it:
n = 0;
while (n > j) {
printf ("*");
n = n + 1;
}

I'm probably doing a million things wrong but I need some help. |< Please help!
Feb 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
it should be n < j
if n=0 , n would never be >(greater than) j which is suppossed to be the # of asteriks printed, otherwise it works
Feb 24 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
I'm a C++ noob and I'm having trouble with:

Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j .

I started a little code for it:
n = 0;
while (n > j) {
printf ("*");
n = n + 1;
}

I'm probably doing a million things wrong but I need some help. |< Please help!
You want "n" asterixes, so you want it to print 'n' times. This means you can create an algorithm like this:

print n(int) asterixes

And then break it down

for each n
print an asterix

You can assume your 'n' is set - by your requirements - you have to match that each time. (There is more to the program than the algorithm above - it's just an example of how to break it down from the original sentence...)

How do you think you would keep track of how many n asterixes you have printed?
Feb 24 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.