14 Replies - 313 Views - Last Post: 30 January 2013 - 07:20 AM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

View PostgraFight, 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;
     }


Was This Post Helpful? 0
  • +
  • -

#4 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • 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...
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 10-August 12

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

Posted 29 January 2013 - 05:48 PM

View PostCTphpnwb, 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.

View PostAdak, 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
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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]?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 10-August 12

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

Posted 29 January 2013 - 05:57 PM

View PostAdak, 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.

View PostCTphpnwb, 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
Was This Post Helpful? 0
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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

Was This Post Helpful? 2
  • +
  • -

#13 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

Thanks, jjl! Correction made.
Was This Post Helpful? 0
  • +
  • -

#14 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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

Was This Post Helpful? 0
  • +
  • -

#15 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1