Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Specifically I'm trying to simplify this particular line of code by removing any of its operators with the limitation that I'm only allowed to use ! ~ & ^ | + << >>:
int combine = ((sign << n) + ~sign + 1);
If you run this program a handful of times, with different values of n, there will be an OBVIOUS pattern:
#include <stdio.h>
int main(void) {
int n = 4;
for(int sign=1; sign<15; ++sign)
{
int combine = ((sign << n) + ~sign + 1);
printf("%d => %d\n", sign, combine);
}
return 0;
}
In each case, when:
n == 1, then combine == sign.
n == 2, then combine == 3*sign.
n == 3, then combine == 7*sign.
In general, for any N, combine == ((2n)-1) * sign
Now, can you find a fast way to express that relationship?
Are there any restrictions on values for n and sign that you haven't told us about?
Or does it need to be solved for all values of n and sign?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I've got a task in my lesson to write a function that will check if a number is prime, it will return 1 if it is and 0 if it is not and all of that to do recursively. the function will look like that :int isPrime(int num);
I've tried everything and searched everywhere and came to the conclusion that it's possible only with static int which I did not learn yet so can't use it or with two parameters.
does anyone know if there is a way to solve it with only one parameter ad no static int?
thanks.
This was a trick question: you are supposed to use recursion, but unless you are barred from using loops, a classic solution with a for loop can be made to comply with the recursion requirement.
Here is a function isPrime that has a single argument and uses a classic loop and recursion to test prime divisors:
int isPrime(int n) {
if (n <= 1)
return 0;
if (n % 2 == 0)
return n == 2;
for (int p = 3; p * p <= n; p += 2) {
if (isPrime(p) && n % p == 0)
return 0;
}
return 1;
}
The above code only performs the modulo operation for prime divisors. It is a false optimisation because it is actually more costly to determine if p is prime with a recursive call than to compute n % p.
So here is my solution to this questionable assignment. It contains only functions taking one int argument and uses recursion and no loop:
#include <iostream>
#include <functional>
std::function<bool(int)> isPrime(int x) {
return [x](int p) {
if (x <= 2) return true;
return (p % (x - 1) != 0) && (isPrime(x - 1)(p));
};
}
int main() {
if (isPrime(19)(19)) std::cout << "is prime" << std::endl;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if it is possible to index an array by using a binary number instead of a decimal number. For instance, arr[binary].
Yep that's definitely possible. Just prepend your binary number with 0b
int array[] = {1,2,4,6};
printf("%d\n", array[0b0001]); // prints 2
from https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html binary constants can be written using the 0b syntax
As pointed out, all numbers stored on a computer are binary. Binary is the only thing that can be stored on a computer.
And, C does not support binary syntax. (Or perhaps come C compilers do?)
You could however convert a string from binary like this:
var value = arr[BinaryToInt("1011")];
int BinaryToInt(string s)
{
int value = 0;
int bitValue = 1;
for (int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1')
value += bitValue;
bitValue <<= 1;
}
return value;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to get sum of carryover on adding two 4 digit numbers?
An example would be:
Carryovers 111
First 4 digit 9999
2nd 4 digit 7777
Answer 17776
Here I want to calculate the sum of the carries (answer of summing carries = 3), what should I do?
This is a rather simple task. You may not know it sice you are new here, but SO is not a coding plateform, so you won't get answers unless you show us your code first.
But, since I wanted to try myself on the algorithm, here's a simple answer. I warn you, it won't work on all examples (I won't tell you when this code won't work) and I'm not commenting the code on purpose. I'm smelling the assignment here, so the code is just to give you some pointers, not to make your homework.
Beware: It's just a skeleton and you won't get a good grade if you copy paste it as it is.
int main(int argc, char** argv)
{
int numberA = 9999;
int numberB = 7777;
int sum_of_carryovers = 0;
while (numberA > 9 && numberB > 9)
{
int digitA = numberA % 10;
int digitB = numberB % 10;
int sum = digitA + digitB;
if (sum > 9)
{
sum_of_carryovers += 1;
}
numberA /= 10;
numberB /= 10;
}
printf("The sum of carryovers = %d\n", sum_of_carryovers);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}