2

I need to create a hollow rectangle but I'm allowed to use only one loop. The program works as-is, but I used two loops in my code and don't know how to continue reducing the last one. (We have learnt only printf, scanf, if/else and loops, so no arrays etc.) The program scans height, width and thickness of the frame.

Could anyone possibly point me the right way?

the code looks like:

row = 0;
while(row < height)
{
    column = 0;
    while(column < width)
    {
        if(thickness > row)    // upper border
            { printf("*");};
        if( some conditions )  // left border
            { printf("*");};
        if( conditions )    // hollow
            { printf(" ");};
        if( conditions )   // right border
            { printf("*");};
        if( conditions )     // bottom border
            { printf("*");};

        column++;
    };

puts("");
row++;
};
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 1
    Why the extra semicolon after each `}` of `if`? – haccks Oct 07 '13 at 20:25
  • @haccks, they're just there, not needed by the language, I think he just put them in. – Rivasa Oct 07 '13 at 20:26
  • 1
    any given mxn "grid" can be thought of as a single string that is mxn, since you are using printf I would suggest thinking of each row as a chunk of string. The first and last part of your string will be all "*" while the middle part will mostly be " "... so really you could just use 2 strings "*****" and "* *" and check if it is the 1st/last row. – technosaurus Oct 07 '13 at 20:29
  • By the way, if you will post your full code then it will help us to answer. – haccks Oct 07 '13 at 20:29
  • Can you use logical operators like AND, OR? – zubergu Oct 07 '13 at 20:30
  • @zubergu : yes, i can – wintersundayy Oct 07 '13 at 20:32
  • @haccks: the extra semicolon... well, you're right, there's no need for them to be there, i'll delete them. and for the full code; thanks, i will remember to post my whole code if asking next time. :) – wintersundayy Oct 07 '13 at 20:38
  • The 0...(n*m) loop for n rows and m columns is probably the most right one. If you wanted to be cheeky, though, you could use [the `goto` statement](http://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c) instead. – CmdrMoozy Oct 07 '13 at 20:40
  • @user2856042 - don't let them goad you into removing the curlies, they hurt nothing and can save you later if you want to if (true) do_something();do_something_else() ... just format it better like if (1){ `new lines with indented code` then } on another new line with same indentation as the if ... many code style guides _require_ all if/else to have curlies – technosaurus Oct 07 '13 at 20:50

3 Answers3

9

Here's a clue : Doing a 0...n loop inside a 0...m loop is the same as doing a 0...(n*m) loop. You can compute the row and column using division and modulo.

Geoffrey Huck
  • 211
  • 1
  • 6
1

Read it only if you are stuck completely or want to see different solution.
As you can see, there's no scanning for input.

#include <stdio.h>

int main(void)
{
  int width=5;
  int height=6;
  int thick=1;
  int x=1;
  int y=height;

  while(y>0)
  {
    if(y>(height-thick) || y<=thick || x<=(thick) || x>(width-thick))
      printf("*");
    else
      printf(" ");
    if(x==width)
    {
      x=1;
      printf("\n");
      y--;
    }
    else
    {
      x++;
    }
  }
  return 0;
}
zubergu
  • 3,646
  • 3
  • 25
  • 38
0

With the below code you can print frame By using 1loop+ if-else and Number of iterations are 2*column+width-2

    int i, column = 6,width=5;

        for(i=1;i<=2*column+(width-2);i++) 
        {
               if( i <= column || i-column>=width-1) 
               printf("* ");
               else    
               printf("\n*%*s\n",2*(column-1),"*");  // prints newline and `*` then Width of 2*(colomn-1) times space and  again * and newline.  
               //if you don't want newline two times, remove trailing one add if statement inside else check i==column+width-2 print newline. 
        };  

Generalized.

#include <stdio.h>

int main(void) {
int i, column ,width;
printf("Enter two");
scanf("%d%d",&column,&width);
    for(i=1;i<=2*column+(width-2);i++)
    {
           if(i <= column || i-column>=width-1)
           printf("*");
           else
            {
            printf("\n*%*s",(column-1),"*");
            if (i-column==width-2)
            printf("\n");
            }
    };

    printf("\n");
        return 0;
}
Gangadhar
  • 10,248
  • 3
  • 31
  • 50