Create a small program that demonstrates the workings of a union in Visual C++.
ID: 3688309 • Letter: C
Question
Create a small program that demonstrates the workings of a union in Visual C++.
Create a new project.
Visual C++
Win32 Console Application
Name the project CIS022_S2016_Assignment9b your name
Select a location where you can find it later
Choose the default application settings
There is no need to format the values, just dump them to the console.
Display a title for each data block, a label for each member and insert spaces between each output block so that the output makes sense.
Create a small program that demonstrates the workings of a union in Visual C++.
Create a new project.
Visual C++
Win32 Console Application
Name the project CIS022_S2016_Assignment9b your name
Select a location where you can find it later
Choose the default application settings
char ch (char is 1 byte) short s (short is 2 bytes) int i (int is 4 bytes) float f (float is 4 bytes) double d (double is 8 bytes) 3. Your program should perform the following 5 operations:
A. Set all of the members of the union to 0.
Insert the character 'A' into the member ch.
Display all 5 members on the console. B. Set all of the members of the union to 0.
Insert the number 32,767 into the member s.
Display all 5 members on the console. C. Set all of the members of the union to 0.
Insert the character 2,147,483,647 into the member i.
Display all 5 members on the console. D. Set all of the members of the union to 0.
Insert the character 999.999 into the member f.
Display all 5 members on the console. E. Set all of the members of the union to 0.
Insert the character 999.999 into the member d.
Display all 5 members on the console.
There is no need to format the values, just dump them to the console.
Display a title for each data block, a label for each member and insert spaces between each output block so that the output makes sense.
Run the program to make sure your outputs work properly and your results make sense. 5. Insert your program documentation and code comments.
Provide program documentation and comment the entire program, including the code that was given to you.
Explanation / Answer
#include<iostream>
using namespace std;
union un{
char ch;
short s;
int i;
float f;
double d;
}myunion;
int main(){
//question A
myunion.ch='A';
cout<<"ch : "<<myunion.ch;
myunion.s=0;
cout<<" s : "<<myunion.s;
myunion.i=0;
cout<<" i : "<<myunion.i;
myunion.f=0.0f;
cout<<" f : "<<myunion.f;
myunion.d=0.0;
cout<<" d : "<<myunion.d<<endl;
//question B
myunion.ch='A';
cout<<"ch : "<<myunion.ch;
myunion.s=32767;
cout<<" s : "<<myunion.s;
myunion.i=0;
cout<<" i : "<<myunion.i;
myunion.f=0.0f;
cout<<" f : "<<myunion.f;
myunion.d=0.0;
cout<<" d : "<<myunion.d<<endl;
//question C
myunion.ch='A';
cout<<"ch : "<<myunion.ch;
myunion.s=0;
cout<<" s : "<<myunion.s;
myunion.i=2147483647;
cout<<" i : "<<myunion.i;
myunion.f=0.0f;
cout<<" f : "<<myunion.f;
myunion.d=0.0;
cout<<" d : "<<myunion.d<<endl;
//question D
myunion.ch='A';
cout<<"ch : "<<myunion.ch;
myunion.s=0;
cout<<" s : "<<myunion.s;
myunion.i=0;
cout<<" i : "<<myunion.i;
myunion.f=999.999f;
cout<<" f : "<<myunion.f;
myunion.d=0.0;
cout<<" d : "<<myunion.d<<endl;
//question E
myunion.ch='A';
cout<<"ch : "<<myunion.ch;
myunion.s=0;
cout<<" s : "<<myunion.s;
myunion.i=0;
cout<<" i : "<<myunion.i;
myunion.f=0.0f;
cout<<" f : "<<myunion.f;
myunion.d=999.999;
cout<<" d : "<<myunion.d<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.