Question

Please help me with the following C Programming project. I am providing the code I used...

Please help me with the following C Programming project. I am providing the code I used which needs to be reworked to add the following requirements. Here is my code

#include<stdio.h>

char input;

int main() {

int i = 0;

while (true){

scanf_s("%c", &input);

if (input == 'X')

break;

printf("%c", input);

}

return 0;

}

Here are the requirements for the code plus and additional note what the code should have.

Goals

  • Understand the ASCII representation of character values.
  • Understand how to convert ASCII hex character values into their decimal value.
  • Utilize Boolean expressions.
  • Utilize an assignment statement.

Problem

Create a C program that reads hexadecimal characters from input (0-9, A-F) and outputs the decimal value of the ASCII character read and the decimal value corresponding to the hexadecimal character read. Input should be read until the character 'X' is read on input.

Assume your input is only ASCII characters '0' - 'F', ending in 'X'.

Tasks

  1. First, alter your code from the previous assignment to output the ASCII character read in as decimal (in the previous assignment you output it as a character just like it was read... hint %d is the format you need... also add \n so that the decimal number is on a single line).
  2. Next, run the program and make sure the value output makes sense in terms of your ASCII chart in your book. Be sure to provide inputs from 0-9 and A-F. Note the decimal values to know what you need to do to convert them to their decimal values for the hexadecimal representation (e.g. '9' is 9 and 'A' is 10).  
  3. Declare a global integer to hold the decimal value converted from the hex character read.
  4. Next, write the code to convert '0' - '9' to 0 - 9 and 'A' - 'F' to 10 -15, setting the variable in the previous step. You'll need to have a condition to detect if the character read is '0' through '9' inclusive. You can just use an else clause if the character is not '0' - '9', assuming that it is then 'A' through 'F'. By doing this, we are assuming that the input is correct.
  5. Output the value variable. You'll need to have a \n after you output the digit to put the converted value on a single line by itself too.

Example of what he would like to see but cannot use this code.

This was to indicate that you had a choice between the two (0-9 or A-F) as to which one you could use in your if statement and leave the other check to the else portion of the if statement.

Unfortunately, this was taken to indicate that you would use the OR (||) in the condition that you would write which would be wrong. The condition that you would typically write to determine if a character read in was between '0' and '9' inclusive would require an AND (&&).

So, you should have in your code something like this:

if (check for character being between '0' AND '9')
   calculation to set value = to the '0' - '9' value read in minus some value to turn it into a decimal value
else
   calculation to set value = to the 'A' - 'F' value read in minus some value to turn it into a decimal value

OR you can do this check

if (check for character being between 'A' AND 'F')
  calculation to set value = to the 'A' - 'F' value read in minus some value to turn it into a decimal value
else
  calculation to set value = to the '0' - '9' value read in minus some value to turn it into a decimal value
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*please refer below code, and copy paste it to some ide so that you can see comment and code clearly, Have a good day*/

#include <stdio.h>

int main(void) {
   char input; //declaring input variable to read from standard input
   while(1)
   {
       scanf("%c",&input);
       //if inputted characted is 'X' then we need to break loop
       if(input=='X')
       {
           break;
       }
       printf("You have entered %c\n",input); //demonstration purpose,you can delete it
       //if input is between '0' and '9'
       if(input>='0'&&input<='9')
       {
           // to find decimal value of the ASCII character read, we need to understand that all character are
           // mapped to some integer valuse,
           // so if we typecast given input to integer, it will give us decimal of ascii character
           int ascii_decimal = (int) input;
           printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
           // to find decimal value of entered hexadecimal value we need to recall that
           // '0' is mapped to 0,'1' is mapped to 1 and so on upto 9
           int hexa = input-'0'; //this will automaticallt shift all input to 0 to 9
           printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);
       }
       else
       {
           // to find decimal value of the ASCII character read, we need to understand that all character are
           // mapped to some integer valuse,
           // so if we typecast given input to integer, it will give us decimal of ascii character
           int ascii_decimal = (int) input;
           printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
           // to find decimal value of entered hexadecimal value we need to recall that
           // 'A' is mapped to 10,'B' is mapped to 11 and so on upto 15
           int hexa = input-'A'+10; //this will automaticallt shift all input by 10
           printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);
       }
       /* here we are assuming that user will always entered allow value, otherwise you can try below code
       which is same as above but its handling invalid input

           if(input>='0'&&input<='9')
           {
               // to find decimal value of the ASCII character read, we need to understand that all character are
               // mapped to some integer valuse,
               // so if we typecast given input to integer, it will give us decimal of ascii character
               int ascii_decimal = (int) input;
               printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
               // to find decimal value of entered hexadecimal value we need to recall that
               // '0' is mapped to 0,'1' is mapped to 1 and so on upto 9
               int hexa = input-'0'; //this will automaticallt shift all input to 0 to 9
               printf("Decimal value of the Hexadecimal digit %c = %d\n",input,hexa);
           }
           else if(input>='A'&&input<='F')
           {
               // to find decimal value of the ASCII character read, we need to understand that all character are
               // mapped to some integer valuse,
               // so if we typecast given input to integer, it will give us decimal of ascii character
               int ascii_decimal = (int) input;
               printf("Decimal value of the ASCII character %c = %d\n",input,ascii_decimal);
               // to find decimal value of entered hexadecimal value we need to recall that
               // 'A' is mapped to 10,'B' is mapped to 11 and so on upto 15
               int hexa = input-'A'+10; //this will automaticallt shift all input by 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;
}

Add a comment
Know the answer?
Add Answer to:
Please help me with the following C Programming project. I am providing the code I used...
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 Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal...

    In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal equivalent. """ total = 0 for c in s total = total * 16 ascii = ord(c if ord('0) <= ascii <= ord('9'): #It's a decimal number, and return it as decimal: total = total+ascii - ord('0') elif ord('A") <= ascii <= ord('F'): #It's a hex number between 10 and 15, convert and return: total = total + ascii - ord('A') + 10 else...

  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g` `Incorrect hex number` Hints: you...

  • JUST GIVE ME THE C CODE FOR THIS QUESTION. I GOT THE ASSEMBLY CODE.

    JUST GIVE ME THE C CODE FOR THIS QUESTION. I GOT THE ASSEMBLY CODE. In this assignment, you will create a C program that iteratively populates a fixed-size integer array of 3 elements, array α.withhexadecimal integer valuesprovided by scanf. The user will enter 3 positive hexadecimal integer values, one per line, and the program will store the 3 values in arYay _afOJ,arvay aflJ, and crr ay_af2J. Once the 3 integers are entered, your program will print the array ain 32...

  • C++ Can someone please help me with this problem- commenting each line of code so I...

    C++ Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language? I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read. #include<fstream> #include<iostream> using namespace std; int main()...

  • C++ please! In this program you will be outputting the characters that map to the ASCIl...

    C++ please! In this program you will be outputting the characters that map to the ASCIl codes 32 through 126. You will need a loop to iterate through the input values and output the corresponding character. This mapping is shown in appendix A in your Gaddis text book. Your program will be reading in two unsigned integer values. The prompt for read will be Enter lower and upper values You need to check that both values are in the range...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

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