0

I am writing a C program to calculate Moving Average (MA) of share price. data is available in an array datavalue[count], count= is number of data values. Now i am trying to write up a function for MA.. but i have to pass three arguments in the function as follows

  • int count = number of data values
  • int K = period of moving average
  • array float type datavalue = array containing share pricing.

I have written following code (as part of full code for calculation MA). it is working fine.

float data[count], mag[count];
double avg, sum;

for (i=0; i<=count; i++)
   data[i] = numberArray[i];

for (i=0; i<k-1; i++) {
     mag[i-1] = 0;
     sum = sum + data[i];
}

for (i=k-1; i<=count; i++) {
    mag[i-1] = avg;
    sum = sum + data[i] - data[i-k];
    avg = sum / k;
}

Above code is working fine within single main file. but as i have to reuse MA again and again in my program, thus i decided to make a separate function for MA. i did it as follows

float *mavg(int count, int k, float datavalue)
{
    float *mag = malloc(sizeof(float)*count);
    //float data[count];
    float avg=0, sum=0;
    int i;

    for (i=0; i<=count; i++) {
        for (i=0; i<k-1; i++) {
            mag[i-1] = 0;
            sum = sum + datavalue[i];
        }
    }

    for (i=k-1; i<=count; i++) {
        mag[i-1] = avg;
        sum = sum + datavalue[i] - datavalue[i-k];
        avg = sum/k;
    }
    return mag;
}

BUT above code is not working when calling from main file. While compiling it is showing error

subscripted value is neither array nor pointer nor vector

in following lines :

  • sum = sum + datavalue[i];
  • sum = sum + datavalue[i] - datavalue[i-k];
5
  • 2
    Please explain what “not working” means. Compilation errors? Runtime errors? Wrong results? Which results, what did you expect to get? Commented Dec 16, 2018 at 8:57
  • while compiling it is showing error: subscripted value is neither array nor pointer nor vector ... in sum=sum+datavalue[i]; as well as in sum=sum+datavalue[i]-datavalue[i-k]; Commented Dec 16, 2018 at 9:01
  • datavalue is declared as a single float. You can’t use it as an array. You probably wanted to declare it as a pointer Commented Dec 16, 2018 at 9:02
  • Well it isn't, what is unclear? float datavalue) datavalue[i] Commented Dec 16, 2018 at 9:02
  • Please edit your question to add that helpful information. Commented Dec 16, 2018 at 9:04

1 Answer 1

2

The variable datavalue is not an array. It's defined here as a single float:

float *mavg(int count, int k, float datavalue)
                              ^^^^^
                              That is not an array

Therefore you get compiler error when doing datavalue[i]

Did you mean to do

float *mavg(int count, int k, float* datavalue)
                                  ^

Also this part seems strange:

for(i=0;i<=count;i++)     // Here you use variable i for the loop
    for(i=0;i<k-1;i++)    // and again here you use variable i for the loop
                          // Sure that is what you want?
    {
        mag[i-1]=0;       // When variable i is zero, you index with -1 which is wrong
                          // as you write outside the allocated memory

        sum=sum+datavalue[i];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Poor 'i', the little guy never had a chance... (and if k is less than count, things could go on for a very long time.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.