Implement a C function that can perform addition of two 15-digit (positive) integers. As in C the
maximum integer number that can be stored in memory is 2147483647 it is not possible to use
primitive data types in order to add very large whole numbers. For this reason, you are required to
represent very long integer numbers using arrays of chars and then implement a function that can
add two such numbers.
Test your function by performing the additions shown below:
459274957162030 + 0027998725610304 = ________________
123450928482350 + 2784910000000000 = ________________

Recommended Answers

All 11 Replies

Oh, homework -- show us how far you got!

i know that i need to use the string as an array of character and convert the character to an integer. i know the code of the string as an array of character but i do not know how to put it in my solution. bdw this is a c++ program. pls help me.

Just write down the code of what you know, send it to us and point us to the point where you get stuck.

#include<stdio.h>
int main()
{
int num1[255], num2[255], sum[255];
char s1[255], s2[255];
int l1, l2;

printf("Enter Number1:");
scanf("%s", &s1);
printf("Enter Number2:");
scanf("%s", &s2);

/* convert character to integer*/

for (l1 = 0; s1[l1] != '\0'; l1++)
num1[l1] = s1[l1] - '0';

for (l2 = 0; s2[l2] != '\0'; l2++)
num2[l2] = s2[l2] - '0';

int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++) {
sum[k] = (num1[i] + num2[j] + carry) % 10;
carry = (num1[i] + num2[j] + carry) / 10;
}
if (l1 > l2) {

while (i >= 0) {
sum[k++] = (num1[i] + carry) % 10;
carry = (num1[i--] + carry) / 10;
}

} else if (l1 < l2) {
while (j >= 0) {
sum[k++] = (num2[j] + carry) % 10;
carry = (num2[j--] + carry) / 10;
}
} else {
if (carry > 0)
sum[k++] = carry;
}


printf("Result:");
for (k--; k >= 0; k--)
printf("%d", sum[k]);

return 0;
}

that is my code...it is not working and I have not use the string as an array of characters as i do not know how. why it is not working? what do I have wrong? how can I include the string as an array of characters pls?

Could you specify what exactly is not working?

all the code that i have posted here....it is telling me that there were build errors..

basiccly it is not working where ever i have k. it is telling me: int k Error: expression must have pointer-to-object type. what can i do? help me pls? i am stuck and cannot find the solution of this problem. I have worked it out for many times I did not get out the restult. with that code in the begining i added this too now:

char num1[15] = {'3', '5', '1', '5', '0', '3', '5', '1', '5', '0', '3', '5', '1', '5', '4'};
char num2[15] = {'1', '2', '3', '4', '5','1', '2', '3', '4', '5', '1', '2', '3', '4', '5'};
int n1, n2;

n1 = num1[14] - '0';
n2 - num2[14] - '0';

printf("%d\n",n1);
printf("%d\n",n2);

I recomend you to use a little endian representation of your string numbers, because it is easy to manage the carry. You can try something like:

string add(const string& n1, const string& n2)
{
  // Add two string numbers in little-endian format (reversed)

  const string& min = n1.size()<n2.size() ? n1 : n2;
  const string& max = n1.size()<n2.size() ? n2 : n1;

  int minsize = min.size();
  int maxsize = max.size();

  string res;
  res.reserve(maxsize+1); // This is for optimization purpose only.

  int c=0;
  for(int j=0; j<minsize; ++j)
    {
      int s = c + max[j] + min[j] - 2*'0';
      c = s/10;
      res += '0' + s%10;
    }
  for(int j=minsize; j<maxsize; ++j)
    {
      int s = c + max[j] - '0';
      c = s/10;
      res += '0' + s%10;
    }
  if(c) res += '0' + c;

  return res;
}

To use this, you need to reverse your string numbers before to call add, and after to get the result.

For example, to add 84 with 456 you must call:
string res = add("48", "654"); giving res == 045.
The finel answer is the reverse of 045 which is 540.
That is 84 + 456 = 540.

ok thank you for your help

Basically, the assignment was to do a small part of a no limits integer library. You need to do the math one digit at a time like a kid, not putting your total number in any integer. You need to start with just the last digit (justify your operands) and add, honoring any operand that is shorter with effective leading zeros. You need a result space that is the longer string length plus 1 for add (e.g., 99+99=198), plus anything you might need for the sign if negative is supported.

Funny thing is, some CPU machine languages can/could do this.

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.