Converting from Binary to Decimal without using arithmetic operations [closed] - c

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 had this written until i realized * is an arithmetic operation.
I want to return a nonnegative integer representation of the binary sequence input. IE. 0x4a returns 74 in decimal
typedef unsigned bit16;
unsigned int bin_to_dec(bit16 x)
{
int dec=0;
int k=0;
int remainder;
while (x!= 0){
remainder = x%10;
dec+=remainder*k;
k=k+2;
x=x/10;
}
:(
How would I go about this conversion if I can't use arithmetic operations other than +/-?

Since + is also an arithmetic operation, it becomes difficult. Depending on the exact rules, using a lookup table might be acceptable: return lookuptable[x];

As + and - are allowed...Instead of multiplying into k*reamainder try looping this way
int n;//consider new int
In the while loop write first line as
n=remainder;
Instead of *
for(i=0;i<k;i++)
remainder+=n;
This would do the multiplication :).
And for x%10, construct a function
int mod(int n)
{
int m;
while(n>0)
{
n=n-10;
m=n;
}
return m;
}
And for x/10 it'd be the same but you must return number of times you subtracted like this :
int mod(int n)
{
int count=0;
while(n>0)
{
count=count+1;
n=n-10;
}
return count;
}
edit : If + and - are also not allowed try making functions for them using binary operators and use them instead of + and - in the above answer!

Related

What is wrong with this code?? It is giving output as 0.0000? [closed]

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 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.

power function K&R [closed]

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 4 years ago.
Improve this question
Hello I am beginner in C and I am reading 1.7 functions in K&R book. Below code is taken as is from the book.
int power(int m, int n);
int main()
{
int i;
for (i=0;i<10;++i)
printf("%d %d %d \n",i,power(2,i),power(-3,i));
return 0;
}
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
I can not understand how this code works, especially this part:
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
Here, where is returned p?
How this whole code raises number in power? And, relationship between these two parts of the code?
Any help is appreciated.
The code multiplies the base n times, which is essentially the definition of an integer exponent. What's important to recognize is that the loop executes n times, and each time it is multiplying p by the base.

Can you index an array with a binary number in C? [closed]

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;
}

Binary To Decimal [closed]

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 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.

Debug C program to sum up all even terms of the Fibonacci Sequence under 4 Million [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
long int first=1,second=2,sum=0,a[4000000],i=0;
while(second<4000000)
{
a[i]=first;
second=first+second;
first=second-first;
i++;
}
for(i=0;i<1999999;i++)
{
sum=sum+ a[2*i+1];
}
printf("The required sum is : %d",sum);
}
I am not able to detect the error, it is running an infinite loop I guess
No Compile time errors found but no result obtained
You do not need to store all the numbers under 4 million - just calculate the numbers on the fly in your first and second variables, and add every other one into your sum.
Based on mc110's answer:
#include <stdio.h>
int main()
{
const int LIMIT = 4000000;
long int first=0,second=1;
long int sum=0;
while(second<LIMIT)
{
if( (second % 2) == 0) // we only want even numbers
sum += second;
// could be optimized even more by simply swapping first/second
long int tmp = first;
first = second;
second += tmp;
}
printf("The required sum is : %d",sum);
}
gives me (spoiler alert)
The required sum is : 4613732

Resources