943,431 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5879
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 4th, 2004
0

Printing a hollow box!

Expand Post »
hello,
im pretty new to C and for my first project, I have to print out information about a box.(exciting...) anyway, i have the user enter the width and height of the 2d box, and then give them a menu to select from, to get info about the box, perimeter, area and so on..., one of the menu options is to print out a picture of the hollow box...I'm having trouble with the logic behind this..of how to perform this action. I can print out a regular box
ie.

Selection: R
A solid Rectangle with 4 rows and 3 collumns
$$$$$$
$$$$$$
$$$$$$
$$$$$$

2 ASCII characters makes up one collumn..
but the problem im having is making it look like this...

A solid Rectangle with 4 rows and 3 collumns
$$$$$$
$$ $$
$$ $$
$$$$$$

I'm not necessarily asking for the code, but maybe the logic of behind how i could do this :cheesy:

Thanks!
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Jul 4th, 2004
0

Re: Printing a hollow box!

oops, the last box, those middle $$ are supposed to be over more...so it looks like...a blank square inside...you get the idea
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Jul 4th, 2004
0

Re: Printing a hollow box!

ok i've written some code for you. it reads the number of rows and columns and prints the drawing.

  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int m,n,i,j;
  7. printf("Number of rows:");scanf("%i",&n);
  8. printf("Number of columns:");scanf("%i",&m);
  9. for (j=0;j<n;j++) printf("$$");
  10. printf("\n");
  11. i=0;
  12. while (i<m)
  13. {
  14. printf("$$");
  15. for (j=1;j<n-1;j++) printf(" ");
  16. printf("$$\n");
  17. i++;
  18. }
  19. for (j=0;j<n;j++) printf("$$");
  20. }

Hope it helps!
oh something else. you know you could just use the EDIT button if you've written your message wrong
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jul 4th, 2004
0

Re: Printing a hollow box!

Greetings.
I have checked your codes. Nice work.
Found some little errors. Maybe you've mixed up rows & columns?

#include <stdio.h>
#include <conio.h>

void main()
{
     int m,n,i,j;
     printf("Number of rows:");
     scanf("%i",&n);
     printf("Number of columns:");
     scanf("%i",&m);

     for (j=0; j<m; j++)  
          printf("$$");
     printf("\n");
     i=0;
     while (i<n-2)
     {
          printf("$$");
          for (j=0; j<m-2; j++) 
               printf("  ");
          printf("$$\n");
          i++;
     }
     for (j=0;j<m;j++) 
          printf("$$");
     printf("\n");
}
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 5th, 2004
0

Re: Printing a hollow box!

Hey, thanks a lot guys, I really appreciate your help!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Jul 5th, 2004
0

Re: Printing a hollow box!

Quote originally posted by red_evolve ...
Greetings.
I have checked your codes. Nice work.
Found some little errors. Maybe you've mixed up rows & columns?

#include <stdio.h>
#include <conio.h>

void main()
{
     int m,n,i,j;
     printf("Number of rows:");
     scanf("%i",&n);
     printf("Number of columns:");
     scanf("%i",&m);

     for (j=0; j<m; j++)  
          printf("$$");
     printf("\n");
     i=0;
     while (i<n-2)
     {
          printf("$$");
          for (j=0; j<m-2; j++) 
               printf("  ");
          printf("$$\n");
          i++;
     }
     for (j=0;j<m;j++) 
          printf("$$");
     printf("\n");
}
Are you sure?
You will print "number of columns" times "$$". In nufanvandal's first post, I understood that a column is made up of a "$$" and a line is made up of a "$$", "number of columns-2--minus 2 "$$-> the first and last columns". Tell me if I'm wrong :!:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jul 5th, 2004
0

Re: Printing a hollow box!

Greetings.
Quote ...
Are you sure?
You will print "number of columns" times "$$". In nufanvandal's first post, I understood that a column is made up of a "$$" and a line is made up of a "$$", "number of columns-2--minus 2 "$$-> the first and last columns". Tell me if I'm wrong
Yeah, take a look at nuvandal's first post. Have a look at how he drew the output.
Initially, 4 rows will give:
  1. $
  2. $
  3. $
  4. $
Then, putting it altogether (4x3), 3 columns will give:
  1. $$$$$$
  2. $$ $$
  3. $$ $$
  4. $$$$$$
This is what I get from my understanding.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 5th, 2004
0

Re: Printing a hollow box!

howdy,

so yeah,

One collumn is made up of two ASCII characters because printed ASCII characters are approximately twice as tall as they are wide, so to make it look more nice, and like a box, one collumn is made up of 2 characters
ie:

##
##
##
##

while one row is made up of only a single ASCII character


better example of output, each collumn is a different color

########< --1st row
########< --2nd row
########< --3rd row

so the above would be a rectangle with 4 collumns, and 3 rows
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Jul 5th, 2004
0

Re: Printing a hollow box!

Greetings.
Okie, so was that what you wanted?
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 6th, 2004
0

Re: Printing a hollow box!

yep exactly right! thanks again!!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004

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: Are You Up For This Project
Next Thread in C Forum Timeline: questions &solutions





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


Follow us on Twitter


© 2011 DaniWeb® LLC