Question

In: Computer Science

3. Please complete the example given in lecture; Programs to display pyramid and inverted pyramid using...

3. Please complete the example given in lecture;
Programs to display pyramid and inverted pyramid using * or # and digits

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

Press “1” button add 1 into the pyramid.
Press “1” to move number 1 to next cell (if it is the end of the triangle, loopback)
Press “2” to move number 1 to previous cell (if it is the end of the triangle, loopback)

First press
1
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Second press
*
1 * *
* * * * *
* * * * * * *
* * * * * * * * *
Eighth press
*
* * *
* * * 1 *
* * * * * * *
* * * * * * * * *

Please write in cpp

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

//method to print the triangle with given number of rows, one_location is the location of 1

void printTriangle(int rows, int one_location){

                //initial number of columns

                int cols=1;

                //counter to count each cell

                int counter=0;

                //looping through each row

                for(int i=0;i<rows;i++){

                                //looping through each column

                                for(int j=0;j<cols;j++){

                                               //updating counter

                                               counter++;

                                               //if counter and one_location are same, printing 1

                                               if(counter==one_location){

                                                               cout<<"1";

                                               }

                                               //else printing a '*'

                                               else{

                                                               cout<<"*";

                                               }

                                              

                                }

                                //line break

                                cout<<endl;

                                //increasing number of columns by 2

                                cols+=2;

                }

}

int main(){

                //number of rows

                int rows=5;

                //total number of cells = rows^2

                int numCells=rows*rows;

                //initial location of 1 (outside)

                int oneLocation=0;

                //to store user input

                int input=0;

                //denoting if the 1 is added or not

                bool one_added=false;

                //looping as long as 3 is not entered (an exit case)

                while(input!=3){

                                //printing triangle

                                printTriangle(rows,oneLocation);

                                //printing menu

                                cout<<endl<<"Press \"1\" button add 1 into the pyramid."<<endl;

                                cout<<"Press \"1\" to move number 1 to next cell."<<endl;

                                cout<<"Press \"2\" to move number 1 to previous cell."<<endl;

                                cout<<"Press \"3\" to exit."<<endl; //for exiting the program

                                cout<<"Choice: ";

                                //getting input

                                cin>>input;

                                cout<<endl; //line break

                                if(input==1){

                                               //if 1 is not added, adding to cell 1

                                               if(!one_added){

                                                               oneLocation=1;

                                                               one_added=true;

                                               }else{

                                                               //otherwise, incrementing oneLocation

                                                               oneLocation++;

                                                               //wrapping around if goes out of range

                                                               if(oneLocation>numCells){

                                                                               oneLocation=1;

                                                               }

                                               }

                                }else if(input==2){

                                               //if 1 is added to the triangle, updating oneLocation to previous cell

                                               if(one_added){

                                                               oneLocation--;

                                                               //wrapping around if necessary

                                                               if(oneLocation<1){

                                                                               oneLocation=numCells;

                                                               }

                                               }else{

                                                               //1 is not added yet

                                                               cout<<"Add 1 first"<<endl;

                                               }

                                              

                                }

                               

                }             

                return 0;

}

/*OUTPUT*/

*

***

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 2

Add 1 first

*

***

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

1

***

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 2

*

***

*****

*******

********1

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 2

*

***

*****

*******

*******1*

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

*****

*******

********1

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

1

***

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

1**

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

*1*

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

**1

*****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

1****

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

*1***

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

**1**

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

***1*

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 1

*

***

****1

*******

*********

Press "1" button add 1 into the pyramid.

Press "1" to move number 1 to next cell.

Press "2" to move number 1 to previous cell.

Press "3" to exit.

Choice: 3


Related Solutions

Complete the following: (With Details and example if needed) Given set S = {-3, -2, 0,...
Complete the following: (With Details and example if needed) Given set S = {-3, -2, 0, 1, 2, 4} Relation R is a relation on S such that R = { (?, ?) | ? ∈ ?, ? ∈ ? ??? ?? ≥ 1} 1. Using roster method, what is R? 2. Show the digraph representation of R 3. Show the matrix representation of R 4. if the relation does not have the property, give an example of why: -...
Consider a project of the Pearson Company (as in an example from Lecture 3 slides). The...
Consider a project of the Pearson Company (as in an example from Lecture 3 slides). The timing and size of the incremental after-tax cash flows for an equity-financed project are: Year 0 1 2 3 4                              CF -1,000 325 250 375 500 The firm is financing the project with $600 debt which carries 8% interest rate. The firm currently has no leverage, faces 40% tax rate and has 10% cost of capital. Value the project using flow to Equity...
Complete the following Java program to, using BigDecimal, calculate and display the radius of the circle...
Complete the following Java program to, using BigDecimal, calculate and display the radius of the circle whose diameter is stored in the variable diameter.  The radius is calculated as half of the diameter. For example, since diameter is 7.5, the program should calculate and display the radius like this:  (2 points)   (CLO 1) package q1; import java.math.BigDecimal; public class Q1 {     public static void main(String[] args) {         float diameter=7.5f;                        } }                                             Radius = 3.75 Consider the following Java program and...
Using the "Theory of planned bahavior" complete the following 3) Provide a specific “Personal” “interpersonal” example...
Using the "Theory of planned bahavior" complete the following 3) Provide a specific “Personal” “interpersonal” example of this theory being used. Provide logical support for how this example is inline with the concepts of the theory and making an "Ethical" decsion. Make sure you address all the context levels in that interaction clearly, richly and directly.
Complete each of the programs here. Create a separate Netbeans project for each program using the...
Complete each of the programs here. Create a separate Netbeans project for each program using the name I specified. Create a single java main class for each of the programs using the filename I specified. Task 1: (5 points) Project name: CtoFConverter Main file name: TempConverter.java A program that converts an inputted temperature in C and provides the equivalent temperature in F. Hint: Google is your friend! Given C, solve for F. Again, check for a valid input value and...
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All...
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All the 3 methods should be written in 1 program. Write a java program calls the following methods: printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. Multiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single line using for loop. hasAnEvenDigit: Takes an int (n) as parameter and returns whether n has...
Using the information provided in Lecture #3, compare the amount of energy released by chemical and...
Using the information provided in Lecture #3, compare the amount of energy released by chemical and nuclear reactions, given an identical mass of fuel. Describe two features of nuclear weapons design which reflect the physical differences between chemical and nuclear reactions.
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
3) Describe (by using a hypothetical example) a typical currency swap transaction. Please be sure to...
3) Describe (by using a hypothetical example) a typical currency swap transaction. Please be sure to explain the potential benefits of a typical currency swap transaction to both parties and construct a diagram, which explains the detailed mechanics of such transaction (including all the relevant cash flows in both currencies).
CHAPTER 10 LECTURE NOTES EXAMPLE #3 A car manufacturer wants to test a new engine to...
CHAPTER 10 LECTURE NOTES EXAMPLE #3 A car manufacturer wants to test a new engine to see whether it meets new air pollution standards. The mean emission, μ, of all engines of this type must be less than 20 parts per million of carbon. Ten engines are manufactured for testing purposes, and the mean and standard deviation of the emissions for this sample of engines are determined to be: X¯¯¯=17.1 parts per million     s=3.0 parts per million     n = 10X¯=17.1 parts per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT