Our instructor asked us to create this using C language(While Loop):
*
**
***
****
*****
and the reverse of that.
I can't install turbo C right now so I can't test if what I thought is right:
int i;
while(i=1; i%26lt;=5; i++)
printf("%d \n", i);
printf("*");
-Am I correct? Can somebody please explain?
C language help?
Okay, very good first try you are getting there. Let's re-read that code you wrote.
int i; /one variable
while(1 , 5, ++) / looping from 1 to 5 once
printf("%d \n", i); / error!!
Plus this first printf does not make that much sense actually.
Your output if it worked would be:
1
*2
*3
*4
*5
*
Which is not gonna get you a very good grade I think.
You need to just sit back and think about the problem without any C code.
Step one: What is the computer doing?
Answer: Printing * a different number of times 5 times.
So what does this mean? It means we need to have two variables. Let us give the variables real names as well, not just i. Let's call them line and asterisk.
So what do we need to do for the first line? Print one star.
Then print 2 stars. and so on.
Final version would look something like this:
int line, asterisk;
while (line=1; line%26lt;=5; line++) {
while (asterisk=1; asterisk %26lt;= line; asterisk++) {
FOR YOU TO DECIDE
}
}
Okay, so that is the basic control structure that you will be using, now you just need to get C up and running, and decide what goes in the "FOR YOU TO DECIDE" spot. Didn't want to do your homework for ya.
Also, if you need it Eclipse might be the IDE you want.
http://www.eclipse.org/
Reply:You're way off.
Your got the "for" loop syntax muddled with the "while" loop syntax. What you've written above will not compile.
replace the 'while' label with a 'for'. After that you will still not get what your instructor is looking for. You will simply be printing out a number and the * in the next line for each number. Get rid of the print for variable i and bring in a while loop. Initialize another variable and compare against the value for i and print a * till the new variable = i (since it will be incremented in each loop)
Heres the pseudo code
init two variables: i and j
for ( i=1; i%26lt;=5; ++i)
set j = i
for ( j = 0; j != i ; ++j)
print your *
print statement for an empty line with just a "\n" so that the next set of *s print in the next line as required by your instructor.
Dont want to write more than that.
Reply:Something like this might work:
for (int i=0; i%26lt;=5; i++){
for (int a=0; a%26lt;i; a++)
printf("*");
printf("\n");
}
Reply:there are a ton of ways to do this here's one
#include %26lt;stdio.h%26gt;
main()
{
int i= 0;
while (i %26lt; 5)
{
int j = 0;
while( j %26lt;= i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment