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

C++ Will Upvote Correct Solution! A) Fix any errors to get the following program

ID: 3749015 • Letter: C

Question

C++

Will Upvote Correct Solution!

A) Fix any errors to get the following program to run in your environment. B) Document each line of code with comments and descibe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. Include a copy of the output after your summary.

#include <iostream>
using namespace std;

int main()
{
int num1 = 1, num2 = 2, num3 = 3;
const int* p1 = &num1;
int* const p2 = &num2;
const int* const p3 = &num3;

   num1 = 11;
   num2 = 22;
   num3 = 33;
  
   cout << num1 << " " << num2 << " " << num3 << endl;
   cout << p1 << " " << p2 << " " << p3 << endl;
   cout << *p1 << " " << *p2 << " " << *p3 << endl;

   *p1 = *p2;
   *p2 = *p3;
   *p3 = *p1;
  
   cout << num1 << " " << num2 << " " << num3 << endl;
   cout << p1 << " " << p2 << " " << p3 << endl;
   cout << *p1 << " " << *p2 << " " << *p3 << endl;
   
   *p2 = 22;
  
   p1 = p2;
   p2 = p3;

   p3 = p1;
  
   cout << num1 << " " << num2 << " " << num3 << endl;
   cout << p1 << " " << p2 << " " << p3 << endl;
   cout << *p1 << " " << *p2 << " " << *p3 << endl;

}

Explanation / Answer

I have commented after every line.

Remove error lines to get correct output :)

#include <iostream>
using namespace std;

int main()
{
int num1 = 1, num2 = 2, num3 = 3; //int variables
const int* p1 = &num1; //pointer to integer constant
int* const p2 = &num2; //constant pointer to integer
const int* const p3 = &num3; //constant pointer to constant integer

num1 = 11; //can change the value of variable but *p1=11 will give error
num2 = 22; //can change variable value but p2++ will give error
num3 = 33; //can change variable value but *p3++ or p3++ will give error
  
cout << num1 << " " << num2 << " " << num3 << endl; //print the variable value
cout << p1 << " " << p2 << " " << p3 << endl; //print the address stored by pointers
cout << *p1 << " " << *p2 << " " << *p3 << endl; //print the value pointed by pointers after variable value updation


*p1 = *p2; //will give error, as p1 is pointer to const integer, can change the constant int value
*p2 = *p3; //ok
*p3 = *p1; //will give error, as p1 is pointer to const integer, can change the constant int value
  
cout << num1 << " " << num2 << " " << num3 << endl; //print variables
cout << p1 << " " << p2 << " " << p3 << endl; //print
cout << *p1 << " " << *p2 << " " << *p3 << endl;

*p2 = 22; //ok, this constant pointer to integer
  
p1 = p2; //ok
p2 = p3; //will give error, this is constant pointer, it cant be made to point to something else

p3 = p1; //will give error, this is constant pointer, it cant be made to point to something else
  
cout << num1 << " " << num2 << " " << num3 << endl; //print variables
cout << p1 << " " << p2 << " " << p3 << endl; //print address stored by pointers
cout << *p1 << " " << *p2 << " " << *p3 << endl; //print values pointed by pointers

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote