Question

Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...

Lab 7: Void and Value-Returning Functions

Lab 7: Void and Value-returning functions              Due date: 11/6/19

Problem: Write a C++ program that calculates the areas of a rectangle and of an ellipse.


                  

Area = base * height             Area = π * radius a * radius b

Note: all images extracted from http://www.mathsisfun.com/area-calculation-tool.html

------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below.
------------------------------------------------------------------------------------------------------------------------ Part A (79 points)

Algorithm solution (in pseudocode):

To accomplish this task, your program will have to take the following steps:

1.   Declare a global constant named PI equal to 3.141592;
2.   Declare variables named base, height, radiusa, radiusb, rec_area, and elli_area that hold double precision real numbers.
3.   Print on the screen “For the rectangle”
4.   Call void function getData(base, height) that gets two lengths from the keyboard and returns them to main( ).
5.   Print on the screen “For the ellipse”
6.   Call void function getData(radiusa, radiusb) that gets two lengths from the keyboard and returns them to main( ).
7.   Calculate the area of the rectangle and store the result in rec_area.
8.   Calculate the area of the ellipse and store the result in elli_area.
9.   Call void function printData(rec_area, elli_area) that receives the two areas and prints on the screen the message:

"The area of the rectangle is ", rec_area
"The area of the ellipse is ", elli_area

Function getData(rpar1, rpar2 ) must:

1.   Prompt the user to "Please enter two lengths: "
2.   Get both values from the keyboard and store them in rpar1 and rpar2.

Note: rpar1 and rpar2 are reference parameters.
Function printData(vpar1, vpar2) must:

1.   Format the output to display the numbers in fixed format with one decimal digit.
2.   Print the message on the screen.

Note: vpar1 and vpar2 are value parameters.

IMPORTANT:
See examples of void functions in the textbook and on blackboard to get a starting point.

------------------------------------------------------------------------------------------------------------------------

Part B (16 points)

Modify the above program so that the areas are calculated by a couple of value-returning functions named area_rectangle( ) and area_ellipse( ). Each of these functions must call getData() to get the values, calculate the area, and return it through the function’s value.

IMPORTANT: steps 3 through 8 specified in Part A should be replaced by a couple of statements where these functions are called.

------------------------------------------------------------------------------------------------------------------------

Part C (5 points)

Modify the above program so that:
printData() sends the output to an output file named output7.txt.

Note: The file must be opened in main() and passed to printData() as an argument. Don’t forget to close it at the end of your program. Make sure you check if the file was opened or not (if it was not opened display a message and stop the program).

------------------------------------------------------------------------------------------------------------------------

Example:


    For the rectangle
Please enter two lengths: 1.23 3.56

For the ellipse Please enter two lengths: 3.1 5.19

The area of the rectangle is 4.4

The area of the ellipse is 50.5
The program must compile without errors or warnings.
I am posting the executable of my solutions for your reference. Please run them and ensure that your programs work like mine.
Your program must have the following comments at the top. Don’t forget to include them because they will count toward the grade of this lab.

//******************************************************************************
// Team #             CSCI 1380.01     Fall 2019       Lab # 7
// First and Last Name
// First and Last Name (of teammate)
// Using your own words, write here a description of what the program does.
//
//******************************************************************************


IMPORTANT!:

_ Submit the most complete version of your program followed by your previous solutions commented out below main(). For example, if you can complete up to part C, submit this program with what you did for parts A and B commented out below the body of main().
_ Please name your file lab7TXX (where XX are the two digits corresponding to your team number). Do not include blank spaces in the name of the file please.



When done, submit your solution through Blackboard using the “Assignments” tool. Do NOT email it.


The following is the basic criteria to be used to grade your submission:

You start with 100 points and then lose points as you don't do something that is required.

Part A (79 points)
         
Didn't implement the required function                      -20 each
No comments or too few comments.                       -5    Wrong implementation of function getData()                   -10    Wrong implementation of function printData()                   -10      
Didn't use parameters or function value data type correctly (example, reference instead of value parameter) -5
Incorrect function name                            -3    Incorrect function call                             -5      
Use of reference parameters in a value-returning function or printData().        -5      
PI not declared as a global constant                       -3      
Wrong output format                             -2      
Mixed data types in expression                          -3
      
Part B (10 points)
      
Functions area_rectangle( ) and area_ellipse( ) should have empty parameter lists.     -5      
Didn't create the functions                            -8      
Wrong implementation of functions area_rectangle( ) and/or area_ellipse().       -6      
Incorrect function call                             -2      
Missing part B                               -10

Part C (11 points)
      
File not passed to printData() as an argument.                   -5    Didn't check if the file was opened.                       -5
Didn't work with file                            -9    Didn't close file                               -2      
Missing part C                               -11      
Incomplete part C                               -5

Late                                     -10

Important: more points may be lost for other reasons not specified here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

1)


#include <iostream>
#include <iomanip>
using namespace std;

const double PI = 3.141592;

void getData(double &base,double &height);
void printData(double rec_area,double elli_area);
int main() {
   //setting the precision to two decimal places
   std::cout << std::setprecision(1) << std::fixed;
     
   //Declaring variables
double base, height, radiusa, radiusb, rec_area,elli_area;
  
cout<<"For the rectangle ";
getData(base, height);
   cout<<"For the ellipse ";
getData(radiusa,radiusb);

rec_area=base*height;
   elli_area= PI * radiusa * radiusb;
   printData(rec_area,elli_area);
      
   return 0;
}
void getData(double &base,double &height)
{
   cout<<"Please enter two lengths: ";
   cin>>base>>height;
}
void printData(double rec_area,double elli_area)
{
  
cout<<"The area of the rectangle is "<<rec_area<<endl;
cout<<"The area of the ellipse is "<<elli_area<<endl;
}

______________________

Output:

_____________________

b)


#include <iostream>
#include <iomanip>
using namespace std;

const double PI = 3.141592;

void getData(double &base,double &height);
double area_rectangle(double base,double height);
void printData(double rec_area,double elli_area);
double area_ellipse(double radiusa,double radiusb);
double area_rectangle(double base,double height);

int main() {
   //setting the precision to two decimal places
   std::cout << std::setprecision(1) << std::fixed;
     
   //Declaring variables
double base, height, radiusa, radiusb, rec_area,elli_area;
  
cout<<"For the rectangle ";
getData(base, height);
   cout<<"For the ellipse ";
getData(radiusa,radiusb);
  
  
rec_area=area_rectangle(base,height);
  
   elli_area=area_ellipse(radiusa,radiusb);
   printData(rec_area,elli_area);
      
   return 0;
}
void getData(double &base,double &height)
{
   cout<<"Please enter two lengths: ";
   cin>>base>>height;
}
void printData(double rec_area,double elli_area)
{
  
cout<<"The area of the rectangle is "<<rec_area<<endl;
cout<<"The area of the ellipse is "<<elli_area<<endl;
}
double area_rectangle(double base,double height)
{
   return base*height;
}
double area_ellipse(double radiusa,double radiusb)
{
return PI * radiusa * radiusb;  
}
__________________________

c)

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const double PI = 3.141592;

void getData(double &base,double &height);
double area_rectangle(double base,double height);
void printData(ofstream &outfile,double rec_area,double elli_area);
double area_ellipse(double radiusa,double radiusb);
double area_rectangle(double base,double height);

int main() {
   //setting the precision to two decimal places
   std::cout << std::setprecision(1) << std::fixed;

   ofstream outFile;
   //Declaring variables
double base, height, radiusa, radiusb, rec_area,elli_area;
  
cout<<"For the rectangle ";
getData(base, height);
   cout<<"For the ellipse ";
getData(radiusa,radiusb);
  
  
rec_area=area_rectangle(base,height);
  
   elli_area=area_ellipse(radiusa,radiusb);
          //setting the precision to two decimal places
   outFile << std::setprecision(1) << std::fixed;
     
   outFile.open("output7.txt");
   printData(outFile,rec_area,elli_area);
  
   outFile.close();
      
   return 0;
}
void getData(double &base,double &height)
{
   cout<<"Please enter two lengths: ";
   cin>>base>>height;
}
void printData(ofstream &outFile,double rec_area,double elli_area)
{
  
cout<<"The area of the rectangle is "<<rec_area<<endl;
cout<<"The area of the ellipse is "<<elli_area<<endl;
outFile<<"The area of the rectangle is "<<rec_area<<endl;
outFile<<"The area of the ellipse is "<<elli_area<<endl;
}
double area_rectangle(double base,double height)
{
   return base*height;
}
double area_ellipse(double radiusa,double radiusb)
{
return PI * radiusa * radiusb;  
}
_______________________________

Output:

__________________________

// output file :


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

    c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did the program Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions (names and purposes given below). Fot o tentoutefill datere nedefremfite You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or...

  • Write a program named program51.py that defines a value-returning function named cuber that returns both the...

    Write a program named program51.py that defines a value-returning function named cuber that returns both the surface area and volume of a cube. A cube is a rectangular prism with all sides equal. Prompt the user to enter the cube's side length from the keyboard in the main function and then call the cuber function. Moving forward for all assignments and for this program, all other code would be in a main function, called at the very end of your...

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Problem: Create a program that contains two functions: main and a void function to read data...

    Problem: Create a program that contains two functions: main and a void function to read data from a file and process it. Your main function should prompt the user to enter the name of the file (Lab7.txt), store this name, and then call the function sending the name of the file to the function. After the function has completed, the main function should output the total number of values in the file, how many of the values were odd, how...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the...

    *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the entered number is 0 or negative 5. Ask the user to enter a positive int between 0 and 10 inclusive 6. If the number is < 1, it terminates. 7. Else: call the above four functions 8. Print the result of each function after its call. Q2. Write a program that removes the punctuations letters from a file and display the output to the...

  • Calculate the area of a rectangle. -          Utilize at least three functions, “Get Input” – “Calculation” –...

    Calculate the area of a rectangle. -          Utilize at least three functions, “Get Input” – “Calculation” – “Show Output” -          The “Get Input” function must use call-by-reference parameters to gather data -          The program must ask the user if they would like to run it again This is a C++ Project using call by reference. If possible please make it as simple as possible as i'm already very confused the way it is.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT