Concept explainers
Explanation of Solution
Corresponding modified code for “show_bytes”:
#include <stdio.h>
//Define variable "byte_pointer" in char datatype.
typedef unsigned char* byte_pointer;
//Function definition for show_bytes.
void show_bytes(byte_pointer start, size_t len)
{
//Declare variable "i" in int data type.
int i;
/* "For" loop to determine byte representation in hexadecimal */
for (i = 0; i < len; i++)
//Display each bytes in "2" digits hexadecimal value.
printf(" %.2x", start[i]);
printf("\n");
}
//Function to determine byte for "int" data type value.
void show_int(int x)
{
//Call show_bytes function with integer value.
show_bytes((byte_pointer) &x, sizeof(int));
}
//Function to determine byte for "float" data type value.
void show_float(float x)
{
//Call show_bytes function float value.
show_bytes((byte_pointer) &x, sizeof(float));
}
//Function to determine byte for "pointer" value.
void show_pointer(void *x)
{
//Call show_bytes function with pointer value.
show_bytes((byte_pointer) &x, sizeof(void *));
}
//Function to determine byte for "short" data type value.
void show_short(short x)
{
//Call show_bytes function with short int value.
show_bytes((byte_pointer) &x, sizeof(short));
}
//Function to determine byte for "long" data type value.
void show_long(long x)
{
//Call show_bytes function with long value.
show_bytes((byte_pointer) &x, sizeof(long));
}
//Function to determine byte for "double" data type value.
void show_double(double x)
{
//Call show_bytes function with double value.
show_bytes((byte_pointer) &x, sizeof(double));
}
//Test all show bytes.
void test_show_bytes(int val)
{
//Define variables.
int ival = val;
float fval = (float) ival;
int *pval = &ival;
//Call function.
show_int(ival);
show_float(fval);
show_pointer(pval);
//Define variables.
short sval = (short) ival;
long lval = (long) ival;
double dval = (double) ival;
//Call function.
show_short(sval);
show_long(lval);
show_double(dval);
}
//Main function.
int main(int argc, char* argv[])
{
//Define the sample value.
int sampleValue = 1024;
//Call test_show_bytes function.
test_show_bytes(sampleValue);
return 0;
}
The given program is used to display the byte representation of different program objects by using casting...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Write answer in C++ language. Take 5 integer inputs from user and store them in an array. Now, copy all the elements in another array but in reverse order.arrow_forwardWrite a program in C++ that can perform encryption and decryption using an arbitrary substitution cipher. In this case, the encrpyion array is a random shuffling of the letters in the alphabet. Your program should generate a random encryption array, its corresponding decryption array, and use these to encode and decode the message.arrow_forwardWrite a C program that uses arrays and functions to implement a calculator for twooperands. Your calculator must have five principal operations, addition ( ), subtraction ( ),division ( ), multiplication ( ), and power ( ). In the beginning of the program, show the menuof operators to the user. When the user selects one of the operators, your program gets the valuesfor operand 1 and operand 2 from the user and saves them into an array. Then pass the array to thesuitable function to calculate the result. The operating functions must return the value to the mainprogram. The calculation stops when the user press “x”. Use a global variable to save the result oflast operation. Anytime that the user types “M”, the program shows the result of last operation.hint: Each mathematic operation must be a separate function. Your main program calls those functions.arrow_forward
- Write this program in C programming language. Also provide a screenshot that it is working.Here is the question:Write a program that determines the three largest elements among inputed numbers.arrow_forwardslove the problem through c++ programing language with standard output. Take 10 integer inputs from the user and store them in an array. Again ask the user to give a number. Now, tell the userwhether that number is present in the array or not. write code in c++ and also show outputarrow_forwardCreate a class reversstring in c++andWrite a function called reversit () that reverses a C-string (an array of char). Use a forloop that swaps the first and last characters, then the second and next-to-last characters, and so on. The string should be passed to reversit () as an argument. Write a program to exercise reversit (). The program should get a string from the user, call reversit (), and print out the result.arrow_forward
- Write in c++ ....arrow_forwardIn c++ , perform data searching using linear and binary search. ( Drop code in words , explain the code and drop the screenshot of output as well )arrow_forwardHi! I need some help writing some code in C language (NOT C++/Java/Python!). How can I merge two different sentences (defined by user input - stored in arrays?) with alternating letters, print it, and then print it backwards? I would also like to remove any gaps or spaces before the sentences are merged. For example: Sentence 1 = I like cake Sentence 2 = I like donuts Merged sentences = IIlliikkeecdaokneuts Backwards merged sentences = stuenkoadceekkiillIIarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education