Monday, June 3, 2013

Some C++ Problems for Beginners (4)


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/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

-ZeroSmaller.cpp: Problem Definition:
Write a function called ZeroSmaller() that is passed three int arguments by reference and 
then sets the smaller of the three numbers to "0"

[C++]
  1 /*  About The Simple Code "ZeroSmaller.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 ... inspired from the sheets Quiz H)D) but a little bit more complex
 22 Write a function called ZeroSmaller() that is passed three int arguments by reference and 
 23 then sets the smaller of the three numbers to "0"
 24 */
 25 
 26 void ZeroSmaller (int &X, int &Y, int &Z);
 27 
 28 void main()
 29 {
 30  int X,Y,Z;
 31  cout<<"Please, Enter the 3 Numbers : "<<endl;
 32  cin>>X>>Y>>Z;          
 33  ZeroSmaller(X,Y,Z);         
 34  cout<<"First Number = "<<X<<endl<<"Second Number = "<<Y<<endl<<"Third Number = "<<Z<<endl;
 35 }
 36 
 37 /*
 38 ZeroSmaller is a function that Set the minimum number of the 3 Inputs numbers to "0"
 39 
 40 e.g.  if 
 41 Inputs:
 42 X = 3, Y = 5, Z = 1 (Minimum)
 43 
 44 Outputs:
 45 X = 3, Y = 5, Z = 0
 46 */
 47 void ZeroSmaller (int &X, int &Y, int &Z)
 48 {  
 51  (X < Y)? ((X < Z)? X = 0 : Z = 0) : ((Y < Z)? Y = 0 : Z = 0);
 52 
 53  /* Another Way to do the function ..Or The normal way *//*
 54           
 55 if(X < Y)
 56  {
 57   if (X < Z)
 58   {
 59    X = 0;
 60   }
 61   else
 62   {
 63    Z = 0;
 64   }
 65  }
 66  else
 67  {
 68   if(Y < Z)
 69   {
 70    Y = 0;
 71   }
 72   else
 73   {
 74    Z = 0;
 75   }
 76  }        
 77 
 78 */
 79  
 80 }



download the ZeroSmaller.cpp  (Commented, Explained Code)

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

No comments:

Post a Comment

Search This Blog