#include<iostream> #include<cmath> using namespace std; int main(){ int num; cou
ID: 645164 • Letter: #
Question
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int num;
cout <<"Enter number of rows (3-23): ";
cin >> num;
if(num%2==0) num++;
int h = num/2;
for(int i=0; i<=num; i++){
if(i!=num/2) {
for(int k=0; k<(num/2-abs(h)); k++)
cout << " ";
for(int p=0; p<abs(num-2*i); p++)
cout <<"*";
cout << endl;
h--;
}
}
return 0;
}
break the program into functions for the various tasks.
int GetNRows( void );
// draw the shape, using helpers as needed (described below)
void DrawShape( int nRows );
// calculate number of spaces given current row and row number
int nSpaces( int nRows, int row );
// calculate number of stars given current row and row number
int nStars( int nRows, int row );
// print n copies of character c on the current row
void PrintChars( int n, char c );
Explanation / Answer
Breaking the program into functions:
#include<iostream>
#include<cmath>
using namespace std;
int GetNRows(void);
void DrawShape(int nRows);
int nSpaces(int nRows,int row); /* function declaration*/
int nStars(int nRows,int row); /* function declaration*/
void PrintChars(int n,char c);
/* MAIN PROGRAM: */
int main()
{
int row,space;
cout << "Enter number of rows from 3-23: "; /* < - - - line 9 */
cin >> row;
if(row < 3)
{
cout << "Too few rows!" << end1; /* line 13 */
return 0;
}
/* END OF MAIN PROGRAM */
/* FUNCTION TO CALCULATE */
if(row > 23)
{
cout<<"Too many rows!" << end1;
return 0;
}
if(row % 2 == 0)
{
row++;
}
space = row/2;
for(int i=0; i<=row; i++)
{
if(i!=row/2)
{
for(int k=0;k < (row/2-abs(space)); k++)
{
cout << ' ';
}
for(int p= 0; p< abs(row-2*i);p++)
cout<< ' * ' ;
cout << end1;
space--;
}
}
return 0;
}
/* end of function definition */
/* END OF FUNCTION*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.