In: Computer Science
pleade provide the output and a brief explanation of the following
C++ code:
double dec1 = 2.5;
double dec1 = 3.8;
double *p, *q;
p = &dec1;
*p = dec2 - dec1;
q = p;
*q = 10.0;
*p = 2 * dec1 + (*q);
q =&dec2;
dec1 = *p + *q;
cout<<dec1<<" "<<dec2<< endl;
cout<<*p<<" "<<*q<< endl;
The following code should output the radius of th ebase, height , volume, and surface area of a cylinder. However, it fails to do so. Correct this code to accomplish teh desired results.After correcting the code, what is the output? Do not alter the values assigned to the radius of the base and the height of the cylinder.(2,3,6)
double *baseRadius;
double *height;
cout<<fixed<<showpoint<<setprecision(2);
baseRadius = new double;
*baseRadius = 1.5;
height = new double;
*height = 2 * (*baseRadius);
baseRadius = new double;
*baseRadius = 4.0;
cout<<"Radius of the base:"
<<baseRadius<<endl;
cout<<"Height:" << height <<endl;
cout<<"Volume:" << 3.14 *(baseRadius) *
(baseRadius)<<endl;
cout<<"Surface area:" << 2*3.14*(baseRadius) * (height)
<<endl;
//code 1
//importing header files
#include<iostream>
using namespace std;
//main function
int main()
{
//declaring dec1 double type variable having value 2.5
double dec1 = 2.5;
//declaring dec2 double type variable having value 3.8
//In you code previously it was written as again double dec1 which
will create error so changing 2nd double dec1 to dec2
double dec2 = 3.8;
//declaring two double type pointers p and q that means they will
point to the variable of double type
double *p, *q;
//now storing the address of dec1 in p that means now p will point
to variable dec1
p = &dec1;
//changing the value at the location pointed by p to
dec2-dec1
//Basically p represent the pointer pointing to dec1 and *p
represents the value at that location of dec1
//so changing *p changes value at dec1 location so dec1
becomes=dec2-dec1=>3.8-2.5=>1.3
*p = dec2 - dec1;
//now setting q to p
//now q will also points to the location to which p is pointing
that is dec1
q = p;
//changing the value at the location pointed by q
//as we know that q points to dec1 so changing *q that is dec1 to
10
//now dec1=*p=*q= 10 after all *p also points to dec1
*q = 10.0;
//changing the value of * p that is obviously dec1 to 2*dec1
+(*q)
//dec1 is 10, *q is also 10 as it points to dec1
//so changing *p to 2*10+10=20+10=30
//now *p=*q=dec1=30
*p = 2 * dec1 + (*q);
//now assigning the address of dec2 to the pointer q that means q
will now points to dec2 location
q =&dec2;
//changing dec1 value to *p+*q
//now p points to dec1 which now has value 30, q points to dec2
which means it has value 3.8
//dec1 will be *p+*q=>30+3.8=33.8
dec1 = *p + *q;
//printing the value of dec1 and dec2
//dec1 is 33.8 and dec2 is 3.8
cout<<dec1<<" "<<dec2<< endl;
//printing the value of *p and *q
//now p points to dec1 so it has value same as dec1 that is
33.8
//q points to dec2 so it has value same as dec2 that is 3.8
cout<<*p<<" "<<*q<< endl;
return 0;
}
//code 2
//header files
#include<iostream>
#include<iomanip>
using namespace std;
//main function
int main()
{
//declaring two pointers of double type
double *baseRadius;
double *height;
//It is used to set the formatting of the output
cout<<fixed<<showpoint<<setprecision(2);
//now allocating memory of double to it
baseRadius = new double;
//setting the value to 1.5
*baseRadius = 1.5;
//allocating memory to height pointer of double
height = new double;
//setting the value of *height to 2*(*baseRadius) that is 2*
1.5=3.0
*height = 2 * (*baseRadius);
//again allocating memory of double to baseRadius
baseRadius = new double;
//setting its value to 4.0
*baseRadius = 4.0;
//printing the radius of base
//previously it was written like baseRadius only to print so that
will print the address of the variable
//it is a pointer so if we want to access its value we have to use
* with its name
//so changed baseRadius to *baseRadius
cout<<"Radius of the base:"
<<*baseRadius<<endl;
//printing the height
//again same mistake was done previously
//height represent address so changes it to *height to access its
value
cout<<"Height:" << *height <<endl;
/*Here i have changed the formula of volume and surface area of
the cylinder
because the formula which you have used in the code given in the
question was wrong
so its my duty to correct it and specify that to you as well.
*/
//printing the volume of cylinder which is
3.14*radius*radius*height
//previously baseRadius was used so that will give the error in the
program as 3.14(double) is multiplied by double* type
//so here accessing the value of radius as *baseRadius as it is a
pointer
cout<<"Volume:" << 3.14 *(*baseRadius) *
(*baseRadius)*(*height)<<endl;
//printing the surface area of cylinder which is
2*3.14*radius*(radius+height)
//previously baseRadius and height was used which will obviously
produce the error as mentioned above because of the
//multiplication of double type value with double*
//so here accessing the value of radius as *baseRadius and height
as *height as both are pointer
cout<<"Surface area:" << 2*3.14*(*baseRadius) *
((*baseRadius)+(*height)) <<endl;
return 0;
}