Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that displays a diamond of given the odd length (anodd integer s

ID: 3613995 • Letter: W

Question

Write a program that displays a diamond of given the odd length (anodd integer
size less than or equal to 21). Your program should accept as inputfrom the
keyboard the character used to form the diamond, and the value forthe length of
the diamond.

Out of range input values should result in an informative errormessage (if it does
not meet the specifications of this assignment).

Your program should process only one input case, legal or not.Termination occurs
after either the error message is printed or the diamond isdrawn.

It is necessary that the program be written using loops.

Sample runs:

Diamond drawing program
Enter the character, and length (eg. x 7):A 7
   A
A A
A   A
A     A
A   A
A A
   A

Diamond drawing program
Enter the character, and length (eg. x 7):R 17
        R
       R R
      R   R
     R     R
    R       R
   R        R
R           R
R            R
R              R
R            R
R           R
   R        R
    R       R
     R     R
      R   R
       R R
        R

Explanation / Answer

int main(){
int n,i,j;
char ch;
cout<<"Enter char and odd int :";
cin>>ch;

cin>>n;
if(n %2 ==0 || n<0 || n >21)
{
     do{
            cout<<"Invalid int range( 1-21) and odd:";
            cin>>n;
            }while(n %2 ==0 || n<0 || n >21);
}
for(i=0;i<n/2+1;i++)
{
for(j=n/2-i;j>=0;j--)
cout<<" ";
cout<<ch;
for(j=1;j<=2*i-1;j++)
cout<<" ";
if(i>0)
cout<<ch;
cout<<endl;
}
for(i=n/2-1;i>=0;i--)
{
for(j=n/2-i;j>=0;j--)
cout<<" ";
cout<<ch;
for(j=1;j<=2*i-1;j++)
cout<<" ";
if(i>0)
cout<<ch;
cout<<endl;
}

system("pause");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote