Question

In: Computer Science

What is the output of the following C++ code? int* length; int* width; length = new...

What is the output of the following C++ code?

int* length;
int* width;

length = new int;
*length = 5;
width = length;
length = new int;
*length = 2 * (*width);

cout << *length << " " << *width << " "
     << (*length) * (*width)
     << endl;

Solutions

Expert Solution

Below is the line by line explaination of the above code -

int* length;
int* width;

Two pointer variables with name length and width are declared here

length = new int;

Pointer variable named length is assigned a memory in the program and it is now pointing(referencing) to a memory block which can hold integral value

*length = 5;

Value of 5 is stored at the memory location pointed by length

width = length;

Pointer variable named width is now pointing to the memory location pointed by length

length = new int;

Pointer variable named length is now assigned a new memory location in the program Note that the previous memory block which contains value of 5 is still pointed by width

*length = 2 * (*width);

Value to be stored at memory location pointed by length is will be two times the value at memory location pointed by width that is two times of 5 that is 10

cout << *length << " " << *width << " "
<< (*length) * (*width)
<< endl;

On printing above values output is -

10 5 50

As value at memory location pointed by length is 10,  value at memory location pointed by width is 5 and product of these 2 values is 50

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.


Related Solutions

1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1,...
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1, i = 6; while (i < 9) {       product = product * i;       i++; } cout << “i is : ” << i << endl; cout << “product is : ” << product << endl; 6.    What is the output of the following code: int product = 1, i = 6;      do {       product = product * i;       i++;...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages,...
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages, 55) Append(ages, 88) Append(ages, 66) Print(ages) A. 55, 66, 88 B. 55, 88, 66 C. 55, because there is no loop D. 66, because there is no loop E. None of the above. QUESTION 2 Type the list after the given operations. Each question starts with an empty list. Type the list as: 5, 7, 9 Append(list, 3) Append(list, 2) Append(list, 1) Remove(list, 3)...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele) { stk.push(ele); length++; } if(length%2) stk.pop(); for (int i=0;i<length/2;i++) { ele=stk.top(); stk.pop(); stk2.push(ele); } int flag=1; for(int i=0;i<length/2;i++) { if(stk.top()==stk2.top()) { stk.pop();stk2.pop(); } else { flag=1; break; } } if(flag==1) cout<<"NOT palindrome"; else cout<<"palindrome"; }
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT