Question: Write a c + + a beginning class program that takes a positive odd number as input and outputs the following. You may assume that
Write a ca beginning class program that takes a positive odd number as input and outputs the following. You may assume that you always enter positive odd numbers correctly. result should look like an hourglass with zeros on the empty space like in output examples. in my code is there another without using arrays, vectors or private classes
#include
#include For setw
using namespace std;
class Hourglass
private:
int size;
int arr; Assuming a maximum size of for the hourglass pattern
Helper function to fill the hourglass pattern
void fillHourglass
Reset all elements of the array to
forint i ; i size; i
forint j ; j size; j
arrij;
int number ;
int start end size ;
forint i ; i size; i
forint j start; j end; j
arrij number;
ifi size
start;
end;
else
start;
end;
Helper function to find the width needed for formatting
int getWidth
int maxnum size size;
int width ;
whilemaxnum
maxnum ;
width;
return width;
public:
Constructor to initialize the size of the hourglass
Hourglassint s : sizes
Function to print the hourglass pattern
void print
fillHourglass;
int width getWidth;
Print the hourglass pattern with appropriate width
forint i ; i size; i
forint j ; j size; j
cout setwwidth arrij; Print numbers with setw
cout endl; Move to the next line after printing a row
;
int main
char choice;
do
int size;
cout
Enter size: ;
cin size;
Hourglass hourglasssize; Create an instance of the Hourglass class with the given size
hourglass.print; Call the print function to print the hourglass pattern
cout
More yn;
cin choice;
whilechoice y choice Y;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
