Basic Arrays: Average, Stddev, Bubblesort

Page 1 of 1

5 Replies - 426 Views - Last Post: 17 October 2012 - 02:34 PMRate 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=296025&amp;s=c50473c91932f9d1f94db5be90d84165&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 smilesthunder

Reputation: 0
• Posts: 20
• Joined: 15-October 12

Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 01:03 PM

Hello all,

To get familiarity with arrays in C I'm trying to write a simple program to calculate average, standard deviation, and bubblesort from some numbers passed into an array. Yet I keep running into the same error in my average and stddev functions... It says "practice.c:X: error: lvalue required as left operand of assignment" AND
"practice.c:X: error: incompatible types when returning type ‘double (*)(int *, int)’ but ‘double’ was expected"
I feel that this is a very simple problem, but I am stumped.

Thanks!

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

const int SIZE = 10;

//average
double average(int x[], int SIZE)
{
double total = 0.0;
int i;

for (i=0; i < SIZE; i++)
{
total += x[i];
}
average = total / (SIZE-1);

return average;
}

//standard deviation
double stddev(int x[], int SIZE, double average)
{
double total = 0.0;
int i;

for(i=0; i < 100; i++)
{
total += (average - x[i]) * (average - x[i]);
}

stddev = sqrt(total/(SIZE-1));

return stddev;
}

//bubblesort
void bubblesort(int x[], int SIZE)
{
int i;
int j;
int k;

for(i = 0; i < (SIZE - 1); i++){
for(j = 0; j < (SIZE -1); j++){
if(x[j] > x[j + 1]){
k = x[j];
x[j] = x[j+1];
x[j+1] = k;
}
}
printf("%d", x[i]);
}

int main(void)
{
int i = 0;

for(i = 0; i <= 10; i++)
{
scanf("%d", &x[i]);
printf("%d\n", x[i]);
}
}
```

Is This A Good Question/Topic? 0

Replies To: Basic Arrays: Average, Stddev, Bubblesort

#2 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,012
• Joined: 20-March 10

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 01:24 PM

You need to learn about functions first,

http://www.dreaminco...t-i-the-basics/

http://www.dreaminco...ing-and-arrays/

If you still have questions after that...

Well...repost..

Best Wishes

Snoopy.

#3 nuclearfroggy

Reputation: 82
• Posts: 167
• Joined: 04-August 08

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 01:30 PM

Your problem is coming from a bit of a misconception. In short, the error says that you're trying to return a function pointer when it expected a double. This might be a bit beyond what you've done, but the important bit is the fact you've declared your function "double average" doesn't mean you've got a double variable called "average". You've got a function (called "average") that returns a double value.

The simple fix is to create a variable to store the value and return that. For example, for the average function this should work:
```//average
double average(int x[], int SIZE)
{
double avg, total = 0.0;
int i;

for (i=0; i < SIZE; i++)
{
total += x[i];
}
avg = total / (SIZE-1);

return avg;
}
```

Hope you've seen where you went wrong, if you need any clarification please say!

#4 smilesthunder

Reputation: 0
• Posts: 20
• Joined: 15-October 12

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 02:05 PM

Thank you, both of you have clarified my initial problem. I appreciate it.

Now it appears it doesn't like something with my const int SIZE... I made the change you recommended in average function and in stddev before my for loop I put
```double avg = average(x[i], SIZE);
```

Now I get some error regarding pointers...
practice.c: In function ‘stddev’:
practice.c:35: warning: passing argument 1 of ‘average’ makes pointer from integer without a cast
practice.c:13: note: expected ‘int *’ but argument is of type ‘int’

#5 nuclearfroggy

Reputation: 82
• Posts: 167
• Joined: 04-August 08

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 02:14 PM

Okay, this is because when you say x[i] you're saying the ith element of the array x. You're then passing this to average(), but average() doesn't want an integer, it wants an array of integers. What you meant was:
``` double avg = average(x, SIZE);
```

By just using the array name you pass a pointer to the first element in the array. You should also check out some tutorials on pointers and arrays while you're at it.

#6 smilesthunder

Reputation: 0
• Posts: 20
• Joined: 15-October 12

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 02:34 PM

Ahh okay, I get it. Thanks!!