Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone go through the c++ code and explain how these answers were reached?

ID: 3910196 • Letter: C

Question

Can someone go through the c++ code and explain how these answers were reached? I provided the answers I just don't know how to get those answers going through this code.

Code 1

struct MyStruct {

long l1 = 0;

long l2 = 0;

MyStruct () = default ;

MyStruct ( long p1 , long p2 ): l1 ( p1 ) , l2 ( p2 ) {}; };

(16) What is the output of MyStruct s; cout << s.l1 << endl;? 0

(17) What is the output of MyStruct s(2, 3); cout << s.l1 << endl;? 2

Code 2 :

void fn3 ( MyStruct & s , long lng ) {

s . l1 += lng ;

s . l2 += lng ; }

(18) After executing MyStruct s; fn3(s, 1);, what is the value of s.l1? 1

(19) After executing MyStruct s(2, 3); fn3(s, 2);, what is the value of s.l1? 4

Explanation / Answer

At first I am providing the line by line explabnation of CODE1 and CODE 2; then it will be easier to nderstand the output;

Explanation of CODE 1:

struct MyStruct //this is structure definition
{
long l1 = 0;//member variable of type long l1 is initialized with 0

long l2 = 0;//member variable of type long l2 is initialized with 0
/* this is default constructor of structure with this
default value of l1 and l2 will be initialized which is 0 here
*/
MyStruct () = default ;
/* following is the parametric constructor; if we pass p1 and p2 value of type long
to construct the structure then p1 value will be assigned to l1 and p2 will be assigned to l2
*/
MyStruct ( long p1 , long p2 ): l1 ( p1 ) , l2 ( p2 ) {};
};

Explanation of CODE 2:

//function definition of fn3 : it takes two arguments and returns nothing
// 1st argument: Variable of MyStruc type structure which is pass by refence
//2nd argument: a long type variable
void fn3 ( MyStruct & s , long lng ) {
// the value of l1 in s struct is increamented by lng amount
s . l1 += lng ;
// the value of l2 in s struct is increamented by lng amount
s . l2 += lng ;
//as the function argument is passed by reference so the changes that is made
//within the function will actualy change variables value parmanently
}

Explanation of question 16) (Here line by line basis explanation is provided)

MyStruct s; // variable s of type MyStruct is created with default constructor

/* as s is created with default constructor then value of l1 and l2 will be 0*/

//printing the value of l1 as the value is 0 so output will be 0

cout << s.l1 << endl;

SO OUTPUT is 0.

Explanation of question 17) (Here line by line basis explanation is provided)

MyStruct s(2, 3); // a variable s of type MyStruct is created with parametric constructor with value 2 as p1 and 3 as p2

/* as s is created with parametric constructor then value of l1 and l2 will be equal to p1 and p2 respectively;

so now l1 will be 2 and l2 will be 3

*/

//printing the value of l1 as the value is 2 so output will be 2

cout << s.l1 << endl;

SO OUTPUT is 2.

Explanation of question 18) (Here line by line basis explanation is provided)

MyStruct s; // variable s of type MyStruct is created with default constructor

/* as s is created with default constructor then value of l1 and l2 will be 0*/

//now the fn3 function is called with variable s and 1 as argument

//so in function fn3 lng variable will be 1

fn3(s, 1);

//after executing the function the member variable os s will be increamented by 1

// as before calling the function l1 was 0 so after increamented by 1 the value of l1 will be 1

//hence the output will be 1 here

cout << s.l1 << endl;

SO OUTPUT is 1.

Explanation of question 19) (Here line by line basis explanation is provided)

MyStruct s(2, 3); // a variable s of type MyStruct is created with parametric constructor with value 2 as p1 and 3 as p2

/* as s is created with parametric constructor then value of l1 and l2 will be equal to p1 and p2 respectively;

so now l1 will be 2 and l2 will be 3

*/

//now the fn3 function is called with variable s and 2 as arguments

//so in function fn3 lng variable will be 2 here

fn3(s, 2);

//after executing the function the member variable os s will be increamented by 2

// as before calling the function l1 was 2 so after increamented by 2 the value of l1 will be 4

//hence the output will be 4 here

cout << s.l1 << endl;

SO OUTPUT is 4.

Hope these explanation helps. Thanks.

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/