I need to write a program that asks the user for a number of asterisks in a midd
ID: 3563902 • Letter: I
Question
I need to write a program that asks the user for a number of asterisks in a middle row and then the number of rows. There cannot be less than 2 asterisks in any row as well. This is what I have, but I am not sure how to decrease the amount of asterisks by two in each row to make the oval shape.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int mRow(0);
int numRow(0);
cout<<"Enter size of middle row: ";
cin>>mRow;
while (mRow <= 2){
cout<<"Enter size of middle row: ";
cin>>mRow;
}
cout<<"Enter number of rows: ";
cin>>numRow;
for( int i = 0 ; i <= mRow; i++){
for( int j = 0; j <= numRow; j++){
cout<<"*";
}
cout<<endl;
}
return(0);
}
Thank you for your help
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int mRow(0);
int numRow(0);
int space;
int a,b;
cout<<"Enter size of middle row: ";
cin>>mRow;
while (mRow <= 2){
cout<<"Enter size of middle row: ";
cin>>mRow;
}
cout<<"Enter number of numRow: ";
cin>>numRow;
space = numRow - 1;
for ( b = 1 ; b <= numRow ; b++ ) {
for ( a = 1 ; a <= space ; a++ )
printf(" ");
space--;
for ( a = 1 ; a <= 2*b-1 ; a++)
printf("*");
printf(" ");
}
// Print second half of the triangle.
space = 1;
for ( b = 1 ; b <= numRow - 1 ; b++ ) {
for ( a = 1 ; a <= space; a++)
printf(" ");
space++;
for ( a = 1 ; a <= 2*(numRow-b)-1 ; a++ )
printf("*");
printf(" ");
}
return(0);
}
-----------------------------------------------------------------------------------------------------------------------------------------------
when numRow = 3
Sample Output: