Question

In: Computer Science

Given the following code segment, tell the output if it is part of an executable program....

Given the following code segment, tell the output if it is part of an executable program. Assume the addresses of the variables, a and b, are 0012A32A and 0012A340, respectively.

       int* intPtr1, *intPtr2;

       int a = 10, b;

       intPtr1 = &a;

       cout<< "The data in a is "<<a<<endl;

       cout << "The address of a is "<<&a<<endl;

       cout <<"The data in intPtr1 is "<< intPtr1 <<endl;

       cout <<"The data in the variable pointed by intPtr1 is "<< * intPtr1<<endl;

       a++;

       cout << "After changing a: "<<endl;

       cout <<"The data in the variable pointed by intPtr1 is "<< * intPtr1<<endl;

       b = *intPtr1;

       cout << "The value in b is "<<b<<endl;

       a++;

       cout << "After changing a: "<<endl;

       cout <<"The data in the variable pointed by intPtr1 is "<< * intPtr1<<endl;

       cout << "The value in b is "<<b<<endl;

Output:

     

Please Use C++

Solutions

Expert Solution

The data in a is 10
The address of a is 0012A32A
The data in intPtr1 is 0012A32A
The data in the variable pointed by intPtr1 is 10
After changing a: 
The data in the variable pointed by intPtr1 is 11
The value in b is 11
After changing a: 
The data in the variable pointed by intPtr1 is 12
The value in b is 11

Explanation inside comment:

int *intPtr1, *intPtr2;

        int a = 10, b;
        //initialize a = 10

        intPtr1 = &a;
        //intptr1 stores address of variable a
    //intptr1 = 0012A32A

        cout << "The data in a is " << a << endl;
        //print value stored in a = 10

        cout << "The address of a is " << &a << endl;
        //print address of a = 0012A32A

        cout << "The data in intPtr1 is " << intPtr1 << endl;
        //print value stored in intPtr1 which is address of a = 0012A32A

        cout << "The data in the variable pointed by intPtr1 is " << *intPtr1 << endl;
        //print value stored at address in intPtr1 = 10

        a++;
        //increment a; a = 11
        cout << "After changing a: " << endl;

        cout << "The data in the variable pointed by intPtr1 is " << *intPtr1 << endl;
        //print value stored at address in intPtr1 = 11

        b = *intPtr1;
        //b = value stored at intPtr1 => b = 11

        cout << "The value in b is " << b << endl;
        //print value stored in b = 11

        a++;
        //increment a; a = 12 only a value is changed here

        cout << "After changing a: " << endl;

        cout << "The data in the variable pointed by intPtr1 is " << *intPtr1 << endl;
        //print value stored at address in intPtr1 = 12

        cout << "The value in b is " << b << endl;
        //print value stored in b = 11

Related Solutions

What would the output be for the following program code segment? #define ROWS  3      #define COLS 3...
What would the output be for the following program code segment? #define ROWS  3      #define COLS 3 void addCoulmns(int arr[][COLS],int Sum[COLS]); int main(void) {        int array[ROWS][COLS] = {{ 10, 35, 23 },                                   { 5, 4, 57 },                                   { 4, 7, 21 }};        int ColSum[ROWS];        int rows;        int columns;        addCoulmns(array,ColSum);        for (columns = 0; columns < COLS - 1; columns++)               printf("%d\n",ColSum[columns]);        return(0); } void addCoulmns(int arr[][COLS],int Sum[COLS]) {        int row;        int col;        for (col = 0; col < COLS -...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back('...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back(' ') l = ['A', 'L', 'E', 'S', 'T', 'E'] for i in l: → if ord(i) % 3 == 0: → → dq.push_back(i) → elif ord(i) % 5 == 0: → → dq.push_front(i) → → dq.push_back(i) → elif ord(i) % 4 == 0: → → dq.push_back(i) → else: → → dq.peek_front() → → dq.push_front('X') → → dq.push_back('R') dq.push_front(dq.peek_back()) dq.pop_back() print(dq)
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 =...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Show what is written by the following segment of code, given item1, item2, and item3 are...
Show what is written by the following segment of code, given item1, item2, and item3 are int variable: item1 = 4; item3= 0; item2 = item1 + 1; stack.Push(item2); stack.Push(item2 +1); stack.Push(item1); item2 = stack.Top(); stack.Pop(); item1 = item2 + 1; stack.Push(item1); Stack.Push(item3); while (!stack.IsEmpty()) {      item3 = stack.Top():      cout <<item3<<endl;      stack.Pop(); } cout<<item1 <<endl <<item2<<endl<<item3<<endl;
I. Given the following code segment below what is the best description of line 5 and...
I. Given the following code segment below what is the best description of line 5 and line 6? Identify the public and private members. 1. #include <iostream.h> 2. class SimpleCat 3. { 4. public: 5. SimpleCat (int age, int weight); 6. ~SimpleCat(){} 7. int GetAge() {return itsAge;} 8. int GetWeight() {return itsWeight;} 9. private: 10. int itsAge; 11. int itsWeight; 12. }; II. Multiple Choice questions 1. A function that is called automatically each time an object is created or...
Edit the given program to produce the following output in c++ mode: - Take in the...
Edit the given program to produce the following output in c++ mode: - Take in the name of a superhero & tell him how many villains he/she has to defeat today - The number of villains is randomly generated and should be a number between 11 and 42. - Use a seed of 7. Hint: Compile the program first before making edits What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT