Reuse the asterisk diamond program and create a function diamond (......) that t
ID: 3856563 • Letter: R
Question
Reuse the asterisk diamond program and create a function diamond (......) that takes two values, an integer for the side of the diamond and a char value for the character printed and outputs the corresponding diamond shape. The function has default values, 5 for the side and * for the character. The user can choose to use the default values or specify new values. Do you want to use the default values? (Y/N): y Do you want to use the default values? (Y/N): n Enter an integer for the side of the diamond: 3 Enter a char value for the character printed: @Explanation / Answer
NOTE: Let me know if there are any issues. I will revert back within 24 hours.
Code:
#include <iostream>
#include <string>
#include <locale>
using namespace std;
void diamond(int n = 5, char ch = '*')
{
int no_of_symbols, i, j, k;
// printing pyramid format
no_of_symbols = 1;
for(i = 1; i <= n; i++){
for(j = 1; j <= n -i; j++)
cout << " ";
for(k = 1; k <= no_of_symbols; k++)
cout << ch;
cout << " ";
no_of_symbols += 2;
}
// printing reverse pyramid format
for(i = 1; i <= n; i++){
for(j = 1; j <= i; j++)
cout << " ";
for(k = no_of_symbols-4; k > 0; k--)
cout << ch;
cout << " ";
no_of_symbols -= 2;
}
}
int main()
{
char choice, ch;
int n;
cout << "Do you want to use the default values? (Y/N) ";
cin >> choice;
if(tolower(choice) == 'y')
diamond();
else{
cout << "Enter an integer for the side of the diamond: ";
cin >> n;
cout << "Enter a char value for the character to be printed: ";
cin >> ch;
diamond(n, ch);
}
}
Execution output sceenshot:
https://pasteboard.co/GAGEQEr.png
https://pasteboard.co/GAGEVML.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.