C programming Loop question?
HI,
I am doing self 'C' study from a book I bought. I have been trying to figure out how to write this into code. Write a program that has the user input a height and width. Then it draws a rectangle made of #'s. For example Enter a height: 3 Enter a width: 5 ##### ##### ##### Could anybody please show me how this is done. Thanks much. |
Code:
/* "header files" (like "stdio.h") contain definitions needed by the program... */ Quote:
|
One more question
HI,
Thanks for the help. Could you help me with this one What is the value of J++ I think J++ means plus one. What would the output me? #include <stdio.h> int main(){ int i=0,j=0,k=0; while(((i < 5) || (j < 20)) && (k != 5)){ if((j % 3) == 0){ k++; i = i + 2; } i--; j++; } return 1; } |
Please put your code samples in <code> tags---it keeps the formatting and makes it much easier to read.....
"j++" does not have a value. The expression means "increment j after using it". "++j" means "increment j before using it". Quote:
If you mean what is the output of the code, why not run it and see? |
Hello. What book are you studying [sorry for not providing help]?
|
What is the value of j++ after running the program
HI,
After I ran the program I get nothing. Does that mean it will just keep running forever. j++ #include <stdio.h> int main(){ int i=0,j=0,k=0; while(((i < 5) || (j < 20)) && (k != 5)){ if((j % 3) == 0){ k++; i = i + 2; } i--; j++; } return 1; } Any help would be geat |
What is the value of j++ after running the program
HI,
After I ran the program I get nothing. Does that mean it will just keep running forever. j++ #include <stdio.h> int main(){ int i=0,j=0,k=0; while(((i < 5) || (j < 20)) && (k != 5)){ if((j % 3) == 0){ k++; i = i + 2; } i--; j++; } return 1; } Any help would be great |
Quote:
printf("i=%d, j=%d, k=%d\n", i, j, k); right after the line "i = i + 2". It also helps if you include your code into the code tags, it will be more readable, like this: Code:
#include <stdio.h> Another thing. Try reading your code as if it were just plain text. It's a good way to understand what the program is doing. In your case it will look like this: Make i, j and k zero, then, while the condition "(i is less than 5 or j is less than 20) and k is not five" is true, do the following: if j is divisible by three, increment k and add 2 to i. subtract 1 from i increment j by one. Good luck. |
Just to add to what Uncle_Theodore said about reading the code a good self-test would be to decide what the values of i, j and k would be each time the code prints out the values, and what the final values will be - before you actually run the code. Were you correct? If not why were you wrong? So long as you understand those questions you will have learnt something from the code.
|
All times are GMT -5. The time now is 03:45 AM. |