Question

In: Computer Science

I have char memory[1024]. i want to write integer value of 300 into the memory. Type...

I have char memory[1024]. i want to write integer value of 300 into the memory. Type casting worked ok for all values 0-127. above that it looks like a garbage. could you write a function in c++ that will write an integer value say 300 into the memory. Thank you.

Solutions

Expert Solution

Char memory[1024] initiates character value. So if you store an integer in the character array it will act as a character but it will remain an integer for you.

Use the ‘unsigned char’ for memory optimization method.Developers generally use int to store integer values, without thinking about data range, if the data range is less, we should use unsigned char,

A type of char data type, unsigned char can store values between 0 to 255, so we can use unsigned char instead of short or int.

#include <stdio.h>
int main()
{
        unsigned char value=0;
        
        printf("Value is: %d\n",value);
        
        value=50;
        printf("Value is: %d\n",value);
        
        value=255;
        printf("Value is: %d\n",value);
        
        return 0;       
}

Output

Value is: 0 
Value is: 50
Value is: 255

What will happen, if the value is greater than 255?

int main()
{
        unsigned char value=300;
        printf("Value is: %d\n",value);
        return 0;
}

Output

value is: 44

Why?

Unsigned char store only 8 bits data into the memory, when the value is greater than only first 8 bits will be stored, see the given image.

In this image 8 bits value is: 0010 1100 which is equivalent to 44.


Related Solutions

I have a char memory[1024]. I want to write the string "mike" into the memory starting...
I have a char memory[1024]. I want to write the string "mike" into the memory starting from an index. I also need a function to move "mike" to another index in the same memory swapping m and k in mike. Basically, i need a function to write to memory and a function to move the string to an index. Thank you In c++ please.
CREATE TABLE Hotel ( roomNumber     INTEGER         PRIMARY KEY, type                  CHAR(1
CREATE TABLE Hotel ( roomNumber     INTEGER         PRIMARY KEY, type                  CHAR(10)         NOT NULL, rate                   INTEGER         NOT NULL, -- CONSTRAINT IC1 CHECK (type IN ('suite', 'king', 'queen')), CONSTRAINT IC2 CHECK (type <> 'suite' OR rate > 200), CONSTRAINT IC3 CHECK (NOT (type = 'king' AND (rate < 80 OR rate > 220))), CONSTRAINT IC4 CHECK (NOT (type = 'queen' AND rate >= 100)) ); which 8 of these inserts will be rejected only 8 are rejected 1. INSERT INTO Hotel VALUES (21, 'king', 90); 2. INSERT INTO Hotel...
1) If c is type char and i type int, what is assigned to c and...
1) If c is type char and i type int, what is assigned to c and i? Give answers in hex. Assume c is one byte, and i is four bytes. Explain. c = 240; i = c; 2) If c is type unsigned char and i type int, what is assigned to c and i? Give answers in hex. Explain c = 240; i = c; 3) If c is type signed char and i type int, what is...
Write a program that reads n integer values. If a negative value is entered, we want...
Write a program that reads n integer values. If a negative value is entered, we want to terminate the input, i.e., exit from the loop. If a zero value is entered, we want to ignore it and read the next value. Any strictly positive values (greater or equal zero) are to be totaled. Print the number of values read, the number of values totaled and the total. If a negative value is entered, print an error message before terminating the...
6- Write a C++ program that determines the amount of memory used by char, short int,...
6- Write a C++ program that determines the amount of memory used by char, short int, unsigned short int, float, and double types and display the information on the screen. You program should also display the typical range for each data type.
Write a function intLog of type Integer -> Integer that returns the exponent of the largest...
Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument. This needs to be a haskell function
Write an essay on why I want to be an educator. What I believe I have...
Write an essay on why I want to be an educator. What I believe I have to offer the students of our future?
For university, I have to write a Research paper and i want to examine whether there...
For university, I have to write a Research paper and i want to examine whether there is a relationship (correlation)between hours worked in a Country per week and the Happiness Index points as the dependent variable. However, it has to be a mutiple linear regression model but I struggle with finding more independent variables. Can somebody help me here?
In this lab, I want you to take in a numeric grade as an integer and...
In this lab, I want you to take in a numeric grade as an integer and then print a letter grade per the table below: 90 or above is an A 80 or above is a B 70 or above is a C 60 or above is a D Below a 60 is an F You must use a branching statement that prints the appropriate letter grade based on the user input2. Please comment your code
Write a subroutine that will receive a char input value. This subroutine should then use a...
Write a subroutine that will receive a char input value. This subroutine should then use a switch statement to determine if the char is a vowel, consonant, digit, or other type of character. Write the subroutine only,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT