Question

In: Computer Science

pleade provide the output and a brief explanation of the following C++ code: double dec1 =...


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;

Solutions

Expert Solution

//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;
}


Related Solutions

Determine the output using C++ of the following code fragment i) double *pt; double a[3] =...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] = {1.2, 2.3, 3.4}; pt = &a[1]; pt += 1; cout << *pt << endl; ii) int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { cout << num << "bb"; num *= 3; } cout << endl; i++; }
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials * * 2. Adding polynomials * * 3. Multiplying polynomials. * * 4. Displaying polynomials * * 5. Clearing polynomials. * * 6. Quit. * *********************************** Select the option (1 through 6): 7 You should not be in this class! ************************************* *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials...
the following three concepts and provide a definition and brief explanation of their importance in microeconomic...
the following three concepts and provide a definition and brief explanation of their importance in microeconomic analysis. Economies of Scope vs. Economies of Scale; Transitivity Assumption of preference orderings; Short-run Expansion Path.
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++ 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...
• Based on the following code snippet and plan attributes, provide the output for Plan A,...
• Based on the following code snippet and plan attributes, provide the output for Plan A, Plan B and Plan C. Code Snippet • {{IF:[Copay]=Not Covered && [Coinsurance]=Not Covered, show ‘Not covered’}} • {{IF:[Copay]!=$0.00 && [Copay]!=Blank, show value from {{Copay}} copayment}} • {{IIF:[Copay]!=$0.00 && [CopayFrequency]!=Blank, show {{Copay}} copayment/{{CopayFrequency}}}} • {{IF:[CopayMax]=$0.00 OR [Copay]=$0.00, show No Charge}} • {{IF:[[CopayMax]!=$0.00 && [CopayMax] contains ‘$’ && [Coinsurance] contains ‘%’ && [CopayMaxFrequency]=Blank, show {{CopayMax}} copayment then {{Coinsurance}} coinsurance]}} Plan Copay CopayFrequency CopayMax CopayMaxFrequency Coinsurance Plan...
Provide a brief description/explanation about the following: A) The cortical reaction up to blastulation B) Direct...
Provide a brief description/explanation about the following: A) The cortical reaction up to blastulation B) Direct exchange with the environment vs indirect (systemic) change C) Communication of neurons with other cells
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Provide a brief explanation of various perspectives of market failures with regard to the environment.
Provide a brief explanation of various perspectives of market failures with regard to the environment.
Provide a brief history of the Federal Reserve, including an explanation of why it was created....
Provide a brief history of the Federal Reserve, including an explanation of why it was created. Explain what the dual mandate is and how it guides the actions of the Federal Reserve Money supply Describe the goals of the Federal Reserve’s monetary policy Explain the tools used to regulate the money supply Relationship with Congress Explain who controls fiscal policy and how it attempts to close a recessionary gap. Describe why it is important for the Federal Reserve to maintain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT