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

C++,Devcpp NOTE: Please provide the comment for each section//, I am a beginner,

ID: 3747398 • Letter: C

Question

C++,Devcpp

NOTE: Please provide the comment for each section//, I am a beginner, I need to understand, how the program is working. Thanks.

Diamond pattern, but it adds a bit more work. The steps are:

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.


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.

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.

4) 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.
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

Screenshot

Question1

//Header files
#include<iostream>;
#include<string>
using namespace std;
int main()
{
   //Variable for lines and space creation
   int n = 7, space = n - 1;
   //First loop print upper part of the diamond
   for (int i = 1; i <= n; i++) {
       for (int j = 1; j <= space; j++) {
           cout << " ";
       }
       space--;
       for (int j = 1; j <= 2 * i - 1; j++) {
           cout << "*";
       }
       cout <<" ";
   }
   //Second loop lower part of the diamond
   for (int i = 1; i <= n - 1; i++)
   {
       cout << " ";
       for (int j = 1; j <= space; j++) {
           cout << " ";
       }
       space++;
       for (int j = 1; j <= 2 * (n - i) - 1; j++) {
           cout << "*";
       }
       cout << " ";
   }
    return 0;
}

Output

*
     ***
    *****
   *******
*********
***********
*************
***********
*********
*******
   *****
    ***
     *

--------------------------------------

Question 2

//Header files
#include<iostream>;
#include<string>
using namespace std;
int main()
{
   //Variable for lines and space creation
   int n , space ;
   //User prompt to enter number of lines
   cout << "Enter the number of line: ";
   cin >> n;
   //Check the line count
   if (n <= 0) {
       cout << "We can't create diamond!!!" << endl;
       exit(0);
   }
   else {
       space = n - 1;
       //First loop print upper part of the diamond
       for (int i = 1; i <= n; i++) {
           for (int j = 1; j <= space; j++) {
               cout << " ";
           }
           space--;
           for (int j = 1; j <= 2 * i - 1; j++) {
               cout << "*";
           }
           cout << " ";
       }
       //Second loop lower part of the diamond
       for (int i = 1; i <= n - 1; i++)
       {
           cout << " ";
           for (int j = 1; j <= space; j++) {
               cout << " ";
           }
           space++;
           for (int j = 1; j <= 2 * (n - i) - 1; j++) {
               cout << "*";
           }
           cout << " ";
       }
   }
  
    return 0;
}

--------------------------------------------

Question 3 & 4

//Header files
#include<iostream>;
#include<string>
using namespace std;
int main()
{
   //Variable for lines and space creation
   int n , space,k ;
   //User prompt to enter number of lines
   cout << "Enter the number of rows desired to make a diamond pattern (0 to quit):";
   cin >> n;
   k = n;
   while (k > 0) {
       space = n - 1;
       //First loop print upper part of the diamond
       for (int i = 1; i <= n; i++) {
           for (int j = 1; j <= space; j++) {
               cout << " ";
           }
           space--;
           for (int j = 1; j <= 2 * i - 1; j++) {
               cout << "*";
           }
           cout << " ";
       }
       //Second loop lower part of the diamond
       for (int i = 1; i <= n - 1; i++)
       {
           cout << " ";
           for (int j = 1; j <= space; j++) {
               cout << " ";
           }
           space++;
           for (int j = 1; j <= 2 * (n - i) - 1; j++) {
               cout << "*";
           }
           cout << " ";
       }
       //User prompt to enter number of lines
       cout << "Enter the number of rows desired to make a diamond pattern (0 to quit):";
       cin >> n;
       k = n;
   }
  
   //Check the line count
   if (n <= 0) {
       cout << "Goodbye!!!" << endl;
       exit(0);
   }
  
  
    return 0;
}

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