Hackety Hacking Problem Cells in Human Body StringManipulation

From OLPC
Jump to: navigation, search

Problem Statement

How many cells are in an adult human? Lots. More than anyone could count, and the bigger you are the more cells there would be. Growth is a process of cellular reproduction, so as you grow bigger you are made up of more cells.

Although no exact number can be given, the order of magnitude of the number of cells in a human body can be approximated to 10e14 or one hundred trillion cells.

Thus if you have to perform some arithmetic operation on the number of cells, you have to store it in a data type which should have ideally infinite range of storage(limited by size of Computer!).


HINT: Do you know one of the limitations of your compiler.Let me introduce you with it. This is that "size of the integers or number which it can work with is limited". So many a times, it happens that you cannot do a sum on your compiler because it exceeds the size of integer supported by the compiler. So here we try to do something so that you can do the calculations without caring about the size of the integer and not even losing precision.

Write a program that takes two no and gives output as its sum.Use Strings to achieve the task.

Sample Output

Enter the first no            : 5555555555555555555555555
Enter the second no           : 4444444444444444444444444
The sum of the two numbers is : 9999999999999999999999999

Curriculum Connections

This problem gives student help in understanding few things mentioned below:

  • How to convert data from one type to another(TYPECASTING)
  • Why typecasting is useful
  • The benefit of using STRINGS
  • Knowing about the limitations of the compiler

Solution

#include<iostream>   //Header file
#include<string>
#include<algorithm>

using namespace std; //Namespace a bit of syntax

int main()
{

//these are two strings used to save the two numbers as there is no bound on the size of the string 

string a;                              
string b;
string c;

//take the two numbers

cout<<"Enter the first no    : ";
cin>>a;
reverse(a.begin(),a.end()); //this command reverses the string so that we can eeasily manipulate it
cout<<"\nEnter the second no   : ";
cin>>b;
reverse(b.begin(),b.end());
int i;
int maxm=max(a.length(),b.length());
int minm=min(a.length(),b.length());

//this block formats the shorter string to be equal to that of the larger array by appending zeros in the end

if(a.length()<b.length())
{
 for(i=minm;i<maxm;i++)
 {
  a[i]='0';                         
 }
}
else
{
  for(i=minm;i<maxm;i++)
 {
  b[i]='0';                         
 }  
}
for(i=0;i<=maxm;i++)
   c[i]='0';

//This for loop used to carry out the sum starting from the LSB(Least significant bit)

for(i=0;i<maxm;i++)
{
  c[i]=char(c[i]+a[i]-'0'+b[i]-'0');
  if(c[i]>'9')
  {
     c[i+1]++;
  }
}

//This loop is used  to check if there is any carry for the last digit

if(c[maxm]=='0')
{
i=maxm-1;
}

//This block is used to print the output on the screen 

cout<<"The sum of the two numbers is: ";

//This loop prints the sum in reverse order as we have to show MSB(Most significant Bit) First 

for(;i>=0;i--)
{
  cout<<c[i];
}

return 0;

}