can someone help me do this problem
An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0], 3 in a[1], 2 in a[2], and 1 in a[3].

In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of the two numbers. Your program will read the digits as values of type char so that the number 1234 is read as the four characters '1', '2', '3', and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. (Whether or not you reverse the order of the elements in the array is up to you. It can be done either way, and each way has its advantages and disadvantages.)

Your program will perform the addition by implementing the usual paper- and-pencil addition algorithm. The result of the addition is stored in an array of size 20, and the result is written to the screen. If the result of the addition is an integer with more than the maximum number of digits (that is, more than 20 digits), then your program should issue a message saying that it has encountered "integer overflow." You should be able to change the maximum length of the integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.

Recommended Answers

All 12 Replies

You may want to check this out before posting homework.

y would i do that i need to get this done

i have tried and this is what i have so far

// *************************************************************
//
//  BigInt.cpp
//
//  This program adds large integers that are stored in arrays.
//  The digits are stored in the array in reverse order so that
//  addition is easier.  The program allows a user to keep doing 
//  more additions as long as he/she wishes.
//
// *************************************************************

#include <iostream>
#include <cctype>

using namespace std;

const int SIZE = 20;


int digit[SIZE]; // the digits stored in reverse order
int qty=0;

Char Num(double theNum[SIZE+1], int a[SIZE], int sizeA);


void inputBigInt (int a[], int& sizeA);
// Precondition: The user is ready to enter an integer
// Postcondition: The digits of the integer read in are stored
// in reverse order in the array a and the number of digits is 
// stored in sizeA.


void outputBigInt(int a[], int sizeA);
// Precondition: a is defined
// Postcondition: The integer represented by a has been output.


void add (int a[], int sizeA, int b[], int sizeB, int sum[], int& sizeSum);
// Precondition: Parameters a, sizeA, b and sizeB have values.
// Postcondition: The parameter sum contains the sum of the
// integers represented by a and b and sizeSum contains the number of
// digits in the sum. If the sum overflows, a message is output.


// ========================
//      main function
// ========================
int main()
{
   Num(SIZE+1);    


     return 0;
}



Char Num(double theNum[SIZE+1])
{
   cin >> theNum[x, a[SIZE, sizeA];
   for(;(qty<=SIZE)&&(theNum[qty]!='\0'); qty++);

   if(qty==(SIZE+1))
     sizeA=0;
     return;
   a[SIZE]={0};
   sizeA=qty;
   qty--;
   for(int x=0; qty>=0; x++)
   {
      if(isDigit(theNum[qty]))
        a[x]=theNum[qty]-48;
      else
        sizeA=0;
        return;

      qty--;
}

'Char' is not a valid C++ type.

'Num' function does not match its prototype (see lines 23 and 58).

'Num', if ever executed, will never execute lines 66-77; enclose your 'if' statements in braces and you should find the answer.

Match all opening braces ( '{' ) with closing braces ( '}' ).

Should help clear some of your compiler errors.

thanks man is that all i need to do and then im done or what

It appears you still have three functions to code and potential logic errors to debug, but that should get rid of your compiler errors for the moment.

can u help me write those functions cuz i am very lost and dont know where to start

Unsure why I would receive a private message re this problem as I have enough problem with using Visual Basic and I presume your problem is to be solved using C++. Sorry about that.
Secondly it is usually sensible to give some context in explaining the problem, then break it down into simple steps in plain english, then resolve each step by code; but there again I am from the Old Timer's school and still use a flowchart in the resolution of complex mathematical problems. Cheers.

Write out the steps in pseudocode and tell us where you are stuck (since those methods are much of your assignment)

// *************************************************************
      //
      // BigInt.cpp
      //
      // This program adds large integers that are stored in arrays.
      // The digits are stored in the array in reverse order so that
      // addition is easier. The program allows a user to keep doing
      // more additions as long as he/she wishes.
      //
      // *************************************************************
       
      #include <iostream>
      #include <cctype>
       
      using namespace std;
       
      const int SIZE = 20;
       
       
      int digit[SIZE]; // the digits stored in reverse order
      int qty=0;
       
      char Num(int theNum[]);
       
       
      void inputBigInt (int a[], int& sizeA);
      // Precondition: The user is ready to enter an integer
      // Postcondition: The digits of the integer read in are stored
      // in reverse order in the array a and the number of digits is
      // stored in sizeA.
       
       
      void outputBigInt(int a[], int sizeA);
      // Precondition: a is defined
      // Postcondition: The integer represented by a has been output.
              

      void add (int a[], int sizeA, int b[], int sizeB, int sum[], int& sizeSum);
      // Precondition: Parameters a, sizeA, b and sizeB have values.
      // Postcondition: The parameter sum contains the sum of the
      // integers represented by a and b and sizeSum contains the number of
      // digits in the sum. If the sum overflows, a message is output.
       
       
      // =======================
      // main function
      // =======================
      int main()
      {
      int number[SIZE+1];
      int random;
      
      inputBigInt(number,random);

      return 0;
      }
       
      char Num(int theNum[])
      {
      cout << "Enter an integer: ";
      for( (qty<=SIZE)&&(theNum[qty]!='\0'; qty++)
      {
      cin >> theNum[qty];
      }
      }

      void inputBigInt (int a[], int& sizeA)
      {
      Num(number);
      int isdigit;
      sizeA=qty;
      qty--;
      for(int x=0; qty>=0; x++)
      {
      if(isdigit(theNum[qty]))
      a[x]=theNum[qty]-48;
      else
      sizeA=0;
      return -1;
      qty--;
      }
      }
      }

can someone help me fix my errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.