Write a C++ program that draw a diamond as the sample execution below. In the pr
ID: 639345 • Letter: W
Question
Write a C++ program that draw a diamond as the sample execution below. In the program, you can assume that the number given by a user is always valid (= positive number). In the program, you must use only pointer variables and dynamic memory. [For instance, even an index variable in a loop must be a pointer variable]. A sample run of your program should be like below:
Enter a number: 5
*
***
*****
*******
*********
*******
*****
***
*
This is another sample run of your program:
Enter a number: 3
*
***
*****
***
*
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
for(int i=1; i<5; i++)
{
for(int j=1; j<=i; j++)
{
cout<<"*";
}
cout<<endl<<endl;
}
for(int i=5; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
cout<<"*" ;
}
cout<<endl<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.