Question

Defining a binary in c language, write the function int binToDec(const int bin[]) to convert an...

Defining a binary in c language, write the function int binToDec(const int bin[]) to convert an eight-bit unsigned binary number to a nonnegative decimal integer. Do not output the decimal integer in the function. Test your function with interactive input?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<math.h>

int binToDec(const int bin[]){

int i=0,total=0,p=0;

//iterating the array

for(i=7;i>=0;i--,p++){

// starting the power with 0, 2^0,2^1,2^2...2^8

total=total+(pow(2,p)*bin[i]);

}

return total;

}

int main(){

int arr[8];

int n=0,i=0,decimalNumber;

while(n!=-1){

//reading binary number into array

printf("Please 8 bit binary number: ");

for(i=0;i<8;i++)

scanf("%d",&arr[i]);

//converting into decimal

decimalNumber=binToDec(arr);

//printing the decimal value

printf("Decimal Number is : %d\n",decimalNumber);

printf("Press -1 to exit, any other key to continue: ");

scanf("%d",&n);

}

}

Add a comment
Know the answer?
Add Answer to:
Defining a binary in c language, write the function int binToDec(const int bin[]) to convert an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...

    1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...

  • (C++ only) Write a function that returns a decimal number from a binary string. The function...

    (C++ only) Write a function that returns a decimal number from a binary string. The function header is as follows: int bin2Dec(const string& binaryString) For example, bin2Dec("10001") returns 17. Write a test program that prompts the user to enter a binary number as a string and displays its decimal equivalent value Sample Input: 1110100110101 Sample Output: Enter a bianry number: 1110100110101 7477

  • C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...

    C++ Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering system we use is called the decimal system, or base 10 system. The numbering system that the computer uses is called the binary system, or base 2 system. The purpose of this exercise is to write a function to convert a string representing a binary number from base 2 to base 10. To convert a...

  • using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the...

    using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the form of a 16-bit binary string. Convert to unsigned integer and print to the screen. if the user enters less than 16 bits, you may pad the binary number with zeros on the left. Sample Execution: Enter a 16-bit binary number: 1001111001011100 The decimal unsigned integer equivalent is 40540 Enter a 16-bit binary number: 01100 The decimal unsigned integer equivalent is 12

  • In C++ please: Lab question 1: Provide a high-level algorithm in pseudocode to convert binary to...

    In C++ please: Lab question 1: Provide a high-level algorithm in pseudocode to convert binary to signed decimal below. Apply your algorithm on the following signed 8-bit values and provide the results: 00011101 11110011 Exercise 1: Convert your pseudocode from lab question 1 to a high-level language program such as C++ or Java that inputs an 8-bit binary string and then output the signed decimal. Try both test cases from lab question 1 above. Sample input and output: Please enter...

  • programming in c convert binary representation of a number to an unsigned integer. T,t=1, F,f=0 other...

    programming in c convert binary representation of a number to an unsigned integer. T,t=1, F,f=0 other characters are ignored STOP converting when a space appear or at the end of the string. for example ftft or FTFT =5, bTFdt TT=5 unsigned int 123(const char *binary) { }

  • Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k)...

    Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k) that takes as input an array A of values of type T with length n>= 0 and an integer k and returns the k-th smallest value in the array. If k >= n, throw a invalid_argument exception Example: A = [8,6,7,5,3,0,9] (array of ints), n = 7, k = 2, expected value = 5

  • Write a C function named  f such that … the prototype of function  f is … unsigned long...

    Write a C function named  f such that … the prototype of function  f is … unsigned long int f ( unsigned long int X, unsigned long int Y ) function  f returns … the nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  if such a nonnegative integer  Z  exists zero if there is no nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  Example 1 If   X = 3  and   Y = 4 ...

  • Write a function in C to convert a decimal number to binary using recursion and pointers....

    Write a function in C to convert a decimal number to binary using recursion and pointers. For example: convertToBinary(10) -> 1010 long convertToBinary ( int num ) { }

  • Here is the code I made, but the test case is not working, it goes wrong...

    Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help. #include "stdafx.h" #include <iostream> #include <string> #include <math.h> #include <locale> using namespace std; // function for option 1 void decToBin(int number) {        int array[16];        int i = 0;        for (int counter = 0; counter < 16; counter++)        {               array[counter] = 0;        }        while (number > 0)        {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT