Monday, June 3, 2013

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

No comments:

Post a Comment

Search This Blog