Create a function that takes two integers as parameters (width and height in tha
ID: 3729067 • Letter: C
Question
Create a function that takes two integers as parameters (width and height in that order) and prints a rectangle of pluses and dashes as shown below to the screen using only WHILE loops. Do not use for loops. If a value less than 1 is passed as an argument, the function should not print anything. The rectangle should be printed with only dashes (minus), plus signs, and newlines. Each line should end with a newline. There should be no spaces or extra characters anywhere.
For example: printRectangleWhile(10, 6); should print a rectangle with 10 columns and six rows as shown below.
This function should be executed be another file like this:
#include
//Sample main.cc
void printRectangleWhile(int, int);
int main() {
printRectangleWhile(10, 6);
}
Explanation / Answer
#include <stdio.h>
void printRectangleWhile(int, int);
int main()
{
printRectangleWhile(10, 6);
return 0;
}
void printRectangleWhile(int max_col,int max_row)
{
int rowvar=0,colvar=0,sign_var=0;
while(rowvar<max_row)
{
colvar=0;
while(colvar<max_col)
{
if(sign_var==0)
{
printf("+");
sign_var=1;
}
else
{
printf("-");
sign_var=0;
}
colvar++;
}
printf(" ");
rowvar++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.