## Monday, June 3, 2013

### Some C++ Problems for Beginners (5)

This Post has some Previous Posts, you can access the first Main Post here: Some C++ Problems for Beginners (1)
The code was written and tested on Visual Studio 2010 –> “Empty Windows Console Application” . if you don’t have Visual Studio, you can get the latest version from :
-Visual C++ 2010 Express:

note that, you can also use another IDEs like:
-Visual Studio Express 2012 for Windows Desktop: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products#product-express-summary
-CodeBlocks: http://sourceforge.net/projects/codeblocks
-GNU C++ compiler GCC(g++): http://gcc.gnu.org/
-MingW GCC port for windows: http://www.mingw.org/
-ShapesAreas.cpp:
Problem Definition:
Write a program that take from the user "1" if Circle, "2" if Rectangle, "3" if Triangle,
"4" if Square & "5" if Parallelogram
then Calculate and Print the Area of the chosen Shape after taking the inputs from the
user.
Note:
Area of a:
Circle = π × r2               (read r from the user and consider that π = 3.14)
Rectangle = w x h                       (read w and h from the user)
Triangle = 1/2 base x h         (read base and h from the user)
Square = L2                             (read L from the user)
Parallelogram = base * h         (read base and h from the user)

 [C++] ``` 1 /* About The Simple Code "ShapesAreas.cpp" 2 Copyright (C) 2013 Eng. Mohamed Adel (http://www.med-adel.com) 3 4 This simple code is a free simple: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see .*/ 16 17 18 #include 19 #define PI 3.14 20 using namespace std; 21 22 /*Problem Definition 23 Write a program that take from the user "1" if Circle, 24 "2" if Rectangle, "3" if Triangle, "4" if Square & "5" if Parallelogram 25 then Calculate and Print the Area of the chosen Shape after taking the inputs from the user. 26 Note: 27 Area of a: 28 Circle = π × r2 (read r from the user and consider that π = 3.14) 29 Rectangle = w x h (read w and h from the user) 30 Triangle = 1/2 base x h (read base and h from the user) 31 Square = L2 (read L from the user) 32 Parallelogram = base * h (read base and h from the user) 33 */ 34 35 //Prototype(s) 36 void ReadCalculatePrintArea(int choice); 37 float CalculateCircleArea(int r); 38 float CalculateRectangleArea(int w, int h); 39 float CalculateTriangleArea(int base, int h); 40 float CalculateSquareArea(int L); 41 float CalculateParallelogramArea(int base, int h); 42 43 void main() 44 { 45 int choice; 46 cout<<"Please, enter 1 if Circle, 2 if Rectangle, 3 if Triangle, 4 if Square and 5 if Parallelogram : "<>choice; 48 ReadCalculatePrintArea(choice); 49 } 50 51 //ReadCalculatePrintArea is a function that take the Choice from the User then 52 //1. Reads the inputs from the user based on the chosen choice 53 //2. Calls the Specific CalculateArea Function based on the chosen shape 54 //3. Prints on the screen the Final Area Result taken from the specified Called CalculateArea Function 55 void ReadCalculatePrintArea(int choice) 56 { 57 58 float Area; 59 switch(choice) 60 { 61 case 1: 62 int r; 63 cout<<"Please Enter the Radius of the Circle : "<>r; 65 Area = CalculateCircleArea(r); 66 cout<<"The Area of the specified Circle is : "<>w>>h; 73 Area = CalculateRectangleArea(w,h); 74 cout<<"The Area of the specified Rectangle is : "<>base>>h2; 81 Area = CalculateTriangleArea(base,h2); 82 cout<<"The Area of the specified Triangle is : "<>L; 89 Area = CalculateSquareArea(L); 90 cout<<"The Area of the specified Square is : "<>base2>>h3; 97 Area = CalculateParallelogramArea(base2,h3); 98 cout<<"The Area of the specified Parallelogram is : "<