Account in c (visual studio)
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 even numbers ... http://i.stack.imgur.com/dS2us.png
source to share
The return type main()
must first be int
.
Second, for some reason, you are passing the addresses of int
pointers (which are NULL initialized) to your function. Just pass parameters int*
to your function as it should.
Third, it sizeof
returns the size of the array in bytes. You want to iterate over a number if the elements are in the array, not the number of bytes. Therefore, you need to divide the number of bytes 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);
}
source to share
Memory contains memory addresses of some buckets of values:
int *positive = NULL, *even = NULL, *DoubleDigit = NULL;
You want to declare actual slave values where to store results / values, not storage for addresses, so change to:
int positive = 0, even = 0, DoubleDigit = 0;
Also, you want integers in arr4
, so change to:
int soa = sizeof(arr4) / sizeof(int);
source to share
In your function main()
: positive
even
DoubleDigit
was a pointer. However, you are passing your address to the function statistics()
.
statistics(arr4, soa, &positive, &even, &DoubleDigit);
equally
statistics(int arr[],int soa,int **positive, int **even, int **DoubleDigit);
but you are declaring it as
statistics(int arr[], int n, int *positive, int *even, int *doubledigit)
source to share
Try to run
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 );
}
Regarding your code then you provided pointers
int *positive = NULL, *even = NULL, *DoubleDigit = NULL;
but they don't point to actual memory.
You call the function to parse the addresses of the pointers themselves
statistics(arr4, soa, &positive, &even, &DoubleDigit);
that is, for example, the expression &positive
has a type int **
, and the corresponding function parameter
void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )
declared as having type int *
The expression sizeof(arr4)
gives the size in bytes of the array arr4
, while you need to pass the numeric value of the elements in the array.
The main function must have a return type int
int main( void )
{
//...
}
source to share