write a program using nested for loops that displays the following... * ** *** *
ID: 3544351 • Letter: W
Question
write a program using nested for loops that displays the following...
*
**
***
****
*****
****
***
**
*
the program should allow the user to select the maximum number of asterisks in the middle row. (example) if the user entered 3 then the program would print.....
*
**
***
**
*
The current program I am working off of creates a diamond out the asterisks but I am unable to manipulate the size of the middle row and I only need half of the diamond displayed in the program below.
#include<iostream>
#include<iomanip>
int main()
{
const int N=9;
for (int i = 0; i <= 2 * N; i++) {
for (int j = 0; j <= 2 * N; j++) {
if (i <= N) {
if (j < N - i || j > N + i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
else {
if (j < i - N || j > 3 * N - i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
}
std::cout << std::endl;
}
system ("pause");
return 0;
}
Explanation / Answer
#include<iostream>
#include<iomanip>
int main()
{
const int N=9;
for (int i = 0; i <= (2 * N); i++) {
for (int j = 0; j <= N-1; j++) {
if (i <= N) {
if (j < N - i || j > N + i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
else {
if (j < i - N || j > 3 * N - i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
}
std::cout << std::endl;
}
system ("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.