Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C++ language (5 pts in total) Part 3 - Data Types Data types in computation are important because they determine the amount of memory
in C++ language
(5 pts in total) Part 3 - Data Types Data types in computation are important because they determine the amount of memory allocated for a variable and the type of value that can be stored in that memory. They also determine the operations that can be performed on the variable and the type of results that be computed. Using the correct data type ensures that the program will run correctly and efficiently, and that data will be stored and used correctly. The following exercises will enhance your understanding of data types. Write your answers to Q1-Q5 in a txt file. Ex1. int and float Type, compile, and run the following code, then answer the following questions (Note: Do NOT COPY-PASTE code from a PDF, as it might generate errors from): int value =3.14159, value 2=3.9999; cout value 1 endl; cout value 2 endl; float result 1=5/2; cout result 1 endl; int result2 =5.0/2.0; cout result endl; Q1: What are the outputs of valuel and value2? Where is the decimal precision? In order to print out those floating-point numbers, which data type should we choose? Q2: What are the outputs of result1 and result2? What are the differences between integer division vs float division? Ex2. boolean type Type, compile, and run the following code, then answer the following questions: bool my_bool1 = 1 ; bool my_bool2 =0; bool my_bool3 =20; bool my_bool 14=20; cout my_bool1 endl; cout my_bool endl; cout my_bool endl; cout my_boolu endl; Q3: What are the outputs of the code above? What are the only possible values that can be stored in a boolean variable? What values represent true, and what represents false? Q4: What happened if you tried to store a float, or a char into a bool variable? For example, what happened if you attempted the following: bool my_bools = 'C'; bool my_bool =1.001; Ex3. Overflow In lecture, we've mentioned that each data type uses a certain number of bytes to store/represent data. For example, an int type uses 4 bytes, short uses 2 bytes, double uses 8 bytes, etc. ( 1 byte =8 bits) Thus, each data type can only be used to represent a range of values. What happens if we tried to represent a value that is outside of the range of a certain data type? Here let's use short as an example: The maximum value that can be stored in a short data type is 32767(2281), and the minimum value representable by the type is 32768(228). Type, compile, and run the following code, then answer the following questions: short max_short =32767; short max_plus_one = max_short +1; cout max_plus_one endl; short min_short =32768; short min_minus_one = min_short - 1 ; cout min_minus_one endl; Q5: What are the outputs and why Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started