1. ## coding help? (functions)

i wrote up a program using functions to calculate a sum and product. I looked it over and it looks right to me, but im not getting the desired values...

Code:
```#include <stdio.h>
#define MAX_ELEMS 100

void SumAndProduct (double[], int, double, double);

int main()
{
double arr[MAX_ELEMS];
int count = 0;
double val, sum, product;
printf("Enter array values (or 0.0 to stop): ");
do
{
scanf("%lf", &val);
if(val!=0.0)
{
arr[count]=val;
count++;
}
} while( val != 0.0 && count < MAX_ELEMS);
SumAndProduct(arr, count , sum, product);
printf("The sum is %f and the product is %f. \n", sum, product);
return 0;
}

void SumAndProduct( double a[], int len, double sum, double prod)
{
int i;
prod = 1.0;
sum = 0.0;
for(i=0; i< len;i++)
{
sum = sum + a[i];
prod = prod * a[i];
}
}```
not really sure where i went wrong, thanks for the help.

2. I was writing up a reply but I ended up losing it.

Your program logic is correct, but the technical details regarding function parameters are what is causing the incorrect output.

When you pass a variable to a function, only a copy of the variable is passed. You are really just transferring the value of the variable, not the variable itself. This means if you pass an int to a function with a value of 2, your function will receive a new variable with the value of 2. You can change the variable inside the function, but the variable in the calling function will not be changed.

To remedy this, you have to use pointers. Pass the address of the variable that you want to change to the function in question. Define the function to receive a pointer instead of a regular variable. Alter the value that the pointer points to.

So like this:

Code:
```#include <stdio.h>
#define MAX_ELEMS 100

void SumAndProduct (double[], int, double *, double *);

int main()
{
double arr[MAX_ELEMS];
int count = 0;
double val, sum, product;
printf("Enter array values (or 0.0 to stop): ");
do
{
scanf("%lf", &val);
if(val!=0.0)
{
arr[count]=val;
count++;
}
} while( val != 0.0 && count < MAX_ELEMS);
SumAndProduct(arr, count , &sum, &product);
printf("The sum is %f and the product is %f. \n", sum, product);
return 0;
}

void SumAndProduct( double a[], int len, double *sum, double *prod)
{
int i;
*prod = 1.0;
*sum = 0.0;
for(i=0; i< len;i++)
{
*sum = *sum + a[i];
*prod = *prod * a[i];
}
}```

3. hey thanks alot! learn something new everyday haha