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++ 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...

  • 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 ...

  • 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)        {...

  • Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number....

    Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number. Specific requirements: Use InputBox function to acquire the input for an arbitrary binary integer number; Convert the binary integer to its equivalent decimal number; Return the decimal number in a message box. Submit your (VBA code) Excel screen shoot. Below functions may be useful in your VBA code: Val( ): can convert a string-type number in ( ) to its numeric value; Len( ):...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

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
ADVERTISEMENT