C++ ** please provide comment for each section as I am fairly new to coding and
ID: 3749318 • Letter: C
Question
C++
** please provide comment for each section as I am fairly new to coding and need to understand how the program actually works.
1) Write code that displays a 7-line diamond pattern on the screen. This can be done using a double-nested for-loop. The inner loop displays a line of stars, one column (space ' ' or star ‘*’) at a time. The outer loop displays multiple lines, one row (“*****”) at a time. This 7-line diamond pattern can easily be done with 7 cout statements, but then you cannot complete the next step.
Test this. If you stop here, you have earned up to 12 points max.
2) Instead of only making diamonds with exactly 7 lines, ask the user how many lines to make the diamond, then make the diamond with the size desired. If the size requested is <=0, don’t make a diamond. It should work for both even and odd number of lines. See chapter 5, top of page 267 (7ed), page 270 (8ed), page 271 (9ed) for code that will loop using a for-loop. Position diamonds to the far left. Don't display unnecessary extra columns of spaces on the far left or right.
Test this. If you stop here, you have earned up to 16 points max.
3) It is tedious to use and test a program if it does one thing and stops. In this step, put the diamond drawing code inside another loop. It is now triple-nested. The outer loop should repeatedly prompt the user for the number of lines to make the diamond. If the number is <= 0, quit. Otherwise, make a diamond pattern with the number of lines entered. Keep asking the user for the number of lines to draw, then draw one diamond shape, UNTIL the user wants to quit. A size of <=0 means to quit. This looping pattern is similar to the one described in Lab 1.
Test this. If you stop here, you have earned up to 20 points max.
Extra credit: When creating the row of spaces (" ") and stars (“***”), you can construct a string with exactly the number of characters (' ' or ‘*’') you want in one statement without using a loop. Hint: This is described in Table 12-6 on page 806 (7ed) and Table 12-4 on page 817 (8ed). This string constructor was removed from Table 12-3 on page 840 (9ed), but here is a hint: string(int n, char ch); This simplifies the code by eliminating the inner-most loop.
Test this. If you stop here, you have earned up to 22 points max. This is the most possible points.
Example:
Enter the number of rows desired to make a diamond pattern (0 to quit): 5
*
***
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 7
*
***
*****
*******
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 4
*
***
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 0
Goodbye!
Hint: Each row consists of spaces and stars. To compute the correct number of spaces and stars for each row, consider making a table: line number, spaces, stars. Think of the relationship between: line number, number of spaces for that line, number of stars for that line. For help with this, look in “Program Samples” for “Excel file to aid Lab 2: lines to diamonds”. This is a spreadsheet that helps to analyze the problem.
Even a simple lab like this has valid variations. For example, more symmetric diamonds may be drawn like this:
This program draws diamonds using simple ASCII
Enter number of rows in diamond, 0 to quit: 1
*
Enter number of rows in diamond, 0 to quit: 2
**
**
Enter number of rows in diamond, 0 to quit: 3
*
***
*
Enter number of rows in diamond, 0 to quit: 4
**
****
****
**
Enter number of rows in diamond, 0 to quit: 0
Good-bye!
Explanation / Answer
#include<iostream>
using namespace std;
//method to print diamond pattern
void print_diamond(int x)
{
int k;
k= x/2 ;
int as=1;
if(x%2==0)as=2;
int i=0;
while(i<k)
{
//printing upper half
int n=0;
while(n<as){
cout<<"*";n++;}
cout<<endl;
as=as+2;
i++;
}
if(x%2==0)as=as-2;
while(k<x)
{
//printing lower half
int n=0;
//cout<<"hello"<<as;
while(n<as){
cout<<"*";n++;}
cout<<endl;
as=as-2;
k++;
}
}
int main()
{
int n;
while(1)
{
//reading input
cout<<" Enter number of rows in diamond, 0 to quit: ";
cin>>n;
if(n==0)
{
cout<<" Good-bye! ";
break;
}
//calling method print_diamond(n)
print_diamond(n);
}
return 0;
}
output:
Enter number of rows in diamond, 0 to quit: 1
*
Enter number of rows in diamond, 0 to quit: 2
**
**
Enter number of rows in diamond, 0 to quit: 3
*
***
*
Enter number of rows in diamond, 0 to quit: 4
**
****
****
**
Enter number of rows in diamond, 0 to quit: 5
*
***
*****
***
*
Enter number of rows in diamond, 0 to quit: 7
*
***
*****
*******
*****
***
*
Enter number of rows in diamond, 0 to quit: 0
Good-bye!
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.