Question

In: Computer Science

Exercise 5.1 Please provide code for both parts A and B. PART A Modify the P5_0.cpp...

Exercise 5.1

Please provide code for both parts A and B.

PART A

Modify the P5_0.cpp program (located below) and use the predefined function pow to compute the ab power. Here is the definition of the pow function:

double pow (double base, double exponent);

i.e. pow takes two parameters of type double, a and b and its value returned is of type double. You need to use pow in a statement like this:

p = pow(a,b)

Please note that in order to use the pow predefined function, you need to include the cmath directive, i.e. #include.

Call your new program ex51.cpp.

Imagine, you wanted to compute hundreds of these calculations in a program. Can you use a while loop in the program to do so?

PART B

Improve the program of exercise 5.1 by using a while loop that asks the user to input a and b (of any type), computes the pow(a, b)[i.e computes p = pow(a,b)] and displays the result. Call your new improved program ex52.cpp.

CODE: P5_0.cpp program

// P5_0.cpp This C++ program computes the value of ab for three cases.
#include
using namespace std;

int main(void)
{
      int i = 0;
      int a = 2, b = 4, p = 1;

      while(i < b) // computing 2^4
      {
             p = p * a;
             i++;
       }
       cout << a << " to the power of " << b << " is = " << p << endl;

      i = 0;
      p = 1;
      a = 3, b = 3;

      while(i < b) // computing 3^3
      {
             p = p * a;
             i++;
       }
       cout << a << " to the power of " << b << " is = " << p << endl;

      i = 0;
      p = 1;
      a = 5, b = 4;

      while(i < b) // computing 5^4
      {
             p = p * a;
             i++;
       }
       cout << a << " to the power of " << b << " is = " << p << endl;

       return 0;
}

Solutions

Expert Solution

Part A : exc51.cpp

Program :

#include<bits/stdc++.h>
using namespace std;

int main(void)
{  
   int i = 0;
   double a = 2, b = 4,p;
   p = pow(a,b);
cout << a << " to the power of " << b << " is = " << p << endl;
a = 3, b = 3;
p = pow(a,b);
cout << a << " to the power of " << b << " is = " << p << endl;  
a = 5, b = 4;
   p = pow(a,b);
cout << a << " to the power of " << b << " is = " << p << endl;
   return 0;
}

Output :

Imagine, you wanted to compute hundreds of these calculations in a program. Can you use a while loop in the program to do so?

Answer : Yes we can define while loop to compute hundreds of calculations in a program.please go through exc52.cpp program for clear explanation.  

Part B : exc52.cpp

Program :

#include<bits/stdc++.h>
using namespace std;

int main(void)
{  
   int i = 0,n;
   double a,b,p;
   cout << "Enter no.of times to compute values : ";
   cin >> n;
   while(i<n){
       cout << "Enter 'a' value : ";
       cin >> a;
       cout << "Enter 'b' value : ";
       cin >> b;
       p = pow(a,b);
   cout <<"\n" << a << " to the power of " << b << " is = " << p << "\n" << endl;
   i++;
   }
   return 0;
}

Output :


Related Solutions

PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. a. For the file myquiz3outf.txt, you will use an ofstream. b. For the file myquiz3outc.txt, you will capture the cout output in a file using linux commands:./quiz3 > myquiz3outc.txt string ifilename="/home/ec2-user/environment/quizzes/numbers.txt" I've posted...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. side note: ***for the string ifilename, the folder name that the fils are under is "bte320" and the "numbers.txt" that it is referencing is pasted below*** CODE: #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++ is stricter - it rquires it for exit using namespace std; int max(int a[],int dsize){ if (dsize>0){ int max=a[0]; for(int i=0;i<dsize;i++){ if(a[i]>max){ max = a[i]; }...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B 0 -$5,000,000 -5,000,000 1 $1,500,000 $1,250,000 2 $1,500,000 $1,250,000 3 $1,500,000 $1,250,000 4 $1,500,000 $1,250,000 5 $1,500,000 $1,250,000 6 $1,500,000 $1,250,000 7 $2,000,000 $1,250,000 8 0 $1,600,000 Calculate Payback period, NPV, IRR and profitability Index for the two projects Part (II) You have been hired as a consultant for Pristine Urban-Tech Zither, Inc. (PUTZ), manufacturers of fine zithers. The market for zithers is growing...
This exercise has two parts. Please respond to both. The calculation formulas and procedures for both...
This exercise has two parts. Please respond to both. The calculation formulas and procedures for both parts are included in the mini-lecture titled “Rates and Percentages” Part One The Mayors of two small towns that adjoin a large community have engaged in an argument on which of their cities is safer, with respect to its crime rate. City A has a residential population of 123,000 people and reported 185 violent crimes. City B has a residential population of 84,000 people...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising company ABC on its merger and acquisition case. The buyer company offers ABC two options. Option #1= $100 million cash at the acquisition date. Option #2 = $25 million cash at the acquisition date and another additional $90 million AFTER one year. The management team of ABC perceives a 30 percent annual discount rate. Which option should ABC choose? Show your work. Part II:...
Part I and Part II are independent. Please answer both parts. Part I: During a year...
Part I and Part II are independent. Please answer both parts. Part I: During a year of operation, a firm collects $450,000 in revenue and spends $100,000 on labor expense, raw materials, rent and utilities. The firm’s owner has provided $750,000 of her own money instead of investing the money and earning a 10 percent annual rate of return. 1A. The accounting costs of the firm are 1B. The opportunity cost is 1C. Total economic costs are 1D. Accounting profits...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue. Do not use the system(“pause”) statement. Download Source Lab 5 File:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT