Your first lab will be similar to the examples discussed in class, covering prog
ID: 3786938 • Letter: Y
Question
Your first lab will be similar to the examples discussed in class, covering programming concepts in loop, function, and recursion. It includes the following tasks. Using C/C++, write loop(s) to control and output a star pattern as follows: giving a integer N (use can to assign N), first, you need to print a triangle with N rows, the star number in each row equals the row number (e.g. the 1^st row has 1 star, the N^th row has N stars). Then output a reverse triangle with (N-1) roe, the first row of the reverse triangle has (N-1) stars, and the last row of the reverse triangle has 1 star. All stars are center aligned, and the pattern is in a diamond shape. E.g. when N is 3, you will print the following star pattern * ** *** ** *Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int N, k, i, S = 1;
cout<<"Enter the rows-> ";
cin>>N;
S = N - 1;
for (i = 1; i<=N; i++)
{
for (k = 1; k<=S; k++)
cout<<" ";
S--;
for (k = 1; k<= i; k++)
cout<<"*"<<" ";
cout<<" ";
}
S = 1;
for (i = 1; i<=(N - 1); i++)
{
for (k = 1; k<= S; k++)
cout<<" ";
S++;
for (k = 1 ; k<= (N-i); k++)
cout<<"*"<<" ";
cout<<" ";
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.