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?
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?
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...
I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
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
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT