Please use C for coding 4.2 Remember our famous homework#1 program? This program
ID: 3814835 • Letter: P
Question
Please use C for coding
4.2 Remember our famous homework#1 program? This program draws a box 20 characters in width and 10 characters in height, using a for horizontal line and l' for vertical line. printf ("Using brutal force In printf ("--------------------In"); printf In") printf I In"); printf(" In"); printf( In") printf I "); In"); printf( printf(" In") printf I '') printf("---------------- ---- "); Only println0 is used for this purpose. A good solution could involve a loop, which now you know. So, write the solution using loop Hint: One way to solve this would be by writing a loop to draw top line and nested loops to draw the middle lines and later similar loop like top for the bottom line.Explanation / Answer
4.2)
#include <stdio.h>
int main(){
int width = 10;
int height = 10;
int i ;
int j;
i =0;
while (i < width ){
putchar ( '-' );
i++;
}
putchar ( ' ' );
i = 1;
while( i < height ) {
j=1;
putchar ( '|' );
while ( j < width - 1){
putchar ( ' ' );
j++;
}
printf ( "%c ", '|' );
i++;
}
i =0;
while (i < width ){
putchar ( '-' );
i++;
}
putchar ( ' ' );
}
4.3)
#include <stdio.h>
int main(){
int width = 10;
int height = 10;
int i ;
int j;
i =0;
do{
putchar ( '-' );
i++;
}
while (i < width );
putchar ( ' ' );
i = 1;
do {
j=1;
putchar ( '|' );
do {
putchar ( ' ' );
j++;
}while( j < width - 1);
printf ( "%c ", '|' );
i++;
}while( i < height ) ;
i =0;
do{
putchar ( '-' );
i++;
}
while (i < width );
putchar ( ' ' );
}
4.4)
#include <stdio.h>
int main(){
int width = 10;
int height = 10;
int i ;
int j;
for ( i = 0; i < width; i++ )
putchar ( '-' );
putchar ( ' ' );
for ( i = 1; i < height; i++ ) {
putchar ( '|' );
for ( j = 1; j < width - 1; j++ )
putchar ( ' ' );
printf ( "%c ", '|' );
}
for ( i = 0; i < width; i++ )
putchar ( '-' );
putchar ( ' ' );
}
4.5)
#include <stdio.h>
int main(){
int width ;
int height;
char horizontalChar;
char verticalChar;
int i ;
int j;
printf("Please enter width ");
scanf("%d",&width);
printf("Please enter height ");
scanf("%d",&height);
printf("Please enter horizontal char ");
//horizontalChar =getchar();
scanf(" %c",&horizontalChar);
printf("Please enter vertical char ");
//verticalChar =getc();
scanf(" %c",&verticalChar);
for ( i = 0; i < width; i++ )
putchar ( horizontalChar);
putchar ( ' ' );
for ( i = 1; i < height; i++ ) {
putchar (verticalChar );
for ( j = 1; j < width - 1; j++ )
putchar ( ' ' );
printf ( "%c ", verticalChar );
}
for ( i = 0; i < width; i++ )
putchar ( horizontalChar);
putchar ( ' ' );
}
4.6)
#include <stdio.h>
int main(){
int width ;
int height;
char horizontalChar;
char verticalChar;
int i ;
int j;
char userInput;
while(1){
printf("Enter Y to draw Square , N to Exit ");
scanf(" %c",&userInput);
if (userInput =='N'){
break;
}
printf("Please enter width ");
scanf("%d",&width);
printf("Please enter height ");
scanf("%d",&height);
printf("Please enter horizontal char ");
//horizontalChar =getchar();
scanf(" %c",&horizontalChar);
printf("Please enter vertical char ");
//verticalChar =getc();
scanf(" %c",&verticalChar);
for ( i = 0; i < width; i++ )
putchar ( horizontalChar);
putchar ( ' ' );
for ( i = 1; i < height; i++ ) {
putchar (verticalChar );
for ( j = 1; j < width - 1; j++ )
putchar ( ' ' );
printf ( "%c ", verticalChar );
}
for ( i = 0; i < width; i++ )
putchar ( horizontalChar);
putchar ( ' ' );
}
}