0

Array is defined in the code and we should count the number of positive numbers in the array and create a new array and insert all the positive numbers in it if the positive numbers in the original array is bigger than the negative numbers in the original array and if if the oppisite (negative numbers bigger than positive numbers we create a new array and insert all the negative numbers )

and if its equal (postive numbers and negative numbers ) we create a new array and insert first all the postive number and then all the negative numbers

*we must create the array in the function with 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);
}
Toon Casteele
  • 2,479
  • 15
  • 25
  • 1
    Is that a typo for `mat` in the declaration? It should be `*mat` when you declare it so that it is a pointer. – Shafik Yaghmour May 21 '13 at 13:02
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind May 21 '13 at 13:08
  • you have got so many problems in your code , like malloc return is assigned to a integer, array size in main is not defined.Please explain if it's not typo. – Dayal rai May 21 '13 at 13:09
  • If you compile with warnings enabled (`-Wall` for gcc; `/W4` for MSVC) the compiler will point out the various errors in the code – simonc May 21 '13 at 13:10
  • The code is a mess. The algorithm you use doesn't do what you want and there are several bugs in it. I would scrap it all and start over. – Klas Lindbäck May 21 '13 at 13:12
  • ask compiler and debugger before asking us – kotlomoy May 21 '13 at 13:56

3 Answers3

0

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

I hope it's the good solution, good 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);
for (int i = 0; i < size; ++i)
    printf("%d ", mat[i]);
printf("\n");
free(mat);
}
Liviu
  • 1,859
  • 2
  • 22
  • 48
0
#include <stdio.h>
#include <stdlib.h>

#define N 10

int *func(int *arr, int *size){//arr: input array, size : size of array, I/O
    int *ap, *an, *mat;//array positive, array negative, return value;
    int psize, nsize;//size of positive array, size of negative array 
    int i;

    ap = (int*)malloc(*size * sizeof(int));//check return value, omission
    an = (int*)malloc(*size * sizeof(int));
    psize = nsize = 0;
    for(i=0;i < *size;++i){
        if(arr[i]>=0){
            ap[psize++] = arr[i];
        } else {
            an[nsize++] = arr[i];
        }
    }
    if(psize > nsize){
        mat = realloc(ap, sizeof(int)*psize);
        *size = psize;
        free(an);
    } else if(psize < nsize){
        mat = realloc(an, sizeof(int)*nsize);
        *size = nsize;
        free(ap);
    } else {
        mat = ap;
        for(i=0;i<nsize;++i)
            mat[psize + i] = an[i];
        //*size = *size
        free(an);
    }
    return mat;
}

int main(void){
    int array[N] = {6,3,5,-5,4,3,-6,-9,6,-16};
    int *arr, size = N;
    int i;

    arr=func(array, &size);//need size of the returned array

    for(i=0;i<size;++i){
        printf("%d ", arr[i]);
    }
    free(arr);

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

This program has 2 most common programming errors.

1.) mat=(int *)malloc(c1*sizeof(int));

mat is declared as an integer. malloc returns a void pointer to the memory allocated and you type casted it to integer pointer. Hence mat should be pointer to integer instead just integer. The declaration of mat should be as below.

int *mat;

2.) arr++;

Use array index to access the array rather than array arithmetic operation.

With the array arithmetic arr will be pointing to the end of array. Next when you increment arr, it is trying access out of array bounds that may caused the crash.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
77venki77
  • 1
  • 1