Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to add the value of all the items and then divide on total number items, where my mistace?

What I have tried:

C
int Whether(int num_years)
{
    int i, sum = 0;
    for (i=0; i
Posted
Updated 12-Dec-22 0:03am
v2

1 solution

We can't see your whole code: just a fraction of it.

But ... if you are meant to take an array and get the average then you need to pass two pieces of information into your function: the array itself, and the number of items in it.
Then it's trivial:
C
int Average(int data[], int len)
   {
   int sum = 0;
   for (int i = 0; i < len; i++)
      {
      sum += data[i];
      }
   return sum / len;
   }
 
Share this answer
 
Comments
CPallini 12-Dec-22 6:09am    
I can see just the whole of your result, not the fraction of it.
:-D
OriginalGriff 12-Dec-22 6:28am    
That's because it's all integers ... :D
CPallini 12-Dec-22 6:49am    
Nobody can argue with an integer man!
OriginalGriff 12-Dec-22 7:02am    
Real men do!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900