RayofCommand
RayofCommand

Reputation: 4244

programming a rectangle in C with a frame of other characters

I am currently learning C and I am stuck at a task to create a rectangle filled with characters.

The variables are the width and the heights for the inside rectangle

height: 2 width : 3

This would result in:

AAAAA
ABBBA
ABBBA
AAAAA

My idea was to printf the first line by adding 2*A to the normal width The second line would be like B + width*A + B in a while loop until I reach the integer given in heigth. After that again as in line one adding 2*A to the given width displayed in A's.

So I started coding the line one and thought I can add strings easily like

#include <stdio.h>
#include <stdlib.h>

int main () {

int i = 0

while (i<width+2)
printf ("A\n")

printf ("A\n") +  {{while (i<width)  { printf ("B\n");
i++;}} + ("A\n")}
}

But I was not able to concettinate the strings correctly, my A's are displayed like that:

A
A
A
A
A

Can someone give me a rough Idea how to make it properly? I would like to keep the idea of doing it, if possible. I know that you can do it with coordinates, but I didn't like it.

Upvotes: 0

Views: 2818

Answers (2)

Ediolot
Ediolot

Reputation: 521

This is what I will do

First, you need to declare your variables, am using i and j for the loops:

int i, j;

Or you can just declare them in diferent lines if that's cleaner for you:

int i;
int j;

Now, you will need a loop to print the first line. You where doing something like printing "A\n" but the "\n" means a new line so you will just have to use printf("A");. Add 2 to the widht so you can go though all the first line.

    for (i=0; i<(widht+2); i++)
        printf("A");

Now, the middle lines, for this, you will need to iterate in all the middle lines using, in this case, the variable j. On each iteration, you will have to print one "A" at the start and one at the end. The first one have a "\n" so every time you start a line, it will be printed "in a new line". The inner loop does the same as the previous one but with Bs. In this case we dont need to add 2 because we are not in a frame.

    for (j=0; j<height; j++)
    {
        printf("\nA");
        for (int i=0; i<(widht); i++)
            printf("B");
        printf("A");
    }

The last for is like the first, but with an added printf("\n"); to jump into a new line.

    printf("\n");
    for (int i=0; i<(widht+2); i++)
        printf("A");

There is other way to do this.

Use two for to go along all the square this way:

for (j=0; j<(height+2); j++)
{
    for (i=0; i<(widht+2); i++)
    {
        if ( ((i==0) || (i==widht+1)) && ((j==0) || (j==height+1)) 
            printf("B");
        else
            printf("A");
    }
    printf("\n");
}

This second way will iterate line per line, and if we are not in the frame, it will print an B, if we are in the frame, it will print an A

Upvotes: 1

user2371524
user2371524

Reputation:

printf ("A\n") +  {{while (i<width)  { printf ("B\n");
i++;}} + ("A\n")}

This is not valid C syntax. What do you think the + here will do?

Also, \n is the encoding for a newline in C string literals. So, printing a newline will do exactly that, it will print a newline ... nothing to wonder about!

For starters, don't include \n in your output if you don't want a newline. Then, there are better output functions for strings (puts(), fputs()) and single characters (putchar(), fputc()) if you don't need any formatting -- printf() is for formatted output.

That being said, use some loops and output single characters.

Upvotes: 1

Related Questions