trying to make a function that returns average of an array

Page 1 of 1

14 Replies - 521 Views - Last Post: 30 January 2013 - 07:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310190&amp;s=2de9200b6b4d01365e9981271c7323a7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 graFight

Reputation: 1
• Posts: 125
• Joined: 10-August 12

trying to make a function that returns average of an array

Posted 29 January 2013 - 05:21 PM

I feel like i'm doing the actual function right but i'm not sure how to actually call it to see if it is right

I keep getting an error on the call line.

```#include <stdio.h>
#include <stdlib.h>

double arrayAvg(double D[], int len);

int main()
{
double arr[4] = {1.0, 2.0, 3.0, 4.0};

double value = arrayAvg(arr[], 4);

printf("%f\n", value);

system("PAUSE");
return 0;

}

double arrayAvg(double D[], int len)
{
double sum;
double average;

for(int i=0; i <= len; i++)
{
sum += [i];
}

average = sum / (double) len;
return average;
}

```

This post has been edited by graFight: 29 January 2013 - 05:22 PM

Is This A Good Question/Topic? 0

Replies To: trying to make a function that returns average of an array

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11787
• Posts: 44,293
• Joined: 27-December 08

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:30 PM

Call arrayAvg(arr, 4);, removing the brackets around arr when invoking the function.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:35 PM

graFight, on 29 January 2013 - 05:21 PM, said:

I feel like i'm doing the actual function right but i'm not sure how to actually call it to see if it is right

I keep getting an error on the call line.

```#include <stdio.h>
#include <stdlib.h>

double arrayAvg(double D[], int len);

int main()
{
double arr[4] = {1.0, 2.0, 3.0, 4.0};

double value = arrayAvg(arr, 4);

printf("%f\n", value);

system("PAUSE");
return 0;

}

double arrayAvg(double D[], int len)
{
double sum=0.0;
double average;

for(int i=0; i <= len; i++)
{
//sum += [i];
sum+=i;
}

average = sum / (double) len;
return average;
}

```

#4 graFight

Reputation: 1
• Posts: 125
• Joined: 10-August 12

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:43 PM

ok so now that i switched it to just arr that part is working but now i get an error with sum += [i] and then i tried sum += i but that will not give the correct results...

#5 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• Joined: 08-August 08

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:45 PM

Just what do you think [i] is??? Why aren't you using the array?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:46 PM

Let me guess - you forgot:

```double sum=0.0;

```

If you want to average the array, then don't just average the index!

This post has been edited by Adak: 29 January 2013 - 05:48 PM

#7 graFight

Reputation: 1
• Posts: 125
• Joined: 10-August 12

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:48 PM

CTphpnwb, on 29 January 2013 - 05:45 PM, said:

Just what do you think [i] is??? Why aren't you using the array?

i'm sorry i also tried D[i] and still gives me a wrong answer.

Adak, on 29 January 2013 - 05:46 PM, said:

Let me guess - you forgot:

```double sum=0.0;

```

actually yes but still does not fix the new issue lol

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:53 PM

It works fine for me. Did you change sum+=i to sum+=D[i]?

#9 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• Joined: 08-August 08

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:55 PM

I get 2.500000 and that seems right to me.

Using D[i] by the way.

#10 graFight

Reputation: 1
• Posts: 125
• Joined: 10-August 12

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 05:57 PM

Adak, on 29 January 2013 - 05:53 PM, said:

It works fine for me. Did you change sum+=i to sum+=D[i]?

ya, thats when it gave me a garbage number.

CTphpnwb, on 29 January 2013 - 05:55 PM, said:

I get 2.500000 and that seems right to me.

Using D[i] by the way.

Hmm...idk why mine is showing some weird number then

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 29 January 2013 - 08:46 PM

You either have a copy error, or you made some changes manually to your program, but didn't make all the changes that you should have made.

Here's your program fully working - copy and paste all of it, and run it. Note that I have extended the array size to FIVE, instead of four, to make the average more obvious to me.

```#include <stdio.h>
#include <stdlib.h>

double arrayAvg(double D[], int len);

int main()
{
double arr[5] = {10.0, 9.0, 8.00, 11.0, 12.0};

double value = arrayAvg(arr,5);

printf("%f\n", value);

system("PAUSE");
return 0;

}

double arrayAvg(double D[], int len)
{
double sum=0.0;
double average;

for(int i=0; i < len; i++)  //correction! (thanks jjl! )
{
sum += D[i];
}

average = sum / (double) len;
return average;
}

```

I suggest you change your indentation value from 7 chars wide, to either 3,4,or 5 chars wide.

This post has been edited by Adak: 30 January 2013 - 02:07 AM

#12 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

Re: trying to make a function that returns average of an array

Posted 30 January 2013 - 01:03 AM

Quote

Here's your program fully working - copy and paste all of it, and run it.

Your program looks outsides the bounds of the array in your for loop.

This post has been edited by jjl: 30 January 2013 - 01:03 AM

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 30 January 2013 - 02:08 AM

#14 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Re: trying to make a function that returns average of an array

Posted 30 January 2013 - 02:30 AM

Well, don't you think that 'i' should be declared outside the for loop to be in accordance with C99 mode.

something like this.

``` int i;
for( i=0; i < len; i++)  //correction! (thanks jjl! )
{
sum += D[i];
}
```

regards,
Raghav

This post has been edited by raghav.naganathan: 30 January 2013 - 02:32 AM

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: trying to make a function that returns average of an array

Posted 30 January 2013 - 07:20 AM

No. It is working just fine.

I prefer the variable be declared once at the top of the function, but this works for me, as well.

If your compiler is giving out errors because of it, then you have a problem, for sure, and should change where i is declared.

This post has been edited by Adak: 30 January 2013 - 07:22 AM