if anyone could please help me with the c++ coding of the following:-

Write a c++ program that does the following:-

Accepts as input n(to be entered at run time and n must be divisible by 3) integers and stores them in a Stack, sort them using any sorting algorithm (of your choice) and store them in an array. The program should then allow removal of the sorted elements from the array in 3s; the first two numbers are used to create a rectangle(if not equal) or a square( if they are equal) while the third is used to create a circle. For each of the shapes created print out the area and any other output that may be necessary.

Dani AI

Generated

A few clarifications and a compact, reliable plan to implement the assignment correctly.

The phrase "n (to be entered at run time and n must be divisible by 3)" means the count of integers must be a multiple of three, not that each integer must be divisible by 3 — this distinction caused confusion in earlier replies. Validate n up front and reject non-positive or non-multiples-of-3 values.

A simple, clear workflow that matches the spec:

  • Read n and validate it (must be >0 and n % 3 == 0).
  • Push the n integers onto a std::stack<int> to satisfy the "store in a Stack" requirement.
  • Pop all items into a std::vector<int>, sort the vector (use std::sort for simplicity and speed), then copy the sorted values into an array for the next stage.
  • Process the array in groups of three: treat the first two as side lengths (a, b) and the third as a circle radius r. For each triplet print a clear summary: "Square" (if a==b) or "Rectangle", dimensions, area, and optionally perimeter; for the circle print radius, area and circumference.

Practical notes and edge cases:

  • Reject or normalize non-positive side/radius values (zero/negative lengths do not make valid geometric shapes).
  • Use a precise value for pi, e.g. const double PI = std::acos(-1.0); and compute circle area with PI * r * r. Format floating output with std::fixed and std::setprecision(2) for readability.
  • If you must sort while keeping everything on stacks, there is a two-stack sorting technique (use one auxiliary stack to build sorted order), but moving to a vector for sorting is simpler and idiomatic in C++.

Notes on earlier replies: gave useful drawing and area ideas, but his answer mixed up the "n divisible by 3" requirement and pushed toward ASCII drawing (Bresenham) which is optional unless the assignment explicitly requires graphics. For this task, focus first on correct input validation, stack/array mechanics, sorting, robust numeric checks, and clear formatted output.

Recommended Answers

All 2 Replies

Here is the pseudo-code for what you wish to accomplish:

1. create an array of integers that will be large enough to handle user input. This array will hold all of the 'n' rectangle data

2. using a loop, continuously prompt the user for coordinates until until such time user no longer wants to input coordinates

3. In your loop, you will have to test user input to see if it meets the "n must be divisible by 3" critera:

if(!numbers[i]%3){ cout << "\a\nError!  Number must be divisible by 3!";}

4. At this point, you have an array of integers, all of which are divisible by 3. Your next requirement is to sort the user input.

#include<algorithm>
sort(&numbers[0], &number[42]);

5. You now have an aray of integers (all of which are divisible by 3) sorted to ascending order. Your next task now is: "the first two numbers are used to create a rectangle(if not equal) or a square( if they are equal). Since we are only allowed to use 2 integer values to draw our square/rectangle, I will assume the integer values will represent 'side lengths' of the objects we wish to draw. As per your requirement, the first two integers excracted from the array will be used to draw a square/rectangle:

#include<iomanip>

void draw_rect(int& topbottom, int& sides)
{
     for(int i=0; i<topbottom; i++)
     {
          //draw top
     }    cout << '*';      
    
     cout << endl;

     for(int i=0; i<sides; i++)
     {
         //draw sides
          cout << '*' << set(topbottom) << "*\n";
     }

     for(int i=0; i<topbottom; i++)
     {
          //draw bottom
     }    cout << '*';    
}

6. Your last task is to create a circle given one integer. I will therefore assume this will be the radius of the circle. We will make the midpoint of the circle our own choosing. This is somewhat complex task, but there is an algorithm out there called, "The Bresenham Circle" which will draw an ascii circle givin a midpoint and a radius. I am confident that with some minor adjustments, you can make this readily available public function work in your program.

7. You are asked to output the area of these objects, "For each of the shapes created print out the area and any other output that may be necessary."

int area_square =  topbottom * sides;

cout << "The area of the square is:  " << area_square << " units squared.";

The area of the circle is pi*r^2. We assumed that the 3rd integer extracted from our number array is a circle radius, therefore we have enough information to calculate circle area:

#include<cmath>
double area_circle = 3.14 * pow(radius, 2);

cout << "The area of the circle is approximately:  " << area_circle << " units squared.";

No one else will give you this amount of help without any effort on your part, so I suggest you take this pseudocode and run with it.

Btw, you'll also need this function which will allow you to set the cursor position to anywhere on the dos console.

#include <windows.h> 

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

You'll probably become much more interested in this when it comes time to draw ye' circle.


Also, there is an error in the draw_rect() function on line #15. Instead of set() the correct function name is setw() from the <iomanip> library. As with all my code, it is not tested and may contain simple easy to fix errors, and is only intended to help others get pointed in the right direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.