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 c++a 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[100][100]; // Assuming a maximum size of 100 for the hourglass pattern
// Helper function to fill the hourglass pattern
void fillHourglass(){
// Reset all elements of the array to 0
for(int i =0; i size; ++i){
for(int j =0; j size; ++j){
arr[i][j]=0;
}
}
int number =1;
int start =0, end = size -1;
for(int i =0; i size; ++i){
for(int j = start; j = end; ++j){
arr[i][j]= number++;
}
if(i size/2){
start++;
end--;
} else {
start--;
end++;
}
}
}
// Helper function to find the width needed for formatting
int getWidth(){
int max_num = size * size;
int width =0;
while(max_num >0){
max_num /=10;
width++;
}
return width;
}
public:
// Constructor to initialize the size of the hourglass
Hourglass(int s) : size(s){}
// Function to print the hourglass pattern
void print(){
fillHourglass();
int width = getWidth();
// Print the hourglass pattern with appropriate width
for(int i =0; i size; ++i){
for(int j =0; j size; ++j){
cout setw(width) arr[i][j]""; // 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 hourglass(size); // Create an instance of the Hourglass class with the given size
hourglass.print(); // Call the print function to print the hourglass pattern
cout "
More (y/n)?";
cin >> choice;
} while(choice =='y'|| choice =='Y');
return 0;
}
Write a c + + a beginning class program that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!