Question

Given the following: struct Val { int num1; float num2; } ; struct Person { int...

Given the following:

struct Val {

int num1;

float num2;

} ;

struct Person {

int age;

float income;

};

Val t, s;

Person p1, p2, p3;                                       

s.num1 = 0; s.num2 = 2.5;                          //initialize s

p2.age = 25; p2.income = 9999.99;                //initialize p2

t = s;                            //assignment one

p1 = s;                       //assignment two

p2 = p3;                      //assignment three

p3.age = s.num1;     //assignment four

p3.income = s.num1;   //assignment five

p2.age = t.num2;      //assignment six

For each assignment statements above explain whether they will compile or not and why. Also list if there will be any coercions (widening or shortening of datatypes).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assignment one:

t = s

  • No Compilation errors

Both t and s belong to the same type, i.e. they both are the objects of same structure “Val”. So, there will not be any compilation error and ‘t’ will be assigned the values of ‘s’.

t will be [num1 = 0 and num2 = 2.5] after this assignment

  • No Coercions

Value of num1 in object ‘s’ will be assigned to that of num1 of ‘t’ and the same with num2. Since int value is assigned to int and float to float, there will not be any coercions.

Assignment two:

p1 = s

  • Compilation error

The objects ‘p1’ and ‘s’ belong to different types of structures. ‘p1’ belongs to Person and ‘s’ belongs to Val. So, the assignment is not possible and results in an error as conversion of Val object to Person object is not possible

Assignment three:

p2 = p3

  • No Compilation errors

Both p2 and p3 belong to the same type of structure, i.e. they both are the objects of same structure “Person”. So, there will not be any compilation error and ‘p2’ will be assigned the values of ‘p3’.

p2 will be [age = 0 and income = 0] after this assignment

  • No Coercions

Value of age in object ‘p3’ will be assigned to age of ‘p2’ and the same with income. Since int value is assigned to int and float to float, there will not be any coercions.

Assignment four:

p3.age = s.num1

  • No Compilation errors

Both p3.age and s.num1 are integers. So they can assigned to one another and hence there will not be any compilation errors

p3.age will be assigned the value of s.num1 which is 0 after this assignment

  • No Coercions

Since they are both integers there is no concept of coercion.

Assignment five:

p3.income = s.num1

  • No Compilation errors

num1 is an integer which is assigned to a float variable named income. The value of num1 will be implicitly converted to float (coerced) and will be assigned to income. So, there will not be any compilation errors

  • Has Coercions

Since float is assigned an integer value, the latter will be coerced to float before being assigned.

Assignemnt six:

p2.age = t.num2

  • No Compilation errors

num2 is a float variable which is assigned to an integer variable named age. The value of num2 will be implicitly converted to integer (coerced) and will be assigned to age. So, there will not be any compilation errors

  • Has Coercions

Since integer is assigned a float value, the latter will be coerced to integer before being assigned

Value of p2.age will be 2 (t.num2 = 2.5, which when coerced/converted to integer will be 2) after this assignment.

Hope this helps you. Please let me know in case any further clarification is needed.

Add a comment
Know the answer?
Add Answer to:
Given the following: struct Val { int num1; float num2; } ; struct Person { int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT