Write a C program that reads in an integer A, guaranteed to be a positive number
ID: 673788 • Letter: W
Question
Write a C program that reads in an integer A, guaranteed to be a positive number, and then prints various shapes of size A. For this assignment, you must ensure that: You use individual functions to print each picture. Name your functions task1, task2 and task3. Each function should take one parameter (an integer representing the desired size) and return nothing (void). Any printf statements that you use can only print a single character. For example, you can use printf("X"); but not printf("XXXXX");.
For each of the tasks shown below, use the minimum number of print statements that you can to complete the task. Finally, realize that the images shown are for an input of X = 10 (ten). Your program should work for any positive number, not just ten.
task2(num);
//////////////////////////////////////////////////// // Task 3, generate a big X
//
//X X
//X X // X X // X X
// XX
// XX
// X X
// X X //X X //X X
task3(num);
return 0; }
Note that all three tasks will involve nested loops. A couple of hints on this program (for the second and third tasks). For the second task, you are printing either forward or backward slashes. Take a look at what your loop counter values are at each location where you print a forward slash. Then consider your loop counter values whenever you print a backward slash. Do you see a pattern when printing a forward (or backward) slash? For the third task, take a look at the loop counter values when you are printing the line of X’s that goes from the top left to the bottom right. Do you notice any pattern to those locations? Repeat that analysis for the line of X’s that goes from the bottom left to the top right.
Explanation / Answer
#include <stdio.h>
void task1(int);
void task2(int);
void task3(int);
int main(int argc, char **argv) {
int num;
printf("Enter a positive integer : ");
scanf("%d", &num);
task1(num);
task2(num);
//////////////////////////////////////////////////// // Task 3, generate a big X
//
//X X
//X X // X X // X X
// XX
// XX
// X X
// X X //X X //X X
task3(num);
return 0; }
void task1(int num)
{
int i, j;
for(i = 1; i <= num; i++)
{
for(j = 1; j <= num - i + 1; j++)
printf("X");
printf(" ");
}
}
void task2(int num)
{
int i, j;
for(i = 1; i <= num; i++)
{
for(j = 1; j <= num; j++)
{
if((i+j)%2 == 0)
printf("\");
else
printf("/");
}
printf(" ");
}
}
void task3(int num)
{
//Please check with the task3 pattern. It doesn't follow a pattern it seems.
int i, j;
for(i = 0; i < num; i++)
{
printf("X");
for(j = 0; j < num/2 - j; j++)
printf(" ");
printf("X");
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.