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.

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.