954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Printing a hollow box!

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!

nufanvandal
Newbie Poster
18 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

oops, the last box, those middle $$ are supposed to be over more...so it looks like...a blank square inside...you get the idea :)

nufanvandal
Newbie Poster
18 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

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

#include <stdio.h>
#include <conio.h>
void main()
{
	clrscr();
	int m,n,i,j;
	printf("Number of rows:");scanf("%i",&n);
	printf("Number of columns:");scanf("%i",&m);
	for (j=0;j<n;j++) printf("$$");
	printf("\n");
	i=0;
	while (i<m)
		{
		 printf("$$");
		 for (j=1;j<n-1;j++) printf("  ");
		 printf("$$\n");
		 i++;
		}
	for (j=0;j<n;j++) printf("$$");
}


Hope it helps! :)
oh something else. you know you could just use the EDIT button if you've written your message wrong :idea:

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 

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");
}
red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

Hey, thanks a lot guys, I really appreciate your help! :cool:

nufanvandal
Newbie Poster
18 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

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 :!:

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 

Greetings.
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:

$
        $
        $
        $

Then, putting it altogether (4x3), 3 columns will give:

$$$$$$
        $$  $$
        $$  $$
        $$$$$$

This is what I get from my understanding. ;)

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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
:)

nufanvandal
Newbie Poster
18 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

Greetings.
Okie, so was that what you wanted? ;)

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

yep exactly right! thanks again!! :)

nufanvandal
Newbie Poster
18 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

Greetings.
No problem ;)

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You