in Programming in C edited by
508 views
0 votes
0 votes
what is the  value of z?

#include <stdio.h>
int main() {
    int a[]={3,5,6,4};
    int z=0;
    for (int y=0;y<(sizeof(a)/sizeof int);y++)
        z=a[y]+value(a[y]);
}
int value(int *x)
{
    static int count;
    while(*x)
   {
       count=count+*x&1;
        *x>>1 ;
   }
}
in Programming in C edited by
by
508 views

4 Comments

int value(int *x)

x is a pointer variable that can hold the address of an integer.

value(a[y])  means you are passing an integer value and not it's address. For the prog to run properly we should pass the address of a[y].

Also there is not return statement in the function but the return type is of int. I guess it should be return count .. :/
0
0
though print statement not given

but function value() will count number of 1's in each element of the array
0
0
count=((count+(*x))&1)

is this is the right way to resolve the precedence? if not then which order should i follow.
0
0

1 Answer

0 votes
0 votes
Best answer
I think the answer is 11

as the count variables count the number of 1 in the binary representation of array elements.
selected by

2 Comments

If you are not confirm then post it as comment not as answer, please.
2
2
@sandygate: can you explain??
0
0

Related questions