LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C programming Loop question? (https://www.linuxquestions.org/questions/programming-9/c-programming-loop-question-731565/)

chibi2666 06-09-2009 12:09 AM

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.

paulsm4 06-09-2009 12:47 AM

Code:

/* "header files" (like "stdio.h") contain definitions needed by the program... */
#include <stdio.h> 

int
main (int argc, char *argv[])
{
  int h, w;  /* Input variables */
  int iret;  /* return value (for error checking) */
  int i, j;  /* Loop counters */

  /* Get input from user */
  printf ("enter a height and width: ");
  iret = scanf ("%d %d", &h, &w);
  if (iret != 2)
  {
    printf ("ILLEGAL INPUT!\n");
    return 1;
  }

  /* Use two nested "for loops" to draw our rectangle */
  for (i=0; i < h; i++)
  {
    for (j=0; j < w; j++)
    {
      printf ("#");
    }
    printf ("\n");
  }

  /* Hasta la bye-bye */
  printf ("\n\nDone.\n");
  return 0;
}

SAMPLE OUTPUT:
Quote:

enter a height and width: 2 3
###
###


Done.

chibi2666 06-11-2009 07:42 AM

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;
}

pixellany 06-11-2009 07:53 AM

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:

What would the output me?
BE?
If you mean what is the output of the code, why not run it and see?

noctilucent 06-11-2009 10:59 AM

Hello. What book are you studying [sorry for not providing help]?

chibi2666 06-16-2009 07:12 PM

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

chibi2666 06-16-2009 07:12 PM

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

Uncle_Theodore 06-16-2009 07:41 PM

Quote:

Originally Posted by chibi2666 (Post 3576469)
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

No, it will not run forever. Eventually the value of k will hit 5 and the loop will exit. If you want some output on the screen, you might want to add something like

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>
int main(){
  int i=0,j=0,k=0;
  while(((i < 5) || (j < 20)) && (k != 5)){
  if((j % 3) == 0){
    k++;
    i = i + 2;
    printf("i=%d, j=%d, k=%d\n", i, j, k);
  }
  i--;
  j++;
 }
 return 1;
}

Funny that you're returning 1. Is there any particualr reason for that? ;)

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.

graemef 06-16-2009 09:15 PM

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.