Int oopar (void *p) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Int oopar (void *p)

consider the above signature oopar is invoked with a character pointer usually, and the pointer points to a string of '0' and '1' write the body of oopar to determine if the passed string has odd number of ones. Note Function body shouldn't use an arithmetic operator as arithmetic is expensive on the low cost cpu where the function is needed, help me to solve this question in c language.

15th Jun 2022, 6:41 AM
Abhishek Mp
Abhishek Mp - avatar
9 ответов
+ 4
I think there's still an issue here. How to iterate the characters in given argument when arithmetic operation isn't allowed. Then pointer arithmetic is supposedly also not allowed?
15th Jun 2022, 12:00 PM
Ipang
+ 2
Why use void* for parameter when it is clear that argument will be string/char*? Is pre/post increment operator allowed? I think it will be necessary at least, to iterate the characters and increment the '1' character counter.
15th Jun 2022, 10:17 AM
Ipang
+ 2
Because its a pointer pointed to nothing, but arithmetic operation not allowed in this case to print the odd ones
15th Jun 2022, 10:51 AM
Abhishek Mp
Abhishek Mp - avatar
+ 2
In a loop, find the Exclusive Or (^) of all the characters. Then see if the final value is odd. Conveniently, ASCII '1' is 49 - an odd number. ASCII '0' is 48 - even. When you XOR them all together, the result will be odd only if there are an odd number of '1's. After the loop, return a boolean that tells whether the result is odd or even. To check odd/even the fast way, use bitwise And (&) to mask the lowest bit, and that becomes your return value (1 for odd, 0 for even).
15th Jun 2022, 10:56 AM
Brian
Brian - avatar
+ 2
I wasn't referring to the even/odd verification part bro, I was referring to the string iteration part ...
15th Jun 2022, 12:03 PM
Ipang
+ 1
& is a logical operator right hence it is possible or not
15th Jun 2022, 12:02 PM
Abhishek Mp
Abhishek Mp - avatar
0
#include<stdio.h> Int main() { int number; printf("enter number to check even or odd"); scanf("%d",&number); if((number & 1) ==0) printf("%d is even",number); else printf("%d" is odd,number); getch(); } Is this correct?
15th Jun 2022, 11:32 AM
Abhishek Mp
Abhishek Mp - avatar
0
Yeahh correct but that part am not getting
15th Jun 2022, 12:16 PM
Abhishek Mp
Abhishek Mp - avatar
0
You could implement the loop with recursion. Although I would argue that an addition operation is much faster than call/return overhead. Another possibility is to use switch/case with hardcoded as many cases as the allowed maximum length input string, and no break statements so it executes all the cases below the selected case, too. // suppose max string length is 5 int c = 0; switch(stringlen) { case 5: c ^= s[4]; case 4: c ^= s[3]; case 3: c ^= s[2]; case 2: c ^= s[1]; case 1: c ^= *s; case 0: } return c&1; // true if odd count of '1's Yet still, array indexing is implemented using addition. The compiler translates s[4] literally as *(s+4). EDIT- optimized revision: You can reduce it to one addition operation, then use a pointer with autodecrement, which is faster on some machines than addition: int c = 0; char *p = s[stringlen]; switch(stringlen) { case 5: c ^= *--p; case 4: c ^= *--p; case 3: c ^= *--p; case 2: c ^= *--p; case 1: c ^= *p; case 0: } return c&1;
15th Jun 2022, 1:26 PM
Brian
Brian - avatar