Hi,
I am using a function and passing in the array as a parameter. I would like to then sum the array's elements. Can anyone help me with that?
Thanks! Mindi
Printable View
Hi,
I am using a function and passing in the array as a parameter. I would like to then sum the array's elements. Can anyone help me with that?
Thanks! Mindi
Show us what you have so far.
gg
If you want to pass an array and sum its elements, you have to pass in the size of the array as a separate argument too. This is because you can't find the size of an array within a function.
A lame example involving an array of ints:
Code:#include <iostream>
#include <cstdlib>
//
// Calculates the sum of the elements in an array.
//
int sumOfElements( int array[], int sizeOfArray )
{
int total = 0 ;
for( int i=0; i<sizeOfArray; i++ )
total += array[i] ;
return total ;
}
int main(int argc, char *argv[])
{
int intArray[] = { 1, 2, 3, 4, 5, 6, 7 } ;
int sizeOfArray = sizeof(intArray)/sizeof(intArray[0]) ;
std::cout << "The sum of the elements in the array is " ;
std::cout << sumOfElements( intArray, sizeOfArray ) ;
std::cin.get() ;
return 0;
}
Edit: Nevermind what I said...
You can also pass it this way:
Code:int somefunc ( int* array, int size)
>> Not sure if this is correct, but I think there is a way to determine the number of elements in the array...Maybe something like:
It would not work if the array was passed into the function, and if you're trying that within the function:
The following code does not work (as intended).
Code:#include <iostream>
#include <cstdlib>
//
// Calculates the sum of the elements in an array.
//
int sumOfElements( int array[] )
{
int total = 0 ;
for( int i=0; i<sizeof(array)/sizeof(array[0]); i++ )
total += array[i] ;
return total ;
}
int main(int argc, char *argv[])
{
int intArray[] = { 1, 2, 3, 4, 5, 6, 7 } ;
int sizeOfArray = sizeof(intArray)/sizeof(intArray[0]) ;
std::cout << "The sum of the elements in the array is " ;
std::cout << sumOfElements( intArray ) ;
std::cin.get() ;
return 0;
}
Yeah...After I posted I thought about it and realized that it probably wouldn't work, and then I reread your post...
man i had a prog with the same exact question. ill c if i can find it.
As others have mentioned there is no way to tell the size of an array from a pointer. The normal C convention is to pass the pointer and a size, though you should probably be using size_t rather than int.
int accumulate(const int *array, size_t size);
I recommend you adapt the C++ convention of iterator pairs, one to the start and "one past the end".
This is much more frendly to generic code and allows you to write.Code:int accumulate(const int *first, const int *last);
...
int main() {
int arr[] = {1,3,4,63,2,4,56};
int total = accumulate(arr,&arr[sizeof(arr)/sizeof(arr[0])]);
}
That not only works with arrays but also lists, vectors, sets, and any container class you write that is willing to yarf up a forward iterator.Code:template<class ForwardIterator>
int accumulate(ForwardIterator first, ForwardIterator last) {
return std::accumulate(first,last,0);
}
You can also consider using a sentinel value to indicate the end of the array.
You could also consider using vectors and save yourself a great deal of headache.
Considering his post he is probably new to C++ and I didn't learn about Vectors until my third CS class so Vectors might not be an option for him right now:) Vectors are a lot nicer though.