c - Output Crash in array -


array defined in code , should count number of positive numbers in array , create new array , insert positive numbers in if positive numbers in original array bigger negative numbers in original array , if if oppisite (negative numbers bigger positive numbers create new array , insert negative numbers )

and if equal (postive numbers , negative numbers ) create new array , insert first postive number , negative numbers

*we must create array in function malloc..

the code :

#include <stdio.h> #define n 10 void func(int *arr) {     int i,j,c1=0,c2=0,mat;     for(i=0;i<n;i++)     {         if (*arr>=0) c1++;         else c2++;         arr++;     }     if(c1>c2)     {         mat=(int *)malloc(c1*sizeof(int));         for(i=0;i<c1;i++)         if(*arr>0)         {          mat=*arr;          mat++;          arr++;         }     }         else   if(c2>c1)      {         mat=(int *)malloc(c2*sizeof(int));         for(i=0;i<c2;i++)         if(*arr<0)         {          mat=*arr;          mat++;          arr++;         }     }     else     {         mat=(int *)malloc((c1+c2)*sizeof(int));         for(i=0;i<n;i++)         if(*arr>0)         {          mat=*arr;          mat++;          arr++;         }         for(i=0;i<n;i++)         if(*arr<0)         {          mat=*arr;          mat++;          arr++;         }     } } main() {     int array={6,3,5,-5,4,3,-6,-9,6,-16};     int *arr=array;     func(arr); } 

i think it's working now, following modifications (while trying keep whole idea): 1. mat int* instead of int, returned , freed 2. indexed access array instead of pointer arithmetics (and losing arr pointer, example) 3. more test cases.

i hope it's solution, luck !

#include <stdio.h> #include <malloc.h>  #define n 10 int* func(int *arr, int* size) { int i,j=0,c1=0,c2=0; int* mat; for(i=0;i<n;i++) {     if (arr[i]>=0) c1++;     else c2++;     //arr++; } if(c1>c2) {     *size = c1;     mat=(int *)malloc(c1*sizeof(int));     for(i=0;i<n;i++)     if(arr[i]>=0)     {      mat[j]=arr[i];      //mat++;      //arr++;      j++;     } }     else   if(c2>c1)  {      *size = c2;     mat=(int *)malloc(c2*sizeof(int));     for(i=0;i<n;i++)     if(arr[i]<0)     {      mat[j]=arr[i];      //mat++;      //arr++;      j++;     } } else {     *size = c1+c2;     mat=(int *)malloc((c1+c2)*sizeof(int));     for(i=0;i<n;i++)     if(arr[i]>=0)     {      mat[j]=arr[i];      //mat++;      //arr++;      j++;     }     for(i=0;i<n;i++)     if(arr[i]<0)     {      mat[j]=arr[i];      //mat++;      //arr++;      j++;     } }      return mat; } void main() { //int array[n]={6,3,5,-5,4,3,-6,-9,6,-16}; //int array[n]={6,-2,-5,4,3,-6,-9,6,-16, -1}; int array[n]={-6,3,5,-5,4,3,-6,-9,6,-16}; int *arr=array; int size; int* mat = func(arr, &size); (int = 0; < size; ++i)     printf("%d ", mat[i]); printf("\n"); free(mat); } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -