Some handy-ish algorithms

Whilst I was derping around, I made a couple somewhat useful algorithms for finding info about shapes. Here they are.

Firstly, though, the variables.
1
2
3
4
5
6
float base;
float height;
float depth;
float volume;
float surfaceArea;
float circumference;


And the algorithms.

Cylinder:
1
2
circumference = base * 2 /*Radius X 2 is diameter, right?*/ * 3.14;
surfaceArea = (circumference * height) /*Find the area of the Cylinder body*/ + (base * base * 3.14 * 2) /*End caps*/;


Rectangular prism/cube:
1
2
volume = base * height * depth; //B X H X D, or L X W X H
surfaceArea = (base * height * 2) /*The ends*/ + (base * depth * 2) /*Top and bottom*/ + (height * depth * 2) /*Sides*/;


Parallelogram:
1
2
volume = base * height; //B X H = Area. I stored it in volume because I didn't wanna make another variable.
surfaceArea = (base * 2) + (height * 2); //The two parallel sides are equal. Also, didn't want another var. 


Circle:
1
2
volume = base * base * 3.14; //Area is PiR2. Also... Well, you know.
surfaceArea = (base * 2) /*Diameter*/ * 3.14; //C = PiD 


For reference, and context, here's the entire source.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/

/*
Shapedesc, by Some Dude

Version 0.25

Supports Rectangular Prisms, Cylinders, Paralellograms, and Circles
*/

#include <iostream>

using namespace std;

//Variable table
int option;
int loop = 42; //Change and DIE.
float base;
float height;
float depth;
float volume;
float surfaceArea;
float circumference;

int descCylinder(void) //The function for describing Cylinders
{
	//Prompt to get info
	cout << "Radius: ";
	cin >> base; //Didn't wanna make another variable.
	cout << "Height: ";
	cin >> height;
	
	//The part with math in it
	volume = (base * base * 3.14) /*Or "PiR2*/ * height;
	cout << "Volume: " << volume << "\n";
	circumference = base * 2 /*Radius X 2 is diameter, right?*/ * 3.14;
	surfaceArea = (circumference * height) /*Find the area of the Cylinder body*/ + (base * base * 3.14 * 2) /*End caps*/;
	cout << "Surface Area: " << surfaceArea << "\n";
	
	return 0;
}

int descRectPrism(void) //The function for describing Rectangular prisms
{
	//Prompt to get info
	cout << "Base: ";
	cin >> base;
	cout << "Height: ";
	cin >> height;
	cout << "Depth: ";
	cin >> depth;
	
	//The part with math in it
	volume = base * height * depth; //B X H X D, or L X W X H
	cout << "Volume: " << volume << "\n";
	surfaceArea = (base * height * 2) /*The ends*/ + (base * depth * 2) /*Top and bottom*/ + (height * depth * 2) /*Sides*/;
	cout << "Surface Area: " << surfaceArea << "\n";
	
	return 0;
}

int descParalellogram(void)//The function for describing paralellograms, squares, rectangles, etc.
{
	//Prompt to get info
	cout << "Base: ";
	cin >> base;
	cout << "Height: ";
	cin >> height;
	
	//The part with math in it
	volume = base * height; //B X H = Area. I stored it in volume because I didn't wanna make another variable.
	cout << "Area: " << volume << "\n";
	surfaceArea = (base * 2) + (height * 2); //The two paralell sides are equal. Also, didn't want another var.
	cout << "Perimiter: " << surfaceArea << "\n";
	
	return 0;
}

int descCircle(void) //The function for describing Circles
{
	//Prompt to get info
	cout << "Radius: ";
	cin >> base; //We have enough damn variables!
	
	//The part with math in it
	volume = base * base * 3.14; //Area is PiR2. Also... Well, you know.
	cout << "Area: " << volume << "\n";
	surfaceArea = (base * 2) /*Diameter*/ * 3.14; //C = PiD
	cout << "Circumference: " << surfaceArea << "\n";
	
	return 0;
}

int main(void)
{
	
	//Main loop
jumpPoint: //Yeah, yeah, yeah, gotos are bad practice... But they kick @$$.
	while (loop == 42)
	{
		cout << "1: Cylinder, 2: Rectangular prism, 3: Paralellograms, 4: Circles, E: Exit> ";
		cin >> option;
		
		if (option == 1) //Cylinders
		{
			descCylinder();
			goto jumpPoint;
		}
		
		else if (option == 2) //Rectangular Prisms
		{
			descRectPrism();
			goto jumpPoint;
		}
		
		else if (option == 3) //Paralellograms
		{
			descParalellogram();
			goto jumpPoint;
		}
		
		else if (option == 4) //Circles
		{
			descCircle();
			goto jumpPoint;
		}
		
		else //Exit
		{
			break;
		}
	}
	
	return 0;
}


If you can think of some improvements, say so!
Last edited on
If you can think of some improvements, say so!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
jumpPoint: //Yeah, yeah, yeah, gotos are bad practice... But they kick @$$.
	while (loop == 42)
	{
		cout << "1: Cylinder, 2: Rectangular prism, 3: Paralellograms, 4: Circles, E: Exit> ";
		cin >> option;
		
		if (option == 1) //Cylinders
		{
			descCylinder();
			goto jumpPoint;
		}
		
		else if (option == 2) //Rectangular Prisms
		{
			descRectPrism();
			goto jumpPoint;
		}
		
		else if (option == 3) //Paralellograms
		{
			descParalellogram();
			goto jumpPoint;
		}
		
		else if (option == 4) //Circles
		{
			descCircle();
			goto jumpPoint;
		}
		
		else //Exit
		{
			break;
		}
	}


Umm...You have a loop, why not use it the way it is designed?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int option = 1;

while(option > 0 && option < 5) //between 1 and 4
{
    cout << "1: Cylinder, 2: Rectangular prism, 3: Paralellograms, 4: Circles, Anything Else: Exit> ";
    cin >> option;



    //a switch may be used as well.
    if (option == 1) //Cylinders
    {
        descCylinder();
    }
		
    else if (option == 2) //Rectangular Prisms
    {
        descRectPrism();
    }
		
    else if (option == 3) //Paralellograms
    {
        descParalellogram();
    }
		
    else if (option == 4) //Circles
    {
        descCircle();
    }
}

Last edited on
Agreed- there's literally no reason for the goto. None whatsoever.
Are you seriously using 3.14 for pi ? Do you not know it is an infinite number ? Two decimal places is definitely not enough.
Most people either use 2 or 4 places after the decimal point. c++ does not have an "infinite" pi.
Most people either use 2 or 4 places after the decimal point.

Actually most people use the PI button, or in programming something called a constant variable.

c++ does not have an "infinite" pi.

Never said it had it nor did i say you need it.
Yes and that pi button or constant variable have infinite digits..Because that wouldn't take you know an infinite time to calculate... On calculators it is probably only to about the 16th digit. This is really irrelevant and I think you are arguing just for fun.

2 digits is fine for this simple program he has.
ezchgg I think you need a snickers.
@guyofdoom42

Why did you use 42 ?

My guess is "Hitchhiker's Guide to the Galaxy"
Where's my copy of the GNU General Public License?
Oh crap.

I left my real name in the source. If you saw it, FORGET YOU SAW IT.

That being said, I'll use your suggested edits. And yes, it was because Hitchhikers.
Your name is also visible on your public profile.

Also, I do have to agree that 3.14 is horribly imprecise for most use cases.
well i mean its not like hes writing a graphing lib or a high precision calculator
That's not my real name on my profile. But I did start using 3.14159 instead of 3.14...
Yes and that pi button or constant variable have infinite digits..Because that wouldn't take you know an infinite time to calculate... On calculators it is probably only to about the 16th digit. This is really irrelevant and I think you are arguing just for fun.

I'm not saying to use an infinite number, just that PI is infinite, and you use the greatest number of decimals that your finite number system supports. For a hand held calculator it could be 16, for something that needs accuracy maybe to the 100th or more. If you are in high school or later in your education system, you've should have learnt to keep PI value separate for this very reason. Instead placing the symbol PI in your answer. When someone needs to use your equation or otherwise they can specify the accuracy of PI that they need. Whether it's an elementary school student that uses 3.14 or a nasa scientist that needs PI to the 100th digit. It works.

It is relevant, they teach it to you in elementary school, if you've already forgot, or don't have that high of an education. There's no reason to be lazy and say "it doesn't matter for this application", that's how you learn bad habits and that's how mistakes are made.

2 digits is fine for this simple program he has.

Calculate area of a circle, with R = 100, it already loses accuracy to two whole digits.
Last edited on
Topic archived. No new replies allowed.