Monday, June 3, 2013

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

No comments:

Post a Comment

Search This Blog