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;
}
Related
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 12 months ago.
Improve this question
How can I change line 6 to handle the integer overflow so that this code will never terminate (while still increasing i in each loop iteration)?
1 int main() {
2 unsigned int i;
3 i = 1;
4 while (i > 0) {
5 printf("%u \n", i);
6 i *= 2;
7 }
8 printf("%u \n", i);
9 }
Because i is unsigned, it is never less than zero, but it may at some point be zero.
I might try to guarantee it is always at least 1 with something like this:
i = i*2? i*2 : 1;
That is:
If i*2 is non-zero, then that is the new value of i.
Otherwise, i*2 would be zero, so instead set i = 1;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 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 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 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 6 years ago.
Improve this question
How can I search for a number in a variable?
For Example:
Input A : 12131415
Input Search : 1
Output : 1 = 4
Input A : 12131415
Input Search : 12
Output : 12 = 1
Please Help Me. Thank You.
I'll not provide a complete solution for you but here is a basic idea that you can work with.
You could convert the numbers into strings and then count the number of times you can find one string in the other.
Here are some of the basic function you can use. This should get you started.
int A = 12131415;
int B = 1;
int count = 0;
char strA[20];
char strb[20];
sprintf(strA, "%d", A);
sprintf(strB, "%d", B);
int lenB = strlen(strB);
char* m;
char* t = A;
m = strstr(t, strB);
if (m != NULL)
{
// Found a match
++count;
}
else
{
// No more matches
return;
}
// Move pointer
t = m + lenB;
// Now look for next match
m = strstr(t, strB);
//.... and so on....
Your task is to organize the above code in a loop so that you can go through the whole string A.
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);
}