简体   繁体   中英

Last element of array changes itself to first element of another array

So the problem statement is to write a code to find minimum product sum of two arrays, ie, multiply the values in the same index and add them. And the elements in the first array can be changed a maximum of k times as +2 or -2.

My code is:

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

long diffn(long i,long k,long *a,long *b)
    {
        long val1 , val2 ;
        val1 = (a[i]-(2*k))*b[i] ;
        val2 = a[i] * b[i] ;
        return(val2-val1);
    }

long diffp(long i,long k,long *a,long *b)
    {
        long val1 , val2 ;
        val1 = (a[i]+(2*k))*b[i] ;
        val2 = a[i] * b[i] ;
        return(val2-val1);
    }

int main()
    {
        long n , k , i , mi , val , pn , *a , *b , flag ;
        a = (long*)malloc(n*sizeof(long));
        b = (long*)malloc(n*sizeof(long));
        scanf("%li %li",&n,&k);
        for(i=0;i<n;i++)
            scanf("%li",&a[i]);
        for(i=0;i<n;i++)
            {
                scanf("%li",&b[i]);
                flag = 0 ;
                if(i==0)
                    {
                        mi = i ;
                        if(a[i]*b[i]>0)
                            pn = 1 ;
                        else
                            pn = -1 ;
                    }
                else
                    {
                        if((diffp(i,k,a,b)>diffn(i,k,a,b))&&(pn==1)&&(diffp(i,k,a,b)>diffp(mi,k,a,b))&&(flag==0))
                            {
                                mi = i ;
                                pn = 1 ;
                                flag = 1 ;
                            }
                        if((diffp(i,k,a,b)>diffn(i,k,a,b))&&(pn==-1)&&(diffp(i,k,a,b)>diffn(mi,k,a,b))&&(flag==0))
                            {
                                mi = i ;
                                pn = 1 ;
                                flag = 1 ;
                            }
                        if((diffp(i,k,a,b)<diffn(i,k,a,b))&&(pn==1)&&(diffn(i,k,a,b)>diffp(mi,k,a,b))&&(flag==0))
                            {
                                mi = i ;
                                pn = -1 ;
                                flag = 1 ;                              
                            }
                        if((diffp(i,k,a,b)<diffn(i,k,a,b))&&(pn==-1)&&(diffn(i,k,a,b)>diffn(mi,k,a,b))&&(flag==0))
                            {
                                mi = i ;
                                pn = -1 ;
                                flag = 1 ;                              
                            }
                    }
            }
        printf("mi = %ld",mi);
        printf("\na : ");
        for(i=0;i<n;i++)
            printf("%ld ",a[i]);
        printf("\nb : ");
        for(i=0;i<n;i++)
            printf("%ld ",b[i]);
        if(pn==-1)
            a[mi] = a[mi] - (2*k) ;
        else
            a[mi] = a[mi] + (2*k) ;
        val = 0 ;
        printf("\na : ");
        for(i=0;i<n;i++)
            printf("%ld ",a[i]);
        printf("\nb : ");
        for(i=0;i<n;i++)
            printf("%ld ",b[i]);
        for(i=0;i<n;i++)
            val = val + (a[i]*b[i]);                                    
        printf("\nval=%ld",val);                    
        return 0 ;
    }

Now, this is completely random. I sometimes get a segmentation fault after the first array, otherwise the program runs and the last element of the array becomes the first of the next. I just don't get where I'm going wrong.

Please help!

This is the output, and I haven't changed the code between runs at all

divya@divya-Aspire-V3-574G:~$ ./a.out
5 3
2 3 4 5 4
Segmentation fault (core dumped)


divya@divya-Aspire-V3-574G:~$ ./a.out
5 3
2 3 4 5 4
3 4 2 3 2
mi = 1
a : 2 3 4 5 3 
b : 3 4 2 3 2 
a : 2 -3 4 5 3 
b : 3 4 2 3 2 
val=23

You might think about the order of this sequence again:

long n , k , i , mi , val , pn , *a , *b , flag ;
a = (long*)malloc(n*sizeof(long));
b = (long*)malloc(n*sizeof(long));
scanf("%li %li",&n,&k);

You use uninitialized variable n to allocate the memory and then you ask for input.

It's no surprise that you mess up array boundaries...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM