LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-09-2009, 01:09 AM   #1
chibi2666
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
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.
 
Old 06-09-2009, 01:47 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by paulsm4; 06-09-2009 at 01:50 AM.
 
Old 06-11-2009, 08:42 AM   #3
chibi2666
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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;
}
 
Old 06-11-2009, 08:53 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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?
 
Old 06-11-2009, 11:59 AM   #5
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 25
Hello. What book are you studying [sorry for not providing help]?
 
Old 06-16-2009, 08:12 PM   #6
chibi2666
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 06-16-2009, 08:12 PM   #7
chibi2666
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 06-16-2009, 08:41 PM   #8
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
Quote:
Originally Posted by chibi2666 View Post
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.
 
Old 06-16-2009, 10:15 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash programming while loop Histamine Programming 3 10-29-2007 07:35 PM
while loop question IceOner Programming 6 10-26-2007 09:23 AM
vbs loop question jonlake Programming 3 10-05-2006 06:32 PM


All times are GMT -5. The time now is 05:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration