Question

In: Computer Science

C++ In this lab you will be using nested for loops to print out stars in...

C++
In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this:

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

For example , if number of rows=8 then the above pattern is derived.


You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".


Solutions

Expert Solution

code:

#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
using namespace std;

int main()
{
int n; //declaring the variables
ifstream input_diamond;
ofstream output_diamond;
input_diamond.open("input_diamond.txt",ios::in); //opening the input file
output_diamond.open("output_diamond.txt",ios::out); //creating the output file
input_diamond>>n; //assigning input file value to the n
for(int i=1;i<=n;++i){
for(int j=1;j<=2*i-1;++j){
output_diamond<<"*"; //printing the * in output file
}
output_diamond<<"\n";
}
for(int i=n-1;i>=0;i--){
for(int j=1;j<=2*i-1;++j){
output_diamond<<"*";
}
output_diamond<<"\n";
}
output_diamond.close();
}

output:


Related Solutions

Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
/* Homework Lab 8 Nested If statements Write the nested decision logic to print the recommended...
/* Homework Lab 8 Nested If statements Write the nested decision logic to print the recommended meal based on the user's choices for Restaurant and Load given the table shown below: Restaurant Preference Meal ----------------------------------------------------- (M)cDonald's (B)eef Quarter Pounder            (C)hicken Chicken McNuggets            (F)ish Filet-o-Fish            (S)alad Side Salad (O)utback            (B)eef Ribeye            (C)hicken Alice Springs Chicken            (F)ish Grilled Tilapia            (S)alad Aussie...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Create a nested structure in C (one structure that contains another) and print out all the...
Create a nested structure in C (one structure that contains another) and print out all the fields in both structures. The main structure should have fields for: movie name and the year it was released. The extended structure should include the original structure as well as fields for: Lead actor, genre and runtime.
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ####12 ###123 ##1234 #12345
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345...
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345 1) the source code (.java file), and 2) the screenshot of running results of each question.
In C 1- Use nested for loops to create an addition lookup table. Fill in an...
In C 1- Use nested for loops to create an addition lookup table. Fill in an array and print out the following table showing the sum of the row and column numbers 0-9. 0 1 2 3 4.. <- column numbers 1 2 3 4 5.. 2 3 4 5 6.. <- These nos. = Row + Column 3 4 5 6 7.. <- for example 7 = 3 + 4 4 5 6 7 8.. ... ^Row numbers 2-...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT