Question
Write code in c
prototypes for ConvertDecimalToBinary and PrintBinary function ConvertDecimalToBinary0 pass in input decimal number and array to hold binary number (8 cells 1 for each bit) In for loop, use right bitshift to divide by 2 In for loop, use a bitmask to determine if odd or even and ternary if to assign 1 or 0 to bit array function PrintBinary pass in binary number array Use for loop to print out each element of binary number array main Input loop Ask for decimal number to convert If entered number is not between 0 and 255,
media%2F238%2F238d2082-1e00-4284-8e56-90
media%2Fd05%2Fd05355f4-e0e9-4167-baea-d4
media%2F4ec%2F4ec80f59-342c-4a9a-9e1f-ab
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#define INT_SIZE 8 /* Size of int in bits */
#define BIT_MASK 1 /* Bit mask to find whether LSB bit is even or odd */

// function to convert decimal to binary
void convertDecimalToBinary(int decNum, int outBinNum[])
{
//to store the array of 8-bit binary number in reverse order
int index = INT_SIZE - 1;
  
for(int i=index;i >= 0;i--) {
  
//BIT_MASK of value 1 is used along with AND operator on the decimal number to get the Least Significant bit of decimal number
//if LSB is 1 i.e. odd, assign '1' or if even assign '0' to the array of 8-bit binary number in reverse order
outBinNum[i]=(decNum & BIT_MASK)?1:0;

decNum >>= 1; // right shift the decimal number by 1 position to divide by 2
}
}   

// printing 8-bit binary number
void printBinary(int binNum[])
{
printf("\nBinary number: ");
for (int j =0; j <8; j++)
printf("%d",binNum[j]);
}

// Driver program to test above function
int main()
{
int number,arrBinNum[INT_SIZE];
printf("Enter a decimal number: ");
scanf("%d",&number);

while(number<0 || number > 255)
{
printf("\nYou entered a number not between 0 and 255");
printf("\nEnter a decimal number: ");
scanf("%d",&number);
}
convertDecimalToBinary(number,arrBinNum);
  
printBinary(arrBinNum);
return 0;
}

output:

Enter a decimal number: 300                                                                                                      

                                                                                                                                 

You entered a number not between 0 and 255                                                                                       

Enter a decimal number: 100                                                                                                      

                                                                                                                                 

Binary number: 01100100

Add a comment
Know the answer?
Add Answer to:
Write code in c prototypes for ConvertDecimalToBinary and PrintBinary function ConvertDecimalToBinary0 pass in input decimal number...
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
  • In java language: Ask for keyboard input of a decimal number. Use a loop to keep...

    In java language: Ask for keyboard input of a decimal number. Use a loop to keep asking for input of up to ten numbers. Exit the loop if ten numbers are entered or if a sentinel value for “QUIT” is entered. Store the numbers in an array. Sort the array according to ascending number. Get the average for all the numbers in the array. Print out the average. Calculate the distance from the average for each number in the array...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • I am confused how to make program like convert binary to decimal and octal to decimal...

    I am confused how to make program like convert binary to decimal and octal to decimal using array in c++. This is question as below one. Your program must have at least two significant user defined functions, each using C++ arrays. Programs done using only main function, and done without using arrays, will get no credit. The program you will submit will have a menu driven system as below: Caution! No validation is done on numbers entered. They must conform...

  • Name your c++ file cpp. BScore_LastNameFirstName.cpp Write a program that contains a function call Input. The...

    Name your c++ file cpp. BScore_LastNameFirstName.cpp Write a program that contains a function call Input. The Input function should accept 2 decimal test scores that was entered from the user and stores them in an array. Don’t store any number less than 0. Return the array to main. Inside main, the program should pass the array and the total number of scores to countBGrade function. The countBGrade function should count how many B scores (80 to 89) were entered by...

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

  • Please help code in c++ and use the answers you get from question 1 and 2...

    Please help code in c++ and use the answers you get from question 1 and 2 into the 3rd answer! Question 1 is first, question 2 is in the middle, question 3 is last Input Array (10 pts) te a function only (no main) that takes an array of doubles as a parameter as well as another integer rray. Create a for loop that asks the user to input any real number between-100 and r each element of the array....

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:  ...

    MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:       .word       0 1 1 1 0 0 0 1       # is 113 in decimal .globl main main: # Load arguments to argument registers # Call convert()      # Print return value # (it's in $v0, make sure to copy it before overwriting to print)        # Properly end program    convert: # This label is where conversion of the current...

  • 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