943,754 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4174
  • C RSS
Sep 22nd, 2003
0

for loops

Expand Post »
i am in a intro to cs class and need help with a current project. it is a simple project but I seem to be stuck. the program is to read 2 integers for the height and width of a rectangle and then display the rectangle using "#" 's. We are to do this using a loop construct such as a for loop. my problem is that I do not know how to display the "#"'s for the width of the rectangle. can anyone help?
Last edited by lobo; Sep 22nd, 2003 at 8:43 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lobo is offline Offline
2 posts
since Sep 2003
Sep 22nd, 2003
0

Re: for loops

Awright, I don't know what 'cs' is. But here's a basic idea of what you need to do:

Get input and store in variables named 'width' and 'height'
for (temp=0;temp<width;temp++)
for (temp2=0;temp2<height;temp2++)
print out '#'

How it works is it goes across:

#####

But since theres another for loop every time it moves across one, it goes down a column:

###
###
###

For example, it'll start at the first #, right? It'll loop until it reaches the last one, but for every time it goes across, it makes a column with a second for loop.

Hopefully I've explained clearly enough.

Btw, I don't know what 'cs' is, but here's some C/C++ code, if it'll help:

  1. #include <stdio.h>
  2. int main(void) {
  3. int temp=0,temp2=0,width=0,height=0;
  4. printf("Input width and height:\n");
  5. scanf("%d%d",&width,&height);
  6. for (temp=0;temp<width;temp++)
  7. for (temp2=0;temp2<height;temp2++)
  8. putchar('#');
  9. return 0;
  10. }

That's untested, but according to all logic and reasoning available at the time of writing, it should work.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Sep 25th, 2003
0
Re: for loops
thanks, by the way I used "cs" to stand for "computer science"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lobo is offline Offline
2 posts
since Sep 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: struct dynamic 2d array alloc
Next Thread in C Forum Timeline: read ip on xp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC