C array of integers represented as bits converted to a decimal [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
If i have length of 20 2d array
of length 5 integers represented as bits
int bitsval[6];
and the array looks like for example bitsval = {0,0,1,0,1,0}
and i want the decimal value so something like
(2^0 x 0) + ( 2^1 x 1) + (2^2 x 0)... and eventually return the final value as 10
can some one help write a function a function like this

This looks like a homework question, so I'll help, but not give you the code
What you need to do is have a 'total' variable, and a 'current bit worth' variable.
Start with the 'current bit worth' variable equal to 32 (2 ^ 5)
Then, have a loop going through the array, adding the bit worth on to the total if that bit is '1'
Each time you go to the next element in the array you need to half the 'current bit worth' value
[Edit]
OK - if it's not homework, try this:
total = 0;
bitvalue= 32;
for (i = 0; i < 6; i++) {
if (bitsval[i]) total += bitvalue;
bitvalue /= 2;
}

This should work I guess.
#include<math.h>
#include<stdio.h>
void main()
{
int a[]={1,0,0,1,0};
int sum=0;
for(int i=0; i<sizeof(a)/2; i++)
{
if(a[i]==1)
sum+=pow(2,(sizeof(a)/2)-i-1);
}
}

Without going into any kind of binary, and working of off of purely your explanation here is something that should work:
#include <stdio.h>
#include <math.h>
int main(){
int number[6] = {0,0,1,0,1,0}, result = 0;
for (int x = 5; x >= 0; --x){
result += (number[x] * (int) pow(2, 5-x));
}
printf("Final result: %d\n", result);
return 0;
}
You will off course have to massage it to fit

Related

Converting a 10 digit Int into Hex and then converting that to values 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 3 years ago.
Improve this question
For an exercise in C class, we are given an array of ints which contain 10 digits each:
unsigned int Array[] = { 1310740313, 1966096717, 2621456717 };
We are asked to print out a letter, a value in the 255 range and another value in the 65535 range.
We are using a little endian machine so we have to read from the right. I can figure out the answers by using conversions online but I can't figure it out using C code.
Do I get the hex value, store it, and then do a printf on each byte?
My current code looks like:
for (int i = 0; i < ArraySize; i++)
{
printf("%x\n", Array[i]);
}
Output:
4e204f59, 7530414d, 9c40414d
How do I convert these to the letter and 2 number?
I think you looking for something like this:
#include <stdio.h>
unsigned int Array[] = { 1310740313, 1966096717, 2621456717 };
int main()
{
for (int i = 0; i < 3; i++) {
printf("%c %d %d\n", Array[i] & 0xff, // last byte as char
(Array[i] & 0xff00) >> 8, // second byte as decimal
(Array[i] & 0xffff0000) >> 16); // last 2 byte as decimal
}
return 0;
}
Output
Y 79 20000
M 65 30000
M 65 40000
Basically masking the byte(s) of interests and shifting them to the right place.

How to write (a^n-1) in c programming [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
I want to write (a^n-1) in c programming. I wrote pow(a,n-1) and the output is
wrong. How can I solve this problem? Thanks.
Here is my code:
#include <stdio.h>
#include<math.h>
int main() {
float a, r,n;
float sum = 0;
a = 1.04*pow(a, n-1);
r = 1.02*pow(1.04,-1);
n = 2;
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
The correct output should be 286376 but the program showed 2903773
Every C program executes line by line. So, at the time when compiler came on
a = 1.04*pow(a, n-1);
this line, variable a and n was not assigned with any value, resulted in giving you a garbage value...
So, the problem is, you had not assigned values in variables, and still, you were using them.
You have to first assign values in variables before using them. Otherwise, they will pick any garbage value from memory (Any Random number).
Assign value in a and n and try again.
Edit: As chux's comment suggests, if your program is supposed to give 286376 as output, then value of a should be 1 and value of n should be 2.
So, your correct code would be this:
#include <stdio.h>
#include <math.h>
int main() {
float a=1, r, n=2;
float sum = 0;
a = 1.04*pow(a, n-1) ;
r = 1.02*pow(1.04,-1);
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
pow(a, n-1) translates into exp(log(a) * (n-1)) which isn't precisely the same.. You can try to round the output like this:
round(pow(a, n-1))

Sum of carryover on adding two 4 digit numbers [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
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);
}

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.

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.

Resources