3

this is the function code:

void statistics(int arr[], int n, int *positive, int *even, int *doubledigit)
{
    int i = 0, countP = 0, countE = 0, countD = 0;

    for(i = 0; i < n; i++)
    {
        if(arr[i] > 0)
            countP++;

        if((arr[i] % 2) == 0)
            countE++;

        if(abs(arr[i]) >= 10 && abs(arr[i]) < 100)
            countD++;
    }

    *positive = countP;
    *even = countE;
    *doubledigit = countD;
}

void main()
{
    //  double mat[size][size];
    int *positive = NULL, *even = NULL, *DoubleDigit = NULL;
    int arr4[] = {1, 3, 5, -45, 8, 8, 60, 800};
    int soa = sizeof(arr4);
    statistics(arr4, soa, &positive, &even, &DoubleDigit);
}

the problem is that the result of the even numbers is 28:

why is it 28?? it should count the even numbers... https://i.stack.imgur.com/dS2us.png

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
efi
  • 53
  • 4
  • `sizeof` counts bytes, not 'number of integers'. So, you are telling the function to examine 32 integers in your 8 integer array. It is reading garbage and coming up with garbage. – Jonathan Leffler May 02 '15 at 15:49
  • See also [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336) – Jonathan Leffler May 02 '15 at 15:52
  • Note that your compiler was warning you about type mismatches (between the function definition and the function call). Pay attention to the compiler; it is trying to help you. At this stage in your career, assume the compiler is right; it knows a lot more about C than you do. Even later on when you've learned a lot more about C, the compiler will be right more often than you are when the two of you disagree about whether some C code is valid. – Jonathan Leffler May 02 '15 at 15:58

4 Answers4

4

First the return type of main() should be int.

Secondly for some reason you are passing the addresses of int pointers (that are initialised to NULL) to your function. Just pass int* parameters to your function like you should be.

Thirdly, sizeof returns the size of the array in bytes. You want to iterate over the number if elements in the array, not the byte count. Therefore you need to divide the byte count by the number of bytes in each element (sizeof(int)).

Try this instead

int main()
{
  int positive =0, even = 0, DoubleDigit = 0;
  int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };
  int soa = sizeof(arr4)/sizeof(int);
  statistics(arr4, soa, &positive, &even, &DoubleDigit);
}
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
3

Memory addresses of some value-buckets are stored in these:

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

You want to declare actual value-buckets where to store the results/values, not the storage for addresses, so change to:

int positive = 0, even = 0, DoubleDigit = 0;

Also, you want the number integers in arr4, so change to:

int soa = sizeof(arr4) / sizeof(int);
plesiv
  • 6,935
  • 3
  • 26
  • 34
1

In your main() function: positive even DoubleDigit has been a pointer. However you pass their address to function statistics().

statistics(arr4, soa, &positive, &even, &DoubleDigit);

is equal to

statistics(int arr[],int soa,int **positive, int **even, int **DoubleDigit);

but you declare it as

statistics(int arr[], int n, int *positive, int *even, int *doubledigit)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
icecity96
  • 1,177
  • 12
  • 26
1

Try the following

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )
{
    int i;

    for ( i = 0; i < n; i++ )
    {
        if ( arr[i] > 0 ) ++*positive;

        if ( arr[i] % 2 == 0 ) ++*even;

        if ( abs( arr[i] ) >= 10 && abs( arr[i] ) < 100 ) ++*doubledigit;
    }
}

int main( void )
{
    int positive = 0, even = 0, DoubleDigit = 0;
    int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };

    int soa = sizeof( arr4 ) / sizeof( *arr4 );
    statistics( arr4, soa, &positive, &even, &DoubleDigit );
}

As for your code then you declared pointers

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

but they do not point to actual memory.

You call the function parsing addresses of the pointers themselves

statistics(arr4, soa, &positive, &even, &DoubleDigit);

that is for example expression &positive has type int ** while the corresponding parameter of the function

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )

is declared as having type int *

Expression sizeof(arr4) yields the size in bytes of array arr4 while you have to pass the numjber of elements in the array.

Function main shall have return type int

int main( void )
{
   //...
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335