Question

In: Computer Science

In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...

In C/C++ programming language, write down the lines of codes (and figure) to show-

a) assignment-by-sharing

b) dangling reference

Solutions

Expert Solution

Though 'assignment-by-sharing' term does not clears the aim on its own but checking the second part of dangling reference, 'assignment-by-sharing' must be assignment by sharing using reference.

Refer the lines of C++ codes below to understand :

a) Assignment-by-sharing

int main()

{
int x = 10;

int &y=x; //y is reference to x

y=30;

cout<<"x = "<<x<<endl;

x=50;

cout<<"y = "<<y<<endl;

return 0;

}

Here the output will be

30

50

This is because both x and y are sharing the same reference. Hence assigning a value to x will lead to assignment in y and vice - versa.

b) Dangling Reference

int &fun()

{
int i = 10;

return i;

}

int main()

{
int &x = fun();

cout<<x<<endl;

return 0 ;

}

This code seems to be correct but is not. Here 'i' is returned as a reference to fun() and 'i' is local to this function. So when the function fun() is finished, it will deleted 'i' from its stack. Means 'i' is no longer available and 'x' is pointing to 'i'. In this case, 'x' has no idea that 'i' is no longer available. Hence it is a dangling reference.


Related Solutions

Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Write a code in C or C++ programming language that generates the hexadecimal values in Table...
Write a code in C or C++ programming language that generates the hexadecimal values in Table 6-2 in the same format. Table 6-2 Hexadecimal text file specifying the contents of a 4 × 4 multiplier ROM. 00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 20: 00 02 04 06 08 0A 0C 0E 10...
"This C Programming " Write a program that accepts four (4) lines of input: • The...
"This C Programming " Write a program that accepts four (4) lines of input: • The first line contains a float value in 4.2f format • The second line contains a char value • The third line contains a 4-digit int value • The fourth line contains a char value and then displays all of the input on a single line Write a program that accepts a phone number of the form +1(xxx)-xxx-xxxx where x is a digit, and displays...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED Description: Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html), develop a program that generates all permutations for the set {1, 2, 3, 4, 5}. The output should contain all 5! = 120 permutations. Output Sample: P#1: 1 2 3 4 5 P#2: 1 2 3 5 4 P#3: 1 2 4 3 5 ... P#120: 5 4 3 2 1 Hint: - Thoroughly study the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT