-5

I a wrote a C code in Ubuntu 14.04.4 LTS and compiled it. It is working as expected.But in windows it is running but at last in result it is showing some garbage values.

The code I wrote is

#include <stdio.h>
#include <stdlib.h>
struct poly{
    int coe[100];
    };
void mul(struct poly *pr, int a, struct poly *p){
    struct poly res;
    int i;
    for(i = 0; i < 100; i++){
        res.coe[i] = 0;
        int j;
        for(j = 0; j <= i; j++){
            res.coe[i] += ((*pr).coe[j])*((*(p + a)).coe[i - j]);
        }
    }
    *pr = res;

}
void main(){
    struct poly *p;
    p = (struct poly *)malloc(100*sizeof(struct poly));
    int n;
    printf("no. of poly :");
    scanf("%d", &n);
    int i; int max = 0;
    for(i = 0; i < n; i++){
        int de;
        printf("deg? of %d:", i+1);
        scanf("%d", &de); max += de;
        int j;
        for(j = 0; j <= de; j++){
            printf("co-eff of deg %d:", j);
            scanf("%d", &p[i].coe[j]);
        }
    }
    struct poly res;
    struct poly *pr;
    res = p[0];
    pr = &res;
    int fi;
    for(fi = 1; fi < n; fi++){
        mul(&res, fi, p);
    }
    for(i = 0; i < (max + 1); i++){
        printf("%d\n", res.coe[i]);
    }
}

and the result in windows is

C:\Users\Sai\Documents\C++>gcc ac.c -o ac

C:\Users\Sai\Documents\C++>ac
no. of poly :3
deg? of 1:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 2:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 3:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
1
3
6
85067032
255201082
510403447
-1897503563

C:\Users\Sai\Documents\C++>

the result in ubuntu is

sai@sai-Inspiron-7548:~$ gcc ac.c -o ac
sai@sai-Inspiron-7548:~$ ./ac
no. of poly :3
deg? of 1:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 2:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 3:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
1
3
6
7
6
3
1
sai@sai-Inspiron-7548:~$ 

How to make this program run correctly in windows 10.

Is something wrong in code that I wrote.

Thank you.

closed as off-topic by Florian Diesch, Thomas Ward, Terrance, andrew.46, Pilot6 Apr 17 '16 at 8:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Florian Diesch, Thomas Ward, Terrance, andrew.46, Pilot6
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I think you would be better off with your question here – Videonauth Apr 17 '16 at 0:50
1

While this should have been posted elsewhere, this is the solution:

Always initialize!

You are not initializing the coe[100] array of all the elements in your struct poly*.

In your loops you only set some of the entries, however your mul-function also accesses uninitialized ones.

I assume that gcc on linux somehow takes care of that by intilializing them as 0 by default, however the standard defines the value of an uninitialized integer as undefined.

MinGW does not intitialize it for you, so whatever is in the memory at that time messes up your results.

Edit: Of course you should also free the allocated memory with free(p);!

Here is how you can fix it:

#include <stdio.h>
#include <stdlib.h>
struct poly{
    int coe[100];
};
void mul(struct poly *pr, int a, struct poly *p){
    struct poly res;
    int i;
    for(i = 0; i < 100; i++){
        res.coe[i] = 0;
        int j;
        for(j = 0; j <= i; j++){
            res.coe[i] += ((*pr).coe[j])*((*(p + a)).coe[i - j]);
        }
    }
    *pr = res;

}
void main(){
    struct poly *p;
    p = (struct poly *)malloc(100*sizeof(struct poly));
    for(int k = 0; k < 100; k++)
        for(int l = 0; l < 100; l++)
            p[k].coe[l] = 0;
    int n;
    printf("no. of poly :");
    scanf("%d", &n);
    int i; int max = 0;
    for(i = 0; i < n; i++){
        int de;
        printf("deg? of %d:", i+1);
        scanf("%d", &de); max += de;
        int j;
        for(j = 0; j <= de; j++){
            printf("co-eff of deg %d:", j);
            scanf("%d", &p[i].coe[j]);
        }
    }
    struct poly res;
    struct poly *pr;
    res = p[0];
    pr = &res;
    int fi;
    for(fi = 1; fi < n; fi++){
        mul(&res, fi, p);
    }
    for(i = 0; i < (max + 1); i++){
        printf("%d\n", res.coe[i]);
    }
    free(p);
}
  • Not to speak of the serious memory leak profided with this programm (hint malloc without freeing the resource later on) – Videonauth Apr 17 '16 at 0:49
  • Thanks, totally forgot to mention that. Added it to my answer. – clocktown Apr 17 '16 at 1:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.