# Thread: Summation of an array using pointers

1. ## Summation of an array using pointers

I need some help getting unstuck on a line of my program.
Please look at the attached file.[CODE]
//***Question: find the average of 125 randomly generated integers between 30 and 40***
//***must display the answer in the following type XX.X and must incorporate pointers***

#include<stdio.h> //printf,scanf
#include<stdlib.h>
#define SIZE 125
#define NUM 11
void random(int *);
void sum(int *);

int main()

{
int array125[SIZE];
random(array125);
sum(array125);

return 0;
}
//****end main
//*****begin random
void random(int *xptr)
{
int count,ran;
for (count=0;count<SIZE;count++){
ran=30+rand()%NUM;
*(xptr+count)=ran;
}
}
//****End Random
//***Begin Sum
void sum(int *aptr)
{
int count;
int summ=0;
for (count=0;count<SIZE;count++)
*(aptr+count)+summ==summ; //this line has a probelm
printf("\n%d\n",summ); //returns a zero value
}
/*The first function random works fine. I am stuck on adding up the values in the array125[SIZE].
I know that I will have to divide the sum by 125. I am just stuck on formallities in the C language
Will a kind soul point me in the right direction?*/

2. 1) Code tags. They are your friend. So is the preview button.
2) random() is already defined. Use my_random()
3) You do realize that you are trying to passing an ARRAY of integers to random()? line 13

3. The line you are having trouble with has two equals signs. This is the equality operator that you would use in an if statement like

Code:
`if (foo==1){do this stuff}`
One equals sign is the assignment operator where you want the left side to be set to equal whatever is on the right side.

Code:
`x=1; //after this line x will actually hold the value 1`
Do you want to use == or =?

4. >> *(aptr+count)+summ==summ; //this line has a probelm

Try

summ = *(aptr+count)+summ;

And just a pointer on the use of functions:

Code:
```int
sum(int array[], int count)
{
int i, amount = 0;

for(i = 0; i < count; ++i)
amount = amount + array[i];

return amount;
}```
Now you can actually use the function in another program, without having to drag that macro 'SIZE' everywhere you go (and having to change it!).

Also, notice the function returns something useful. Then you can even do:

Code:
```int
average(int array[], int count)
{
return sum(array, count) / count;
}```