Monday, June 3, 2013

Some C++ Problems for Beginners (2)


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

1. -AlphabeticalModulus.cpp: Problem Definition:
Write a Program that read 2 Numbers from the user X & Y where 1<= Y <= 9 (if Y is not in
the specified range terminate the program)
Print R = X % Y (R = X Modulus Y) in English Letters

e.g. 
if 
R = 5 --> Print "Five"
 
note: R will always be a value between 0 and 9

[C++]
  1 /*  About The Simple Code "AlphabeticalModulus.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 Write A Program that read 2 Numbers from the user X & Y 
 23 where 1<= Y <= 9 (if Y is not in the specified range terminate the program)
 24 Print R = X % Y (R = X Modulus Y) in English Letters
 25 e.g. 
 26 if 
 27 R = 5 --> Print "Five"
 28 
 29 note: R will always be a value between 0 and 9
 30 */
 31 
 32 char * ConvertToEnglish(int Num);
 33 
 34 
 35 int main ()
 36 {
 37  int X,Y,R;
 38  cout<<"Please Enter X then Y where (1<= Y <=9) :"<<endl;
 39  cin>>X>>Y;
 40  if(Y<1 || Y>9)
 41   {
 42    cout<<"Invalid Y Value";
 43    return 0;
 44   }
 45 
 46  R = X % Y;
 47  cout<<"The Result is : "<<ConvertToEnglish(R)<<endl;
 48  return 1;
 49 }
 50 
 51 
 52 //ConvertToEnglish is a function that convert the integer number to its corresponding 
 53 //English word and returns a Char* (array of chars/string) containing the English word
 54 //e.g. 1 --> One, 2 --> Two, etc...
 55 char * ConvertToEnglish(int Num)
 56 {
 57  switch(Num)
 58  {
 59  case 0:
 60   return "Zero";
 61   break;
 62  case 1:
 63   return "One";
 64   break;
 65  case 2:
 66   return "Two";
 67   break;
 68  case 3:
 69   return "Three";
 70   break;
 71  case 4:
 72   return "Four";
 73   break;
 74  case 5:
 75   return "Five";
 76   break;
 77  case 6:
 78   return "Six";
 79   break;
 80  case 7:
 81   return "Seven";
 82   break;
 83  case 8:
 84   return "Eight";
 85   break;
 86  case 9:
 87   return "Nine";
 88   break;
 89  default:
 90   return "Error";
 91   break;
 92  
 93  }
 94 
 95 }

download the AlphabeticalModulus.cpp  (Commented, Explained Code)

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

No comments:

Post a Comment

Search This Blog