Purpose: More practice with control structures. For this lab, you will write a s
ID: 3799421 • Letter: P
Question
Purpose: More practice with control structures. For this lab, you will write a small program that prints a few "shapes" using asterisks. Your program will prompt the user to select a shape by entering a number (square or triangle for B grade, add the zigzag for the A grade). It will then prompt for the size, and then if a square or triangle is selected it will prompt for "not filled" or "filled", and if a zig-zag is selected it will prompt for the number of repetitions. (See back for an example run). After printing a shape, your program will loop back and prompt again for another. One of the "shape selections" will cause the program to exit. For pre-lab. draw a flowchart of your code and make a serious attempt at writing it. Bring this to lab. It will be checked at the beginning of lab and will be a part of your total lab grade. Have your code behave exactly (and format exactly) as shown below for the sample run. Suggestion: work on how to draw a square first. Use "nested for loops" for this (one inside the other). For the unfilled square, substitute spaces for the asterisks on the "inside" (how do you know if a given location is "inside" the square?). The triangle code can be similar, but with a few small changes (mine had only two small changes).Explanation / Answer
the example run has not given so the required input/output may vary, but the way of doing this shall be the same :)
-------------
#include<stdio.h>
int main(void)
{
int ch;
//first let the user select which shape he wants to print
printf("Press 1 for zig-zag ",);
printf("Press 2 for square ",);
printf("Press 3 for rect ",);
scanf("%d",&ch);
//now if zig-zag is selected
if(ch==1)
{
printf("enter num of repetitions ");
int rep;
scanf("%d",rep);
for(int i=0;i<rep;i++)
{
//we will draw the zig-zag here
//and it will be repeated rep times
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
{
int modCol = (col % modulusVal);
if (modCol >= numRows)
{
modCol -= numRows;
modCol = ((numRows - 1) - (modCol + 1));
}
modCol = ((numRows - 1) - modCol);
if (modCol == row)
{
printf("*");
}
else
{
printf(" ");
}
}
printf(" ");
}
}
}
//if the user selected traingle
else if(ch==2)
{
//size of the triangle
int n=5;
printf("Press 1 for filled ");
printf("Press 2 for not filled ");
int x;
scanf("%d",&x);
//if filled
if(x==1)
{
for(i=1; i<=n; i++)
{
//Prints trailing spaces
for(j=i; j<n; j++)
{
printf(" ");
}
//Prints hollow pyramid
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf(" ");
}
}
//if unfilled
else if(x==2)
{
for(i=1; i<=n; i++)
{
//Prints trailing spaces
for(j=i; j<n; j++)
{
printf(" ");
}
//Prints hollow pyramid
for(j=1; j<=(2*i-1); j++)
{
if(i==n || j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf(" ");
}
}
}
}
--------------------------------------
i have done the zig-zag and the traingle ,the rect will be the same as the trainlge (just consider the different co ordinates)
-------------
thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.