Question

In: Computer Science

Can I get some assistance with this request. Right now the code that converts a hex...

Can I get some assistance with this request.

Right now the code that converts a hex character read on input into its decimal value is in the main routine. To make the main routine easier to read, make the conversion code concise and reusable, move it into a function named hex2Dec. The hex2Dec function should take a character parameter which is the character read from input. The function should return an integer which is the converted decimal value of the hexadecimal value represented by the character read from input and passed in as a parameter.

Tasks

  1. Copy the conversion code in the main() routine from the main() routine to lines of code just above.
  2. Next, wrap the conversion code in the function declaration code using the return type, name of the function, and the parameters.
  3. Next, add a call to the function in the main routine passing in the character that was read and assigning the return value to the resulting decimal value that you declared.
  4. Test your code in an identical fashion to that of Project 1B

Here is my project 1B. how can i change it to meet this requirement.

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

int main(void) {
   char input;
   while (1){
   scanf("%c", &input);
   if (input == 'X')
{
   break;}
   printf("You have entered %c\n", input);
      
   if (input >= '0' && input <= '9'){
   int ascii_decimal = (int)input;
   printf("Decimal value of the ASCII character %c = %d\n", input, ascii_decimal);
          
   int hexa = input - '0';
   printf("Decimal value of the Hexadecimal digit %c = %d\n", input, hexa);}
   else{
          
   int ascii_decimal = (int)input;
   printf("Decimal value of the ASCII character %c = %d\n", input, ascii_decimal);
          
   int hexa = input - 'A' + 10;
   printf("Decimal value of the Hexadecimal digit %c = %d\n", input, hexa);}

   if(input>='0'&&input<='9')
{
              
   int ascii_decimal = (int) input;
   printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
              
   int hexa = input-'0';
   printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);}
          
   else if(input>='A'&&input<='F')
{
              
   int ascii_decimal = (int) input;
   printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
              
   int hexa = input-'A'+10;
   printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);}
          
   else
{
   printf("you have entered invalid input!!\n");}
      
   fflush(stdin);}

   return 0;
}

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

// Take the hexadecimal value and return the decimal output.
int hex2Dec(char input)
{
printf("You have entered %c\n", input);

int ascii_decimal = (int)input;
printf("Decimal value of the ASCII character %c = %d\n", input, ascii_decimal);
  
// if the character is a number 0-9
if (input >= '0' && input <= '9')
{
int hexa = input - '0';
return hexa;
}
else
{
// input is from (A-F)
int hexa = input - 'A' + 10;
return hexa;
  
}

}

int main(void)
{
char input;
while (1)
{
// read input character
scanf("%c", &input);

// if it's X, stop
if (input == 'X')
{
break;
  
}
// call the function here
int hexa=hex2Dec(input);
  
// print the output here.
printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);
}

return 0;
}

===================

Screenshot:

OUTPUT:


Related Solutions

Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
Please, I need to get this right. No one is yet to get it right, it...
Please, I need to get this right. No one is yet to get it right, it has been answered on Chegg but is wrong In a psychrometric process, Volume of a classroom = 300 m^3. and temperature in the room is T= 20 oC, P=100 kPa relative humidity = 50% if all the water vapor of air in the room is converted to liquid at the same temperature, what is the volume of the liquid water (mL). I believe the...
in verilog if i have a hex number how do i get a specific 4 bits?...
in verilog if i have a hex number how do i get a specific 4 bits? for exampe if i have a hex number 5055 how do i get the last four bits which would be 0101?
I cant not seem to get this right. I have tried and tried can I please...
I cant not seem to get this right. I have tried and tried can I please get help. Thank you! Writing a Modular Program in Java Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values...
Can I get some sample of journal analysis of management ?
Can I get some sample of journal analysis of management ?
Baseball statistics sorting, addition to the old code. Following is what I have right now what...
Baseball statistics sorting, addition to the old code. Following is what I have right now what more specifications of the pseudocode I need addition to the program for the following pseudocode: Step 10 // sort the team[] on increasing order of Number This is an invocation of the method selectionSort() with the parameters team[] and team size. 5 CSC 156 - Assignment 9 Baseball Step 11) // display Player’s Number, Hits, Walks, Outs This is no different than the invocation...
Hi, i would like to request assistance with the following question: Bert computes a 95% confidence...
Hi, i would like to request assistance with the following question: Bert computes a 95% confidence interval for p and obtains the interval [0.600, 0.700]. Note: Parts (a) and (b) are not connected: Part (b) can be answered even if one does not know how to do part (a). (a) Bert’s boss says, “Give me a 90% confidence interval for p.” Calculate the answer for Bert. (b) Bert’s boss says, “Give me a 95% confidence interval for p−q.” Calculate the...
Will literally worship you if you can give me some assistance with these. (I rate immediately)...
Will literally worship you if you can give me some assistance with these. (I rate immediately) Consider the reaction. A(g)⇌2B(g) Find the equilibrium partial pressures of A and B for each of the different values of Kp. Assume that the initial partial pressure of B in each case is 1.0 atm and that the initial partial pressure of A is 0.0 atm. Make any appropriate simplifying assumptions. Part A Kp= 1.6 Part B Kp= 1.5×10−4 Part C Kp= 1.6×105
Can you please select the right answer. I did not  get a good score on this quizz....
Can you please select the right answer. I did not  get a good score on this quizz. Thank you Question 6 5 / 5 pts Define the following terms associated with healthcare insurance: If the answers do not show in their entirety, they are pasted below: The maximum amount an insurer will pay for a certain service A health condition written into the health insurance policy indicating what is not covered by the policy The amount of expenses the patient must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT