-10
#include <stdio.h>
#include <stdlib.h>

int main() {

    int *a, *x;
    int n, k;
    int i;                              //index
    int sum=0, avg=0;

    a = (int*)malloc(sizeof(int)*n);
    if(a==NULL)
        printf("error\n");
    x = (int*)malloc(sizeof(int)*n);
    if(x==NULL)
       printf("error\n");

    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    sum=0;
    avg=0;


    for(i=0; i<k; i++)
    {
        sum=sum+a[i];
        avg=sum/(i+1)+0.5;
        x[i]=avg;
    }
    for(i=k; i<n; i++)
    {
        sum=sum-a[i-3]+a[i];
        avg=sum/3+0.5;
        x[i]=avg;
    }
    for(i=0; i<n; i++)
    {
        printf("%d", x[i]);
    }
    free(a);
    free(x);

    return 0;
}

I want to make moving average code in C. and I made the code but it has still error :( I think I broke the basic rule in C but I don't know what it is ... Plz help me Let me show you what I want to make

Input example

6 3                                                   
1 3 2 10 6 8

Output

1 2 2 5 6 8

Input example 2

9 4                                
2 7 4 5 6 8 2 8 13

Output example

2 5 4 5 6 6 5 6 8
abelenky
  • 63,815
  • 23
  • 109
  • 159
Jiho Park
  • 1
  • 2
  • 4
    Can you please elaborate on the "error" you get. Is it a build error? Wrong results? Crashes? Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Mar 26 '18 at 06:56
  • 3
    I also recommend you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Mar 26 '18 at 06:56
  • Your example inputs appear to be intended to accept values for both n and k; you have no code to accept these values and are then using initialised values. There is no "basic rule of C" that you have broken - it compiles, so that cannot be true. You may have other semantic errors and examples of ill-advised coding practice, and a poorly designed algorithm, but that is a different matter. The compiler will save you from the "basic rule" errors by not compiling and reporting errors. – Clifford Mar 26 '18 at 09:03

1 Answers1

2

The primary problem is in

  a = (int*)malloc(sizeof(int)*n);

where n is unitialized. It contains indeterminate value. The behavior is undefined.

Quoting C11, chapter §6.3.2.1,

[...] If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

That said, please see this discussion on why not to cast the return value of malloc() and family in C...

That said, there are couple of more things

  • Take care (beware) of uninitialized variables, they are source of many problems. Initialize or assign them before they are used. Turn up your compiler warning settings, they will help you out.

  • int main() better be int main(void) for a hosted environment, unless you have a specialized environment/ compiler which has explicit support for int main().

  • On the NULL check, in case of failure, only printing the error is not sufficient, you should also discontinue the program.

  • printf("some_string"); is not a good practice. If you do not have to have a conversion specification required, go with puts().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261