Binary To Decimal [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 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.

Related

why doesn't this program in C language print anything? [closed]

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 2 years ago.
Improve this question
why doesn't this program print anything?
int main(){
int a = getchar()-'0';
getchar();
int b = getchar()-'0';
int vsota = 0;
vs = (a+b)%10;
putchar(vs);
printf("\n");
}
I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.
putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.
Also, fix this:
int vsota = 0;
vs = (a+b)%10;
That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.
because you try to output non printable character
int main(){
int a = '1'-'0';
int b = '6'-'0';
int vsota = 0;
vsota = (a+b)%10;
putchar(vsota + '0');
printf("\n");
}
https://godbolt.org/z/NAyrNn

Taking power with loop(with-out pow() function) [closed]

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.

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

How to find leading number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to write a function that identifies the leading digit of a integer number.I am confused and in lack of knowledge to write a function for this process.Can someone help me?
The simple, and naive, solution is to convert the number to a string, and get the first character of the string and convert it back into a number.
Another solution is to divide by ten in a loop, until the result is zero, while remembering the last digit of the number. When the result is zero, the last digit is the first digit of the whole number.
This can be done easily in two steps.
Change the number to string.
Return the first char of the string.
int num = 12345;
// left most digit:
int digit = abs(num);
while (digit > 9) {
digit /= 10;
}
// right most digit:
int digit = abs(num) % 10;
If you feel more confortable by using arithmetic instead strings and loops, try this approach:
Calculate the integer part of the base 10 logarithm
Divide the source number by 10 power to the integer part of the log
{
int value = abs(978);
int valuelog = (int)log10(value);
int leadingDigit = value / (pow(10,valuelog));
printf("%d", leadingDigit);
}
Example 978:
log10(978)->2.990; int 2
pow(10, 2)->100
978/100->9.78; int 9
#include <stdio.h>
int lead(const int number) {
int divider = 10;
while (number/divider) {
divider *= 10;
}
return number/(divider/10);
}
main()
{
int number = 678;
int leadNumber = lead(number);
printf("%d",leadNumber);
}
you can do that by converting the integer to string and return the first character.
or you can do a simple program as below.
int a;
scanf("%d",&a);
while(a/10>0)
{
a=a/10
}
printf("leading no. of given input is %d\n",a);
int lead_int(const int n) {
return (abs(n) < 10) ? abs(n) : lead_int(n /10);
}
Edited after seeing comments in this post (to include abs()), And made this a one lined function.

c programming Ascii values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}

Resources