Monday, June 3, 2013

Some C++ Problems for Beginners (3)


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:
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express

note that, you can also use another IDEs like:
-Visual Studio Express 2012 for Windows Desktop: http://www.microsoft.com/visualstudio/eng/downloads#d-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/
-C++ Builder (Borland): http://www.embarcadero.com/products/cbuilder

-EquationProblem.cpp:
Problem Definition:
Without Using MATH.h write a program that can solve the following equation and Display
"R" on the screen
R = X^Y + Y^Z - (X + Y)^2

[C++]
  1 /*  About The Simple Code "EquationProblem.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 <http://www.gnu.org/licenses/>.*/
 16 
 17 
 18 #include <iostream>             
 19 using namespace std;            
 20 
 21 /*Problem Definition
 22 Without Using MATH.h write a program that can solve the following equation and Display "R" on the screen
 23 R = X^Y + Y^Z - (X + Y)^2
 24 */
 25 
 26 
 27 float SolveEquation(int X, int Y, int Z);
 28 int Power(int Num, int Pow);
 29 
 30 void main ()              
 31 {
 32  int X,Y,Z;
 33  cout<<"Please enter X, Y and Z : ";
 34  cin>>X>>Y>>Z;             
 35  
 36  float R = SolveEquation(X,Y,Z);         
 37  cout<<"The Result is "<<R;          
 38 }
 39 
 40 
 41 /*SolveEquation is a function that Solve the equation >> R = X^Y + Y^Z - (X + Y)^2 */
 42 float SolveEquation(int X, int Y, int Z)
 43 {
 44  float r = Power(X,Y) + Power(Y,Z) - Power((X+Y),2);    
 45  return r;
 46 }
 47 
 48 /*Power is a function that take a Number and a Power Number and 
 49 returns the Number at the Power of the Power Number (Num^Pow) e.g. 2^3=8 */
 50 int Power(int Num, int Pow)
 51 {
 52  int r=1;
 53 
 54  for(int i = 0; i<Pow; i++)      
 55  {
 56   r*=Num;          
 57  }
 58  return r;          
 59 }

download the EquationProblem.cpp  (Commented, Explained Code)

Download all the C++ Simples I’ve created : C++ Problems

No comments:

Post a Comment

Search This Blog