I am trying to write a program to add big numbers using char data. I need help getting this completed.

char k;
int num1[26], num2[26], sum[26];

ifstream inData;
inData.open("bignums.dat");

inData.get(k);

I need to read in the first number:

7777777777

I need to right justify the first number to the end of the array:

7777777777

I need to read in the second number:

8888888888

I need to right justify the second number to the end of the array:

8888888888

I need to add the first number and the second number using a carry and print out the answer right justified:

16666666665

Can anyone help?
Thanks,
Caramel

Edited 6 Years Ago by Nick Evan: Added code-tags

- write a function to read a number string into an array
- write a function to right-justify that number in the array
- write a function to perform long addition.

Now which one is really bugging you?
Perhaps post your attempt at step 1 and we can take it from there.

- write a function to read a number string into an array
- write a function to right-justify that number in the array
- write a function to perform long addition.

Now which one is really bugging you?
Perhaps post your attempt at step 1 and we can take it from there.

I am having a problem right-justifying as well as performing the long addition. I am olnly getting addition for the last two numbers.
Thanks for your response.

One method to right justify shorter string:

Determine the lengths of A and B
Determine which string is longer
Determine the number of zeros to prepend to the shorter string
Create a string of zeros to prepend to the shorter string
Concatenate the string of zeros and the shorter string

Edited 6 Years Ago by Lerner: n/a

Obviously my attempt at getting you to post some problem code failed.

Good luck.

I apologize for taking me so long. I was awaiting a response from my teacher. This is what I have so far.

#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{
    char k, j;
    int i, p, s, sumit, sec=0, first;
    int num1[26], num2[26], sum[26];
    ifstream inData;
    inData.open("bignumbers.dat");

    while(inData)
    {
    inData.get(k);
    inData.get(j);
    while( k > 0)
        {
            for(i = 1; i < 26; i++)
            {
            num1[i] = 0;
            num1[25] = k -'0';
            cout << num1[i];
            }
            break;
        }
    cout << endl;
    inData.get(j);
    while( j > 0)
        {
            for(p = 1; p < 26; p++)
            {
            num2[p] = 0;
            num2[25] = j -'0';
            cout << num2[p];
            }
            break;
        }
    cout << endl;
    cout << "_________________________";
    cout << endl;

            for(i=1,p=1; i < 26, p< 26;i++, p++)
            {
            sumit = num1[i] + num2[p] + sec;
            first = sumit % 10;
            first= sumit/10;
            sum[s] = sec;
            sum[s] = first;
            sum[s] = sumit;
            cout  << sum[s];
            }
            break;
        }
    cout << endl;
    inData.close();
    system("pause");
    return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I apologize for taking me so long. I was awaiting a response from my teacher. This is what I have so far.
/QUOTE]

1. You really need to use code tags. Read the posting rules. It's easier to get help if you follow the posting rules.

2. If you are trying to use char arrays to add "really big numbers", why are you using int arrays? int num1[26] is not a char array! You should probably check the requirements for this assignment. Incidentally, a char is an integer value. It has only 8 bits of data while an int has (usually) 32. So, you can do all your usual integer arithmetic using chars and save 4 times as much memory.

3. You are starting at 1 in your for loops. This means you are skipping over the first element. Arrays in c and c++ start at 0:

for( int [B]i=0[/B]; i<n; i++ ){
    //do something with i
}

4. Your loops are crazy. Sorry, that's the simple way to put it. You need to re-think your logic. You are getting a sigle character from standard input, then you are setting every element of num1 and num2 to 0 except for the first and last element of the array. The first element is being skipped (see problem #3), and the last one is being set to the input value. However, this value gets overwritten as soon as a new value is gotten from the input. Delete your loop structures and start over. This time, think about what is happening at every step. You need to work out the logic before you start pounding out code.

5. Your summing algorithm is not going to work. First of all, you are starting at index 1 again. Bad form. Second, you are using two index variables when you need only one. The i and p variable always equal the same value, so you only need one of these. You have a good notion in using modular arithmetic to do the addtion with a carry. However, the two successive mod and divide overwrite the same value. You need to have a carry variable or sum such and set this to sumit/10, then you need to set sum = sumit%10.

6. When you do this:

sum[s] = sec;
sum[s] = first;
sum[s] = sumit;

You need to understand that only the last statement has any effect because it is overwriting the values stored at the same location. Once again, think about the logic of the loop. Work it out on paper first, then write code. I'm sorry to say, but you need to go back to the drawing board on this one.

This article has been dead for over six months. Start a new discussion instead.