tags:

views:

463

answers:

5

In C++ I want to add two 50-digit numbers. I use an array to keep each. It means that I want to add two arrays. My problem is that I want to do this in a function named AddNum() and pass the result to another function named WriteNum for printing and I don't know how to pass an array returned by one function to another function. hope that my question was clear enough thanx all

+9  A: 

Don't use arrays. Look up the C++ std::vector class in your text book or help system and use that instead. It will make life much easier for you.

anon
+3  A: 

Don't return an array from the addition function - instead make it return void and pass the array for storing the result by reference or pointer. Then you will pass that array to the function for printing.

sharptooth
A: 

Just to clarify the answer of sharptooth: if you want to return a new array, you have to allocate it in the function and someone else will have to free it. It can be done but you will always have to remember to free the result after calling the function. As Neil Butterworth points out: std::vector helps you solve this.

stefaanv
A: 

if i get it right, and if this is what you wanna do, your functions should have signatures like that:

int* addNumbers(int* array1, int* array2)
{
....
}

and

void writeNumbers(int *array3)
{
....
}

and you can call it like:

//declare and init array1 and array2
//...
writeNumbers(addNumbers(array1, array2));

i hope i understood your question correctly.

emredog
but the more correct way to do it is to pass the third array as a reference, IMO.
emredog
You're going to need to pass in array lengths.
Seth Johnson
+1  A: 

First of all, if you will be adding a lot of numbers, saving them in arrays is troublesome and takes both cpu power and memory. If you can, use GMP (optimized and fast library).

If you must use arrays, then use c++'s vectors instead of c's arrays which will minimize the chance for error and make it simpler.

To send a vector to a function, you do it normally as with int's, string's etc, namely

vector<int> number1;
vector<int> number2;

addnum(number1, number2);

where addnum is defined as :

void addnum(vector<int> a, vector<int> b)

This will copy the first and the second vector array that you have into variables a and b. It is recommended that you send a reference to the addnum in order skip to copy the vectors all the time. This can be done by changing the addnum definition to :

void addnum(vector<int>& a, vector>int>& b)

and then performing normal operation on a and b as usual.

To have addnum return a vector you need to change the definition of addnum to

vector<int> addnum(vector<int>& a, vector>int>& b)

and of course have the return statement with the vector you want to return.

If you choose to send the values by references, that means that the number1 and number2 vectors declared in you main class will also change if you change them in the addnum function. That basically means that, if you save the result in variable a in the addnum function, you will have that same value in the number1 vector, meaning you don't need the function to return a new vector but can instead reuse the existing ones.

Milan