Program 1 – Variable Definitions and sizeof The objective of this Program is to write a C++ program to generate a comprehensive output table that provides information on the different types of data storage that can be used in C++. The table will give typical variable types, names, values, the storage allocation in bytes of memory, and the range of values possible for each variable type Background Information Different computers may be designed to use different numbers of bytes to store data which may affect the size of the data that can be stored in a data type. The range of values within each data type can vary as well depending on how the system is set up. It can be very useful to know the ranges of data values on each system you are using, so it can be useful to run a program to determine those range of data values. For example a data type of short int typically uses 2 bytes of memory and stores values in the range of -32768 to +32767 Binary arithmetic is used to determine the range of values for integers. Each byte of memory is 8 bits, so 2 bytes is 16 bits. Storage in memory is binary: either a 0 or1, so each bit will hold either a 0 or 1 For 16 bits, one bit is reserved for the or – sign, and the other 15 bits determine the value For short integer numbers the minimum value is either -32767 which is(215 1) or -32768 which is 215. The maximum value is normally 32767 which is 215-1. However a data type of int typically uses 4 bytes of memory and typically stores values in the range of 2147483648 to +2147483647 Why would this matter? Suppose you are writing a program that needs a variable for the number of students in a typical classroom. That number certainly wouldn’t be over 32767, so you could use a short int data type. But suppose a different program is counting votes for a national election in the USA? That number would be in the millions and a short int data type would not be big enough to store the number of counted votes. You would need to use an int. Since memory is relatively cheap you could use int for integer variables and would probably be okay. Same for floating point data. It is probably okay to use double for all real variables, but often float is sufficient. We will see in a future program that there are accuracy considerations with real and double data types. It is good programming practice to be aware of the limitations of the computer you are working on. Program Specifications You are to write a computer program that defines nine different variables, one of each type Short integer Integer Long integer Unsigned integer Floating point Floating point double Floating point long double Character Boolean Choose an appropriate name and assign an appropriate value to each variable. For example, in your program you might declare a variable named counter to be integer, and initialize its value as0 using either of the following codes: int counter-0: OR int counter; counter = 0; Included at the end of this document is a sample output for Program 1. You may use the same variable names and values as the examples given, or you may choose your own names and values.
Expert Answer
//C++ program to demonstrate variable types, storage sizes and min/max values
#include <iostream>
#include <climits>
#include <cfloat>
using namespace std;
int main() {
short rate = 4;
int count = 42;
long int counter = -4322;
unsigned int totalint = 8432;
float distance= 0.3221;
double accuracy = 43.2189;
long double temp =369.82;
char charsymbol = ‘r’;
bool flag = 1;
cout<<“nDemonstration of variable types, storage sizes and min/max values”;
cout<<“nNamettTypetttttMemory in bytestttValuetttRange of values”;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n rate tt shorttttttt”<<sizeof(short)<<“ttt”<<rate<<“ttt”<<SHRT_MIN<<” to “<<SHRT_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n count tt integerttttt”<<sizeof(int)<<“ttt”<<count<<“ttt”<<INT_MIN<<” to “<<INT_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n counter t long integerttttt”<<sizeof(long int)<<“ttt”<<counter<<“ttt”<<LONG_MIN<<” to “<<LONG_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n totalint t unsigned integertttt”<<sizeof(unsigned int)<<“ttt”<<totalint<<“ttt”<<“0 to “<<UINT_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n distance t floating pointttttt”<<sizeof(float)<<“ttt”<<distance<<“ttt”<<FLT_MIN<<” to “<<FLT_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n accuracy t floating point doubletttt”<<sizeof(double)<<“ttt”<<accuracy<<“ttt”<<DBL_MIN<<” to “<<DBL_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n temp tt floating point long doublettt”<<sizeof(long double)<<“ttt”<<temp<<“ttt”<<LDBL_MIN<<” to “<<LDBL_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n charsymbol t characterttttt”<<sizeof(char)<<” ttt”<<charsymbol<<“ttt”<<CHAR_MIN<<” to “<<CHAR_MAX;
cout<<“n———————————————————————————————————————————————————“;
charsymbol = 45;
cout<<“n charsymbol t characterttttt”<<sizeof(char)<<” ttt”<<charsymbol<<“ttt”<<CHAR_MIN<<” to “<<CHAR_MAX;
cout<<“n———————————————————————————————————————————————————“;
cout<<“n flag tt boolean ttttt”<<sizeof(bool)<<” ttt”<<flag<<“ttt”<<“0 to 1”;
cout<<“n———————————————————————————————————————————————————“;
return 0;
}
Output: