Thursday, August 22, 2013

Data Compression Techniques - 1


Hi Everyone, My post today is about Data Compression Techniques, I’ve decided to post about Data Compression because It’s really widely used everywhere around us in many applications that we are using everyday… I really like this CS topic trying everyday to know more about it…
Today I’ll be talking about:
  1. Types/ Techniques of Data Compression
  2. Lossless Data Compression
    1. Huffman Coding
    2. LZC Coding
  3. Lossy Data Compression
    1. DCT Coding
Let’s Start now;
First, Simply There is 2 main techniques in data compression:
  1. Lossless Data Compression
  2. Lossy Data Compression
1. Lossless Data Compression: Where no Loss of info –> The compressed data can be decoded into exactly the original data.
2. Lossy Compression: Because sometimes we need to save size and time we are using this technique; Lossy Compression is not accurate 100%, This Compression technique is used in analog ranged data such as videos, JPEG encoding and sounds.
and now let’s talk about the most known fast Lossless Data Compression Algorithms that are used nowadays: Huffman and Lempel-ZIV coding … let’s take a look on them

Examples of Lossless Data Compression Algorithms

1-Huffman Algorithm:
A Huffman code is designed by merging together the two least probable characters, and repeating this process until there is only one character remaining. A code tree is thus generated and the Huffman code is obtained from the labeling of the code tree. An example of how this is done is shown below.
Huffman Design Animation
The final static code tree is given below:
Huffman Design Animation

  • It does not matter how the characters are arranged. The Tree have been arranged above so that the final code tree looks nice and neat.
  • It does not matter how the final code tree are labeled (with 0s and 1s). we have chosen to label the upper branches with 0s and the lower branches with 1s.
  • There may be cases where there is a tie for the two least probable characters. In such cases, any tie-breaking procedure is acceptable.
  • Huffman codes are not unique.
  • Huffman codes are optimal in the sense that no other lossless fixed-to-variable length code has a lower average rate.
  • The rate of the above code is 2.94 bits/character. [1]
  • The entropy lower bound is 2.88 bits/character. [2]


  • 2-Lempel-Ziv Coding (LZ Coding):
    The basic idea is to parse the input sequence into non-overlapping blocks of different lengths while constructing a dictionary of blocks seen thus far.

    Encoding Algorithm:
    1. Initialize the dictionary to contain all blocks of length one (D={a,b}).
    2. Search for the longest block W which has appeared in the dictionary.
    3. Encode W by its index in the dictionary.
    4. Add W followed by the first symbol of the next block to the dictionary.
    5. Go to Step 2.



      The final code results are given below: Lempel-Ziv Encoding Animation
    after talking about LZ Coding … now let’s talk about one of the most known Lossy Data Compression Algorithms : The Discrete Cosine Transform (DCT) used in JPEG and MP3 Compressions

    Example of Lossy Data Compression Algorithms

    1-Discrete Cosine Transform (DCT) Algorithm:
    expresses a finite sequence of data points in terms of a sum of cosine functions oscillating at different frequencies. DCTs are important to numerous applications in science and engineering, from lossy compression of audio (e.g. MP3) and images (e.g. JPEG) (where small high-frequency components can be discarded) (Wikipedia)
    you can take a look on this Demo Java Applet that applies the DCT Algorithm: http://www.comp.nus.edu.sg/~cs5248/0910S2/l01/DCTdemo.html
    DCT Simulation tool:
    http://pi4.informatik.uni-mannheim.de/pi4.data/content/animations/dct_2d/index.html
    e.g.
    Consider this 8x8 grayscale image of capital letter A.

    Original size, scaled 10x (nearest neighbor), scaled 10x (bilinear).
    DCT of the image.
    
\begin{bmatrix}
6.1917 & -0.3411 & 1.2418  &  0.1492  &  0.1583  &  0.2742 &  -0.0724  &  0.0561 \\
0.2205 & 0.0214 & 0.4503  &  0.3947  & -0.7846 &  -0.4391  &  0.1001  & -0.2554 \\
1.0423 & 0.2214 & -1.0017 &  -0.2720  &  0.0789 &  -0.1952  &  0.2801  &  0.4713 \\
-0.2340 & -0.0392 & -0.2617 &  -0.2866 &   0.6351 &   0.3501 &  -0.1433  &  0.3550 \\
0.2750 & 0.0226 & 0.1229  &  0.2183  & -0.2583  & -0.0742  & -0.2042  & -0.5906 \\
0.0653 & 0.0428 & -0.4721 &  -0.2905  &  0.4745  &  0.2875  & -0.0284  & -0.1311 \\
0.3169 & 0.0541 & -0.1033 &  -0.0225  & -0.0056  &  0.1017  & -0.1650 &  -0.1500 \\
-0.2970 & -0.0627 & 0.1960 &   0.0644  & -0.1136 &  -0.1031 &   0.1887  &  0.1444 \\
\end{bmatrix}

    Basis functions of the discrete cosine transformation with corresponding coefficients (specific for our image).
    Each basis function is multiplied by its coefficient and then this product is added to the final image.

    On the left is final image. In the middle is weighted function (multiplied by coefficient) which is added to the final image. On the right is the current function and corresponding coefficient. Images are scaled (using bilinear interpolation) by factor 10x.
    you can know more about DCT here : https://www.tach.ula.ve/vermig/DCT_TR802.pdf





    [1] Compression Ratio: It’s the ration between the bits of data before compression and the Bits of data after compression ( Original Bits:Compressed Bits)
    e.g.
    if we have a file with an original data size of 65,535 bytes .. This file became 16,384 bytes after applying some data compression algorithms on it. we can say that the compression ratio is 65535:16384 which is approximately 4:1 or we can just say now that the file is 75% compressed (The total size is 100% and the compressed size is 25% of the total);
    Now if we have 8 bits per each byte we can say that we are representing each byte by just 2 bits (25 %) or 2 bits per byte or 2 bits/character

    *
    The Difference between the Original data and the Reconstruction data is called “Distortion”  also “Fidelity” and “Quality”.


    [2] The entropy rate of a source: is a number which depends only on the statistical nature of the source. If the source has a simple model, then this number can be easily calculated. Here, we consider an arbitrary source:
    while paying special attention to the case where is English text.
    Zero-Order Model: The characters are statistically independent of each other and every letter of the alphabet,, are equally likely to occur. Let be the size of the alphabet. In this case, the entropy rate is given by

    For English text, the alphabet size is m=27. Thus, if this had been an accurate model for English text, then the entropy rate would have been H=log2 27=4.75 bits/character.



    References:
    1-Khalid Sayoud Introduction to Data Compression 2nd Edition, 2006.
    2-A. Gersho and R. M. Gray, Vector Quantization and Signal Compression.
    3-D. A. Huffman, ``A Method for the Construction of Minimum Redundancy Codes,'' Proceedings of the IRE, Vol. 40, pp. 1098--1101, 1952.
    4-J. Ziv and A. Lempel, ``A Universal Algorithm for Sequential Data Compression,'' IEEE Transactions on Information Theory, Vol. 23, pp. 337--342, 1977.
    5-J. Ziv and A. Lempel, ``Compression of Individual Sequences Via Variable-Rate Coding,'' IEEE Transactions on Information Theory, Vol. 24, pp. 530--536, 1978.
    6-T. A. Welch, ``A Technique for High-Performance Data Compression,'' Computer, pp. 8--18, 1984.
    7.C. E. Shannon, ``Prediction and Entropy of Printed English," available in Shannon: Collected Papers.

    Hope you enjoyed this Data Compression Introduction and Thanks for Reading;  Smile
    For More Info Please Visit Data Compression - Debra A. Lelewer and Daniel S. Hirschberg

    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

    Search This Blog