8-2 Please do the coding in C language! Demo: Here is the solution for Homework#
ID: 3814130 • Letter: 8
Question
8-2
Please do the coding in C language!
Demo: Here is the solution for Homework# 4.6. It is written as a procedural program. int counter 1. counter 2 char hChar1 vChar1 int 1, wd1 char answer y'; print f("In Using for -loop and user values continuously "); while (answer y') printf("VnPlease enter height of a box: scanf("%d &ht1;); printf("VnPlease enter width of a box: scanf("%d &wd1;); //you first need to flush the buffer, which still has In' character due to pressing enter while (getchar In printf ("InPlease enter the vertical charcters to draw box scanf ("%c", &vChar1;); //don't forget to remove the new line character after //reading just a charcter in above code while (getchar() printf("InPlease enter the horizontal charcters to draw box: scanf ("%c", &hChar1;); printf ("In"); for (counter1 1; counter 1 Wd1 counter 1++) printf( "%c hChar1); printf ("In"); for (counter 1; counter 1 ht1 2; counter 1++)Explanation / Answer
#include <stdio.h>
// A.
void drawHorizontalLine(int wd1, char hChar)
{
int counter1;
for(counter1 = 1; counter1 <= wd1; counter1++)
{
printf("%c",hChar);
}
}
// B.
void drawVerticalLine(int ht, int wd, char vChar)
{
int counter1, counter2;
for(counter1 = 1; counter1 <= ht-2; counter1++)
{
printf("%c",vChar);
for(counter2 = 1; counter2 <= wd-2; counter2++)
printf(" ");
printf("%c ",vChar);
}
}
// C.
void drawBox(int ht, int wd, char vChar, char hChar)
{
printf(" ");
drawHorizontalLine(wd, hChar);
printf(" ");
drawVerticalLine(ht, wd, vChar);
drawHorizontalLine(wd, hChar);
printf(" ");
}
int main(int argc, char const *argv[])
{
char hChar1, vChar1;
int ht1, wd1;
char answer = 'y';
printf(" Using for-loop and user values, continuosly: ");
while( answer == 'y' )
{
printf(" Please enter height of box: ");
scanf("%d", &ht1);
printf(" Please enter width of box: ");
scanf("%d", &wd1);
while(getchar() != ' ');
printf(" Please enter the vertical characters to draw box: ");
scanf("%c", &vChar1);
while(getchar() != ' ');
printf(" Please enter the horizontal characters to draw box: ");
scanf("%c", &hChar1);
// D.
drawBox(ht1, wd1, vChar1, hChar1);
while(getchar() != ' ');
printf("Continue? Type y for yes: ");
scanf("%c",&answer);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.