Friday, January 17, 2014

C++ Binary Exercise with Example Code to Develop Your Algorithm Skills

Once you understand the basics of C++ programming language, it is essential for you to develop your problem solving skills using C++ program. In other words, you should know how to develop your programming logic to solve a given problem.

In this tutorial, we’ll give a simple binary problem, which you should solve by writing a C++ program.

Problem Definition

The user will enter the number of digits (n) of a binary number. You need to write a C++ program that will generate all binary numbers with n ciphers, of which two are ones and the rest of ciphers are zeros.
For example:
User input: n=3
Program output: 011, 101, 110.
User input: n=4
Program output: 0011, 0101, 0110, 1001, 1010, 1100.
User input: n=5
Program output: 00011, 00101, 00110, 01001, 01010, 01100, 10001, 10010, 10100, 11000..

Problem Analysis

We can solve this problem using several ways. The following are three possible solutions among several potential solutions.
Algorithm 1: Generate all n cipher binary numbers and display only those that have two ones and rest zeros in their binary presentation.
Algorithm 2: Try to discern the pattern, and translate those numbers into their binary format.
Algorithm 3: First, let us write the output as shown below. We have two markers that represent position of two ones in binary number. For simplicity we could call them the left one and the right one. At the start position, the first row, the left one is located at the second position from right end and the right one is located at far right. Right one shifts to the left side, and when it reaches the left one, it resets its position and goes to the far right end, the left one moves one position toward left end. When left one reaches left end, and the right one is just next to the left one, we stop the program.
0011,
0101, 0110,
1001, 1010, 1100
The first algorithm shown above is very straight forward. It creates a correct solution, but the processor would have many cases of unnecessary checks. However, this approach would be acceptable if we wish to display n binary numbers with k ones.
The second algorithm shown above is good in terms of speed, but its implementation could be difficult to understand.
So, we’ll pick the third algorithm to solve our problem.

C++ Program Code to Solve the Problem

If you are totally new to C++ programming, you should first understand C++ class and object.
The following C++ program code was developed using the third algorithm explained above, which will solve our given problem.
#include <iostream>

using namespace std;

void Display( int , int, int);

int
main(void){

 cout<<"How many digits in the binary number->";
 int iN; cin>>iN;

 //Start position for left and right marker
 int i= iN-1,
     j= iN;
 while(!((i==1)&&(j==2)))
 {
  Display( i, j, iN);

  if(i==j-1)
  {
   i--; j=iN; cout<<endl;
  }
  else
  {
   j--;
  }
 }

 cout<<"11";
 for(int i=2; i<iN; cout<<'0',i++);
 cout<<endl;

 int iEnd; cin>>iEnd;

 return EXIT_SUCCESS;
}

void 
Display( int i,int j,int n)
{
 for(int k=1; k<= n; k++)
  if( (k==i)||(k==j))
   cout<<'1';
  else
   cout<<'0';
 cout<<endl;
}

Additional Exercises

  1. Try to solve a similar problem, but n cipher binary number has only single one.
  2. Try to solve a similar problem, but n cipher binary number has three ones.
  3. Try to solve a similar problem, but n cipher binary number has k ones.
  4. Try to disassemble arbitrary positive integer n into the sum of two squares of positive integers. a^2 + b^2 = n where a,b <n.
  5. Try to disassemble arbitrary positive integer n into the sum of two cubes of positive integers. a^3 + b^3 = n where a,b <n.
  6. If we have a set of k different positive integers and one positive integer n.
    • You need to find whether is it possible to find two numbers from the set, whose sum would equal the n. If possible determine all positive representation.
    • Find the sum of an arbitrary two numbers from the set, whose sum is closest but not equal nor greater than n.
    • Find the sum of an arbitrary two numbers from the set, whose sum is closest but not equal nor lesser than n.
    • Find all combination of two numbers from the set which sum is in certain interval [x...y].
  7. We have the set of k positive integers, and one positive integer n. Investigate whether is it possible to get a number n as a: sum, difference or product of two arbitrary numbers ki and kj from the set.

0 comments:

Post a Comment