The third line of an input data file, called pyramid. txt, contains a number bet
ID: 3797462 • Letter: T
Question
The third line of an input data file, called pyramid. txt, contains a number between 1 and 15 inclusive. Write a correct and thorough C++ program that reads this number and prints a corresponding pyramid on the screen. The printed output must be formatted properly in symmetrical fashion. For example, if the number is 4, then the output on the monitor will be: Assumptions to be made: only the main algorithm is required for header documentation. only an integer appears on the third line of pyramid. txt. the number will be in the range 1, 2, 3, ..., 14, 15.Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream myf;
myf.open("pyramid.txt");
int n;
myf>>n;
int k;
for(int i=1;i<=n;i++)
{
k = 2*(n-i);
if(n>9 && i>9)
{
k += n-i;
}
else
{
k += n-9;
}
for(int j=0;j<k;j++)
{
cout<<" ";
}
for(int j=i;j>=1;j--)
{
cout<<j<<" ";
}
for(int j=2;j<=i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.