Reputation: 29
I've started learning functions in C today. My issue in the function "sum" where I += numbers from arrays, it does write the sum of the numbers but when the program comes out of that "sum" function the integer where I save the sum of these numbers resets to 0 and I can't seem to figure out why. This is the code, I sure do hope that you can help me out.
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void arr(int hello[], int size) {
srand(time(NULL));
for (int i = 0; i < size; i++) {
hello[i] = rand() % 100;
}
}
void print(int hello[], int size) {
for (int i = 0; i < size; i++) {
cout << hello[i] << " ";
}
}
int sum(int hello[], int size, int sumup) {
for (int i = 0; i < size; i++) {
sumup += hello[i];
}
cout << endl;
cout << "Sum of these numbers: " << sumup << endl;
return sumup;
}
int SumEvenOrOdd(int sumup, int size) {
int average = sumup / size;
cout << "Average of the all numbers in array: " << average << endl;
return average;
}
int main() {
bool bye = false;
const int size = 10;
int hello[size], sumup = 0, input;
arr(hello, size);
print(hello, size);
sum(hello, size, sumup);
SumEvenOrOdd(sumup, size);
cin.get();
cin.get();
return 0;
}
Upvotes: 0
Views: 136
Reputation: 123458
The problem is that the formal parameter sumup
in the sum
function is a different object from the actual parameter sumup
in main
, so any changes to sumup
in the function are not reflected in sumup
in main
.
There are several ways to get around this:
sumup
parameter in sum
to be a reference to the sumup
variable in main
:int sum( int hello[], int size, int &sumup )
{
// leave everything else the same.
}
sumup
parameter in sum
to be a pointer to the sumup
variable in main
:int sum( int hello[], int size, int *sumup ) // leading * is necessary
{
...
*sumup += hello[i]; // leading * is necessary
}
int main( )
{
...
sum( hello, size, &sumup ); // leading & is necessary
...
}
sum
back to sumup
or to a different variable:int newsum = sum( hello, size, sumup );
although then the question becomes, why pass sumup
as an argument in the first place? Just declare a local variable within sum
to hold the value, and return that:int sum( int hello[], int size )
{
int result = 0;
for ( int i = 0; i < size; ++i )
result += hello[i];
return result;
}
int main( )
{
...
int sumup = sum( hello, size );
...
}
Upvotes: 2
Reputation: 745
Change your line sum(hello, size, sumup);
to sumup = sum(hello, size, sumup);
. Then your return value will be saved in the variable sumup.
If you don't want to save your sumup variable via the return value, you should learn about call by reference (and how it differs to call by value). Then you could write to the function parameter sumup and change the original value instead of the local copy inside the function.
This will open the can of worms of pointer manipulation. One of the biggest sources of FUN with C ;-)
Example: https://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference
Upvotes: 1
Reputation: 896
The function sum
returns a value without a variable that will accept it.
change this line:
sum(hello, size, sumup);
to:
int my_sum = sum(hello, size, sumup);
Then you will be able to use my_sum in your main.
Another solution can be passing sumup
to your function by reference, just change the decleration to:
void sum(int hello[], int size, int & sumup)
Upvotes: 0
Reputation: 4000
Try this,
sumup = sum(hello, size, sumup);
You should get right sumup value.
Upvotes: 0
Reputation: 708
Pass the function arguments as references so they can be accessed:
void sum(int hello[], int size, int& sizeup)
this will allow sizeup to retain it's value outside of the function since you're passing a reference of sizeup.
Upvotes: 0