5 Replies - 276 Views - Last Post: 17 October 2012 - 02:34 PM Rate Topic: -----

#1 smilesthunder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 775
  • View blog
  • Posts: 2,294
  • Joined: 20-March 10

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 01:24 PM

You need to learn about functions first,

Take these Tutorials by JackOfAllTrades

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.
Was This Post Helpful? 2
  • +
  • -

#3 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • 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!
Was This Post Helpful? 1
  • +
  • -

#4 smilesthunder  Icon User is offline

  • New D.I.C Head

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

#5 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

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

#6 smilesthunder  Icon User is offline

  • New D.I.C Head

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

Re: Basic Arrays: Average, Stddev, Bubblesort

Posted 17 October 2012 - 02:34 PM

Ahh okay, I get it. :bananaman: Thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1