C Programming You must use a while loop as instructed for th

C++ Programming: You must use a while loop as instructed for this task. Do not use functions, arrays or struct statements. Please write the code with same format in the code below.

Here is the code that needs to be modified:

//header files
#include <iostream>

using namespace std;

//main function   
int main()   

{
//variable declaraion
double radius; //the radius of the trough\'s end
double width; //the width of the trough   
double volume; //the volume of water   

//user input   
cout << \"What is the radius of the trough (in meters)? \";
cin >> radius;
cout << \"What is the width of the trough (in meters)? \";
cin >> width;

//calculation
volume = 0.5 * 3.1415 * radius * radius * width * 1000;

//print output   
cout << \"Your trough can hold \" << volume
<< \" L of water.\" << endl;

return 0;
}

Here is the task:

1. (10 marks) Modify the sample solution program to Question 1 of Assignment 2 to obtain a program that will meet the following specifications. Your program must prompt the user to enter the dimensions of a semi-circular trough in meters then determine the volume of water the trough can hold in liters, repeatedly until a non-positive value is entered for wither dimension. You must use a while loop to implement the program repeatedly asking for trough dimensions until a non-positive integer is entered. Your program should act EXACTLY as seen in the test runs below (including alignment) Notes: You may let TT 3.1415, 1 m 1000 L, and the volume of the pictured trough is V Tr W Test run 1 (0.25 radius and 1.5 width input by the user): Trough Volume Calculator Enter the radius and width in meters (space delimited) 0.025 1.5 Your trough can hold 147.258 L of water Enter the radius and width in meters (space delimited) 0.42 3 Your trough can hold 83 1.241 L of water. Enter the radius and width in meters (space de limited) 0.1 63 Your trough can hold L of water The user terminate his program Test run 2 (0.42 radius and 3 width input by the user): Trough volume Calculator Enter the radius and width in meters (space delimited) 0 4.2 The user terminated this program. HINT: Change the program to implement the change in prompts first. When that is working change the program by adding a while loop to classify five integers repeatedly. Carefully consider what must happen before entering the loop, inside the loop, and after the loop.

Solution

Following is the required code. Please comment below, if some changes are required

#include <iostream>
using namespace std;

//main function                                                               
int main()                                                 

{
   //variable declaraion                                                      
   double radius; //the radius of the trough\'s end                            
   double width; //the width of the trough                                   
   double volume; //the volume of water                                       

   //initial text to print
   cout << \"Trough Volume Calculator\" << endl;
   cout << \"========================\" << endl;

   //TO MATCH EXTACTLY, assuming that, if input is wrong for the first time
   //it directly terminates, else it prints as shown in third example of test run 1

   bool firstInput = true;

   while( true ){
       //take user input                                                               
       cout << \"Enter the radius and width in meters (space delimited) : \";
       cin >> radius >> width;

       if( radius <= 0 || width <= 0 ){
              if( not firstInput ){
                  cout << \"    Your trough can hold L of water.\" << endl;
              }
              break;
       }

       //calculation                                                              
       volume = 0.5 * 3.1415 * radius * radius * width * 1000;
       //print output                                                             
       cout << \"    Your trough can hold \" << volume
            << \" L of water.\" << endl;

       firstInput = false;
   }

   cout << \"*** The user terminated this program. ***\" << endl;
   return 0;
}

C++ Programming: You must use a while loop as instructed for this task. Do not use functions, arrays or struct statements. Please write the code with same forma
C++ Programming: You must use a while loop as instructed for this task. Do not use functions, arrays or struct statements. Please write the code with same forma

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site