Hackety Hacking Problem Longsum Implemented c

From OLPC
Revision as of 20:59, 16 October 2007 by Vijjay (talk | contribs) (New page: == Problem Statement == Hi 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 with it can work with is limited"....)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Problem Statement

Hi 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 with 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 lets 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.

HINT: 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 thins 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;

}

Anubhav please see the program I have complicated it a bit as i want to avoid any factor of limit if i use array of char.Please check it.....