11 Replies - 3614 Views - Last Post: 30 January 2011 - 09:13 AM Rate Topic: -----

#1 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Sum of large integers (classes and functions)

Posted 27 January 2011 - 06:25 PM

I HAVE A PROBLEM WITH MY CODE...I CREATE A CLASS TO MAKE THE SUM OF LARGE INTEGERS..CAN I GET SOME HELP HERE ...IT IS MY FIRST PROGRAM WITH CLASS FUNCTION.. I DONT GET THE SUM AND THE FIRST INTEGER COMPLETE...THANK YOU
//my calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H

using namespace std;

const int MAXIMUM_SIZE = 20;  


class Calculator
{
public:
	
	void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);  
	void input_Large_Int(int a[], int& size_of_A);
	void output_Large_Int(int a[], int size_of_A);  
	
	int a[20];
	int b[20];
	int sum[20];

Calculator(); // constractor

};

#endif



Calculator::Calculator()
{
	int i=0;

	for (i=0;i<MAXIMUM_SIZE;i++)  

         {  

            a[i]=0;  

            b[i]=0;  

            sum[i]=0;  

              

         }  

            

}  


void introduction()  

 {  

     cout << "This program will allow you to input two large integers and add\n" 

          << "them together. Then you will be allowed to run the program again\n" 

           << "and again depending on how many calculations you would like to make.\n" 

           << endl << endl;  

     }  

    

 void input_Large_Int(int a[], int& size_of_A)  

 {  

     char digit[MAXIMUM_SIZE];  

     char change;  

     int i = 0;  

    

     cout << "Please enter a positive integer with no more than " << MAXIMUM_SIZE  

          << " digits: ";  

    

     cin.get(change);  

     if (change == '\n')  

         cin.get(change);  

     while (isdigit(change) && i < MAXIMUM_SIZE)  

     {  

         digit[i] = change;  

         i++;  

         cin.get(change);  

     }  

   

     size_of_A = i;  

     int  j = 0;  

    while (i > 0)  

     {  

         i--;  
         a[j] = digit[i] - '0';  

         j++;  

     }  

   

     for (int i = j; i < MAXIMUM_SIZE; i++)  

         a[i] = 0;  

    

 }  

    

 void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)  

 {  

      int i;  
	  sum[20]=0; 	
   

      for(i = 0; i < MAXIMUM_SIZE -1 ; i++)  

         {  

              sum[i] += (a[i] + b[i]) % 10;   

              sum[i + 1] += (a[i] + b[i]) / 10;  

               }  

              i++;  

              sum[i] += (a[i] + b[i]) %10;  

               if ((a[i] + b[i])/10 > 0)  

               {  

               cout << "There was an overflow causing more than 20 integers.\n" 

                   << endl;  
				
			   
               }  

      }  

 void output_Large_Int(int a[], int size_of_A)  

 {  

       

    for (int i = 0; i < size_of_A; i++)  

         cout << a[size_of_A - i - 1];  


 
 } 



AND THIS IS MY MAIN CPP.
#include<iostream>
#include<string>
#include<cmath>
#include "mycalculator.h"
#include <cstdlib>  
#include <iostream>  
#include <cctype>  

using namespace std;


int a[MAXIMUM_SIZE], b[MAXIMUM_SIZE], sum[MAXIMUM_SIZE];  

     int size_of_A, size_of_B, size_Sum;  

    char ans;  

int main()
{


char input;
char func;


Calculator calc;    

cout<<"YOU CAN CHOOSE +,  x ONLY"<<endl;
cout<<"Do you have 2 numbers to perform a function on? (y/n) ";
cin>>input;

while(input != 'n')
{
	cout<<"What function would you like to try? ";
	cin>>func;
	
	
	{            
		if(func == '+')           
		{
		


    introduction();  

   

     do {  

          

         input_Large_Int(a, size_of_A);  

         input_Large_Int(b, size_of_B)/>;  

   

        add(a, size_of_A, b, size_of_B, sum, size_Sum);  

         cout << "The sum of ";  

         output_Large_Int(a, size_of_A);  

cout << " and ";  

         output_Large_Int(b, size_of_B)/>;  

         cout << " is ";  

   

         output_Large_Int(sum, size_Sum);  

        cout << endl << endl;  

        cout << "Would you like to add two more numbers? (y or n): ";  

         cin >> ans;  

     }  

     while (ans == 'y' || ans == 'Y');  

        

     system("PAUSE");  

     return 0;  

 }  

				
				else
					cout<<"You didn't enter one of the supported functions."<<endl;
			
		
	}
}
	
	
	cout<<"Try again y/n? ";
	
	cin>>input;
}


This post has been edited by macosxnerd101: 27 January 2011 - 06:27 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Sum of large integers (classes and functions)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Sum of large integers (classes and functions)

Posted 27 January 2011 - 09:17 PM

The scope operator (::) is used to specify which class a function belongs to, when it is being defined.

void Calculator::input_Large_Int(int a[], int& size_of_A) 
{



When calling the function an object of the class is required (here calc).
int main()
{
  Calculator calc;  

  calc.input_Large_Int(a, size_of_A);


Was This Post Helpful? 0
  • +
  • -

#3 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 02:37 PM

THANK YOU VERY MUCH FOR YOUR HELP...I CORRECT THOSE MISTAKES BUT WHEN I TYPE THE FIRST 20 INTEGER, I DO NOT GET THE WHOLE NUMBER AND I DONT GET THE SUM, EITHER....IT REALLY DRIVES ME CRAZY ..
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 02:42 PM

May I ask that you not use caps? It's considered shouting.
Was This Post Helpful? 0
  • +
  • -

#5 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 02:45 PM

ok....caps are off...
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 02:46 PM

Thank you. :)
Was This Post Helpful? 0
  • +
  • -

#7 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 02:54 PM

BucthDean. Can you check my code in your Visual Studio program..I can not see my mistakes..
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 03:32 PM

post the code where you fixed those mistakes
Was This Post Helpful? 0
  • +
  • -

#9 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 04:13 PM

//my calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H

using namespace std;

const int MAXIMUM_SIZE = 20;  


class Calculator
{
public:
	
	void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);  
	void input_Large_Int(int a[], int& size_of_A);
	void output_Large_Int(int a[], int size_of_A);  
	
	int a[20];
	int b[20];
	int sum[20];

Calculator(); // constractor

};

#endif



Calculator::Calculator()
{
	int i=0;

	for (i=0;i<MAXIMUM_SIZE;i++)  

         {  

            a[i]=0;  

            b[i]=0;  

            sum[i]=0;  

              

         }  

            

}  


void introduction()  

 {  

     cout << "This program will allow you to input two large integers and add\n" 

          << "them together. Then you will be allowed to run the program again\n" 

           << "and again depending on how many calculations you would like to make.\n" 

           << endl << endl;  

     }  

void Calculator::input_Large_Int(int a[], int& size_of_A)  

 {  

     char digit[MAXIMUM_SIZE];  

     char change;  

     int i = 0;  

    

     cout << "Please enter a positive integer with no more than " << MAXIMUM_SIZE  

          << " digits: ";  

    

     cin.get(change);  

     if (change == '\n')  

         cin.get(change);  

     while (isdigit(change) && i < MAXIMUM_SIZE)  

     {  

         digit[i] = change;  

         i++;  

         cin.get(change);  

     }  

   

     size_of_A = i;  

     int  j = 0;  

    while (i > 0)  

     {  

         i--;  
         a[j] = digit[i] - '0';  

         j++;  

     }  

   

     for (int i = j; i < MAXIMUM_SIZE; i++)  

         a[i] = 0;  

    

 }  

    

void Calculator::add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)  

 {  

      int i;  
	  sum[20]=0; 	
   

      for(i = 0; i < MAXIMUM_SIZE -1 ; i++)  

         {  

              sum[i] += (a[i] + b[i]) % 10;   

              sum[i + 1] += (a[i] + b[i]) / 10;  

               }  

              i++;  

              sum[i] += (a[i] + b[i]) %10;  

               if ((a[i] + b[i])/10 > 0)  

               {  

               cout << "There was an overflow causing more than 20 integers.\n" 

                   << endl;  
				
			   
               }  

      }  

void Calculator::output_Large_Int(int a[], int size_of_A)  

 {  

       

    for (int i = 0; i < size_of_A; i++)  

         cout << a[size_of_A - i - 1];  


 
 } 



//main.cpp
#include<iostream>
#include<string>
#include<cmath>
#include "mycalculator.h"
#include <cstdlib>  
#include <iostream>  
#include <cctype>  

using namespace std;


int a[MAXIMUM_SIZE], b[MAXIMUM_SIZE], sum[MAXIMUM_SIZE];  

     int size_of_A, size_of_B, size_Sum;  

    char ans;  

int main()
{


char input;
char func;


Calculator calc;    

cout<<"YOU CAN CHOOSE +,  x ONLY"<<endl;
cout<<"Do you have 2 numbers to perform a function on? (y/n) ";
cin>>input;

while(input != 'n')
{
	cout<<"What function would you like to try? ";
	cin>>func;
	
	
	{            
		if(func == '+')           
		{
		


    introduction();  

   

     do {  

          

         calc.input_Large_Int(a, size_of_A);  

         calc.input_Large_Int(b, size_of_B)/>;  

   

        calc.add(a, size_of_A, b, size_of_B, sum, size_Sum);  

         cout << "The sum of ";  

         calc.output_Large_Int(a, size_of_A);  

cout << " and ";  

         calc.output_Large_Int(b, size_of_B)/>;  

         cout << " is ";  

   

         calc.output_Large_Int(sum, size_Sum);  

        cout << endl << endl;  

        cout << "Would you like to add two more numbers? (y or n): ";  

         cin >> ans;  

     }  

     while (ans == 'y' || ans == 'Y');  

        

     system("PAUSE");  

     return 0;  

 }  

				
				else
					cout<<"You didn't enter one of the supported functions."<<endl;
			
		
	}
}
	
	
	cout<<"Try again y/n? ";
	
	cin>>input;
}


This post has been edited by ButchDean: 28 January 2011 - 04:23 PM
Reason for edit:: Please use code tags.

Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sum of large integers (classes and functions)

Posted 28 January 2011 - 04:26 PM

victormartin1, neither I or I doubt anyone else is going to make the effort for you that you should be making yourself. Even asking me explicitly to look at the code for you will not happen unless I see some effort on your part to resolve what the problem is.

None of us here are your personal tutor, but we are more than willing to give you the help you need if you show genuine effort on your part.
Was This Post Helpful? 1
  • +
  • -

#11 victormartin1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

Re: Sum of large integers (classes and functions)

Posted 29 January 2011 - 11:20 PM

you are right!...im working hard in this code ...that's why im reading and i need help in this code... i dont get the number in the screen and the sum ...i dont know why

//main cpp.
#include<iostream> 
#include"mycalculator.h"
#include<string>
#include <cctype>  

using namespace std;

bool checkinput(string);


int main()
{
int a[21];
int b[21];
int c[51];
int i,lena=0,lenb=0,lenc=0;


char input;
char func;


Calculator calc;    

cout<<"YOU CAN CHOOSE +  ONLY"<<endl;
cout<<"Do you have 2 numbers to perform a function on? (y/n) ";
cin>>input;

while(input != 'n')
{
	cout<<"What function would you like to try? ";
	cin>>func;
	
	
	
		if(func == '+')           
		{
			
			
			
			
			
			
			calc.getnum("first number: ",a);
			cout<<endl;
			calc.getnum("second number: ",B)/>;
			calc.add(a,b,c,lena,lenb);
			cout<<"    ";
			calc.print(a,lena);
			cout<<"+   ";
			calc.print(b,lenb);
			for(i=0;i<lenc+5;i++)
			 cout<<"-";
			cout<<"\n    ";
			calc.print(c,lenc);
			system("pause");

			return 0;
}
				else
					cout<<"You didn't enter one of the supported functions."<<endl;

}
cout<<"Try again y/n? ";
	
	cin>>input;

}


//h.
//my calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<iostream>
#include<string>
using namespace std;
 
bool checkinput(string);

class Calculator
{
public:
	
	int getnum(string mess,int a[]);
	int add(int a[], int b[],int c[],int lena,int lenb);
	void print (int a[],int);
	

	int a[20];
	int b[20];
	int c[51];
	
	Calculator(); // constractor

};

#endif



Calculator::Calculator()
{
	a[20]=0;
	b[20]=0;
	c[51]=0;

}



bool checkinput(string in)
{int i;
if(in.length()>20)
      return false;
for(i=0; i<in.length();i ++)
     if(in[i]<'0'||in[i]>'9')
          {cout<<"invalid input\n";
          return false;
          }
return true;
}





int Calculator::getnum(string mess, int a[])
{
int i;
for(i=0;i<21;i++)
    a[i]=0;
string input;
cout << "Enter " << mess;
cin>>input;
while(!checkinput(input))
    {cout<<"Enter "<<mess;
     cin>>input;
     }
for(i=0;i<input.length();i++)
     a[input.length()-1-i]=input[i]-'0';
return input.length();
}

int Calculator::add(int a[], int b[], int c[], int lena, int lenb)

{int i,n,carry=0,d;
cout<<endl;
for(i=0;i<21;i++)
    c[i]=0;
if(lena>lenb)
{    
n=lena;
cout<<n<<endl;
}

else
{n=lenb;
cout<<n<<endl;
}
   
for(i=0;i<n;i++)
    {d=a[i]+b[i]+carry;
      c[i]=d%10;
      carry=d/10;
      }
if(carry==0)
   return n;
else
   c[i]=carry;
    
return n+1;
}

void Calculator::print(int a[], int len)
{int i;
  for(i=len-1;i>=0;i--)
       cout<<a[i];
  cout<<endl;
  }


This post has been edited by ButchDean: 30 January 2011 - 09:11 AM
Reason for edit:: For the second time please use code tags.

Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sum of large integers (classes and functions)

Posted 30 January 2011 - 09:13 AM

Please use code tags. :)

Have you tried debugging your code? It might be wise to set breakpoints at the point of interest where values are to be printed and check if they actually contain the expected values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1