Monday, June 3, 2013

Some C++ Problems for Beginners (7)


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

-2DimArrayFrom1DimArray.cpp:
Problem Definition:
Supposing that you have the following 1 Dimensional Array called A as a known input:
int A[10] = {1,1,2,5,3,4,9,8,7,1};
 
Fill the Char Array B (char B [10][10]) with the Histogram values taken from the Array A
then Print the Histogram in B
================================================
output:
-------
 
*
*
**
*****
***
****
*********
********
*******
*
 
================================================

[C++]
  1 /*  About The Simple Code "_2DimArrayFrom1DimArray.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 #include <iostream>
 18 using namespace std;       
 19 
 20 /*Problem Definition
 21 Supposing that you have the following 1 Dimensional Array called A as a known input:
 22 int A[10] = {1,1,2,5,3,4,9,8,7,1};
 23 
 24 Fill the Char Array B (char B [10][10]) with the Histogram values taken 
 25 from the Array A then Print the Histogram in B
 26 ================================================
 27 output:
 28 -------
 29 
 30 *
 31 *
 32 **
 33 *****
 34 ***
 35 ****
 36 *********
 37 ********
 38 *******
 39 *
 40 
 41 ================================================
 42 //*///Prototypes
 43 void FillArray(int A[10], char B[10][10]);
 44 void PrintArray(char B[10][10]);
 45 
 46 void main()
 47 {
 48  int A[10] = {1,1,2,5,3,4,9,8,7,1};        
 49  char B [10][10];            
 50 
 51  FillArray(A, B);            
 52  PrintArray(B);             
 53 }
 54 
 55 //Function "FillArray" takes 2 Attributes A --> The input Array & B --> The output Array
 56 //It takes the values from the Array "A" and fill the Output Array "B" with 
 57 //Stars and Spaces to Draw the Histogram
 58 void FillArray(int A[10], char B[10][10] )
 59 {
 60  for(int i = 0; i < 10; i++)         
 61  {
 62   for(int j = 0; j <10; j++)        
 63   {
 64    if( j < A[i])          
 65    {
 66     B[i][j] = '*';         
 67    }
 68    else
 69    {
 70     B[i][j] = ' ';        
 71    }
 72   }
 73  }
 74 
 75 }
 76 
 77 
 78 //Function "PrintArray" takes 1 Attribute B --> The Output Array and prints it
 79 //It Loops over all the Array "B" elements and print them all (The Histogram)
 80 void PrintArray(char B[10][10])
 81 {
 82  for(int i = 0; i < 10; i++)     
 83  {
 84   for(int j = 0; j < 10; j++)    
 85   {
 86    cout<<B[i][j];      
 87   }
 88   cout<<endl;        
 89 
 90  }
 91 
 92 }

download the 2DimArrayFrom1DimArray.cpp  (Commented, Explained Code)

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

Some C++ Problems for Beginners (6)


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
-TwoDimArray.cpp:
Problem Definition:
Write a code fragment that construct a two-dimensional array (name it B) of integers with
5 rows and 5 columns. Then:
-Fill-in the array B to contain the multiplication table from 1 to 5
-Find the sum of the numbers for each row
-Find the sum of all numbers in the array

[C++]
  1 /*  About The Simple Code "TwoDimArray.cpp"
  2     Copyright (C) 2013  Eng. Mohamed Adel (http://www.med-adel.com)
  3     
  4 
  5     This simple code is a free simple: you can redistribute it and/or modify 
  6     it under the terms of the GNU General Public License as published by 
  7     the Free Software Foundation, either version 3 of the License, or 
  8     (at your option) any later version. 
  9   
 10     This program is distributed in the hope that it will be useful, 
 11     but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13     GNU General Public License for more details. 
 14   
 15     You should have received a copy of the GNU General Public License 
 16     along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
 17 
 18 
 19 #include <iostream>
 20 using namespace std;   
 21 
 22 
 23 /* Problem Definition: ... taken from the Quizes quiz D)d
 24 Write a code fragment that construct a two-dimensional array (name it B) of integers 
 25 with 5 rows and 5 columns. Then:
 26 -Fill-in the array B to contain the multiplication table from 1 to 5
 27 -Find the sum of the numbers for each row
 28 -Find the sum of all numbers in the array
 29 */
 30 
 31 //Prototypes
 32 void InsertIntoArray(int B[5][5]);
 33 void RowSum(int B[5][5]);
 34 void ArraySum(int B[5][5]);
 35 
 36 
 37 void main()
 38 {
 39  int B[5][5];    
 40  InsertIntoArray(B);   
 41  RowSum(B);     
 42  ArraySum(B);    
 43 
 44 }
 45 
 46 
 47 /* 2D Array Structure /*
 48 
 49 1 2 3 4 5
 50 2 4 6 8 10
 51 3 6 9 12 15
 52 4 8 12 16 20
 53 5 10 15 20 25
 54 
 55 */
 56 
 57 //Function InsertIntoArray is a Function that insert the values of 
 58 //the multiplication table from 1 to 5 and print them on the screen
 59 void InsertIntoArray(int B[5][5])
 60 {
 61  cout<<"Full Array"<<endl<<"=_==_==_==_==_==_==_==_==_="<<endl;
 62  for(int i = 0; i<5; i++)    
 63  {          
 64   for (int j = 0; j<5; j++)   
 65   {         
 66   B[i][j] = (i+1) * (j+1);   
 67   cout<<B[i][j];      
 68   } 
 69   cout<<endl;       
 70  }          
 71 }
 72 
 73 //Function RowSum is a Function that Print the Sum of each Row from the 2D Array on the Screen
 74 void RowSum(int B[5][5])
 75 {
 76  cout<<endl<<"Row Sum"<<endl<<"=_==_==_==_==_==_==_==_==_="<<endl;
 77  int sum = 0;         
 78  for(int i = 0; i<5; i++)      
 79  {
 80   for (int j = 0; j<5; j++)     
 81   {
 82   sum += B[i][j];        
 83   }
 84   cout<<sum<<endl;       
 85   sum=0;          
 86  }
 87 }
 88 
 89 
 90 //Function RowSum is a Function that Print the Sum of all the values in the 2D Array on the Screen
 91 void ArraySum (int B[5][5])
 92 {
 93  cout<<endl<<"Array Sum"<<endl<<"=_==_==_==_==_==_==_==_==_="<<endl;
 94  int sum = 0;       
 95  for(int i = 0; i<5; i++)    
 96  {
 97   for (int j = 0; j<5; j++)   
 98   {
 99   sum += B[i][j];      
100   } 
101  }
102  cout<<sum<<endl;      
103 }



download the TwoDimArray.cpp  (Commented, Explained Code)

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

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:
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
-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 <http://www.gnu.org/licenses/>.*/
 16 
 17 
 18 #include <iostream>
 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 : "<<endl;
 47     cin>>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 : "<<endl;
 64   cin>>r;                 
 65   Area = CalculateCircleArea(r);           
 66   cout<<"The Area of the specified Circle is : "<<Area<<endl;    
 67   break;
 68 
 69  case 2:                  
 70   int w,h;                
 71   cout<<"Please Enter the Width and the Height of the Rectangle :"<<endl;
 72   cin>>w>>h;                
 73   Area = CalculateRectangleArea(w,h);          
 74   cout<<"The Area of the specified Rectangle is : "<<Area<<endl;   
 75   break;
 76 
 77  case 3:                  
 78   int base,h2;               
 79   cout<<"Please Enter the Base and the Height of the Triangle :"<<endl;
 80   cin>>base>>h2;               
 81   Area = CalculateTriangleArea(base,h2);         
 82   cout<<"The Area of the specified Triangle is : "<<Area<<endl;   
 83   break;
 84 
 85  case 4:                  
 86   int L;                 
 87   cout<<"Please Enter the Lenght of the Square : ";
 88   cin>>L;                 
 89   Area = CalculateSquareArea(L);           
 90   cout<<"The Area of the specified Square is : "<<Area<<endl;    
 91   break;
 92 
 93  case 5:                  
 94   int base2,h3;               
 95   cout<<"Please Enter the Base and the Height of the Parallelogram";
 96   cin>>base2>>h3;               
 97   Area = CalculateParallelogramArea(base2,h3);       
 98   cout<<"The Area of the specified Parallelogram is : "<<Area<<endl;  
 99   break;
100 
101  default:                 
102   cout<<"Invalid Choice";
103   break;
104  }
105 }
106 
107 //CalculateCircleArea is a function that return the Circle Area
108 float CalculateCircleArea(int r)
109 {
110  return PI * r * r;
111 }
112 
113 //CalculateRectangleArea is a function that return the Rectangle Area
114 float CalculateRectangleArea(int w, int h)
115 {
116  return w * h;
117 }
118 
119 //CalculateTriangleArea is a function that return the Triangle Area
120 float CalculateTriangleArea(int base, int h)
121 {
122  return 0.5 * base * h;
123 }
124 
125 //CalculateSquareArea is a function that return the Square Area
126 float CalculateSquareArea(int L)
127 {
128  return L * L;
129 }
130 
131 //CalculateParallelogramArea is a function that return the Parallelogram Area
132 float CalculateParallelogramArea(int base, int h)
133 {
134  return base * h;
135 }

download the ShapesAreas.cpp  (Commented, Explained Code)

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

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

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

Some C++ Problems for Beginners (1)


Yesterday, I’ve decided to write some simple C++ programs to help beginners to know how to write some code;
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. –DynamicStars.cpp: Problem Definition:
Write a Program that Dynamically draw Stars * from "N" entered by the User to 1
 
e.g. 
 
Input: 3
 
Output:
 
***
**
*

[C++]
  1 /* About The Simple Code "DynamicStars.cpp"
  2 
  3 Copyright (C) 2013 Eng. Mohamed Adel (http://www.med-adel.com)
  4 
  5 This simple code is a free simple: you can redistribute it and/or modify 
  6 
  7 it under the terms of the GNU General Public License as published by 
  8 
  9 the Free Software Foundation, either version 3 of the License, or 
 10 
 11 (at your option) any later version. 
 12 
 13 This program is distributed in the hope that it will be useful, 
 14 
 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 
 16 
 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 18 
 19 GNU General Public License for more details. 
 20 
 21 You should have received a copy of the GNU General Public License 
 22 
 23 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
 24 
 25 #include <iostream>
 26 
 27 using namespace std;
 28 
 29 /*Problem Definition
 30 
 31 Write a Program that Dynamically draw Stars * from "N" entered by the User to 1
 32 
 33 e.g. 
 34 
 35 Input: 3
 36 
 37 Output:
 38 
 39 ***
 40 
 41 **
 42 
 43 *
 44 
 45 */
 46 
 47 void DrawStars(int n);
 48 
 49 void main ()
 50 
 51 {
 52 
 53  int n;
 54 
 55  cout<<"Please enter the max number of stars to be drawn :";
 56 
 57  cin>>n;
 58 
 59  DrawStars(n); 
 60 
 61 }
 62 
 63 
 64 /*DrawStars is a function that draw Stars starting from N till 1 Star*/
 65 void DrawStars(int n)
 66 
 67 {
 68 
 69  for(int i = 0 ; i < n; i++)
 70 
 71  {
 72 
 73   for(int j=0; j < n-i; j++) 
 74   { 
 75    cout<<"*"; 
 76   }
 77 
 78   cout<<endl; 
 79 
 80  }
 81 
 82 }


download the DynamicStars.cpp  (Commented, Explained Code)

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

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

Search This Blog