Question

In: Computer Science

Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...

Task #1 void Methods

  1. Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this.

  1. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.   

  1. Below the main, write the function definition for PrintMenu.  It should simply print out instructions for the user with a menu option for the user to choose from. The menu should appear to the user as:

This is a geometry calculator.

Choose what you would like to calculate.

  1. Find the area of a circle

  1. Find the area of a rectangle

  1. Find the area of a triangle

  1. Find the circumference of a circle

  1. Find the perimeter of a rectangle

  1. Find the perimeter of a triangle

Enter the number of your choice:

  1. Add a line in the main method that calls the PrintMenu function as indicated by the comments.

  1. Compile, debug and run. You should be able to choose any option, but you will always get 0 for the answer. We will fix this in the next task.

Task #2 Value-Returning Functions

  1. Write a function prototype and a function definition for CircleArea that takes the radius of the circle and returns the area using the formula A = πr2.

  1. Write a function prototype and a function definition for RectangleArea that takes in the length and width of the rectangle and returns the area using the formula A = lw.

  1. Write a function prototype and a function definition for TriangleArea that takes in the base and height of the triangle and returns the area using the formula A = ½ bh.

  1. Write a function prototype and a function definition for CircleCircumference that takes in the radius of the circle and returns the circumference using the formula C = 2πr.

  1. Write a function prototype and a function definition for RectanglePerimeter that takes the length and the width of the rectangle and returns the perimeter of the rectangle using the formula P = 2l + 2w.

  1. Write a function prototype and a function definition for TrianglePerimeter that takes the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.

Task #3 Calling Functions

  1. Add lines in the main which will call the functions. The comments indicate where to place the function calls.

  1. Compile, debug, and run.  Test out the program using your sample data.

Task #4 Reusable and Well-documented Functions

  1. Create a header file called formulas.   

  1. Remove #pragma once as this is non-standard and will not work in every environment.  Add the header file guards  

#ifndef FORMULAS_H

#define FORMULAS_H

  1. Cut the function prototypes for the six functions that calculate values from the geometry file and paste after the header file guards.

  1. End the file with #endif

  1. In the geometry file, add a preprocessor directive (after the other #include lines)’

#include “formulas.h”

  1. Add comments for each of the function prototypes you just moved. They should include:

  1. A one-line summary of what the function does.

  1. A description of what the function requires to operate and what the result of that operation is.

  1. A listing of each parameter and what that parameter represents.

  1. If a value is returned, what that value represents

  1. Create a new file called formulas.cpp.

  1. Cut the function definitions that match the prototypes you just moved and paste them into the new file.

  1. Copy the comments from each function prototype to the corresponding function definition.

  1. Compile, debug, and run. It should operate as previously.   

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

#include <iostream>
using namespace std;

int main()
{
   int choice;           //the user's choice
   double value = 0;   //the value returned from the method
   char letter;       //the Y or N from the user's decision to exit
   double radius;       //the radius of the circle
   double length;       //the length of the rectangle
   double width;       //the width of the rectangle
   double base;       //the base of the triangle
   double height;       //the height of the triangle
   double side1;       //the first side of the triangle
   double side2;       //the second side of the triangle
   double side3;       //the third side of the triangle

   //do loop was chosen to allow the menu to be displayed first
   do
   {
       //call the printMenu function

       cin >> choice;

       switch (choice)
       {
       case 1:
           cout << "Enter the radius of the circle: ";
           cin >> radius;

           //call the circleArea function and store the result in value

           cout << "The area of the circle is " << value << endl;
           break;

       case 2:
           cout << "Enter the length of the rectangle: ";
           cin >> length;
           cout << "Enter the width of the rectangle: ";
           cin >> width;

           //call the rectangleArea function and store the result in value

           cout << "The area of the rectangle is " << value << endl;
           break;

       case 3:
           cout << "Enter the height of the triangle: ";
           cin >> height;
           cout << "Enter the base of the triangle: ";
           cin >> base;

           //call the triangleArea function and store the result in value

           cout << "The area of the triangle is " << value << endl;
           break;
      
       case 4:
           cout << "Enter the radius of the circle: ";
           cin >> radius;

           //call the circleCircumference function and store the result in value

           cout << "The circumference of the circle is " << value << endl;
           break;

       case 5:
           cout << "Enter the length of the rectangle: ";
           cin >> length;
           cout << "Enter the width of the rectangle: ";
           cin >> width;
           //call the rectanglePerimeter function and store the result in value

           cout << "The perimeter of the rectangle is " << value << endl;
           break;

       case 6:
           cout << "Enter the length of side 1 of the triangle: ";
           cin >> side1;
           cout << "Enter the length of side 2 of the triangle: ";
           cin >> side2;
           cout << "Enter the length of side 3 of the triangle: ";
           cin >> side3;

           //call the trianglePerimeter function and store the result in value

           cout << "The perimeter of the triangle is " << value << endl;
           break;

       default:
           cout << "You did not enter a valid choice." << endl;
       }

       cout << "Do you want to exit the program (Y/N)? ";
       cin >> letter;

   } while (letter != 'Y' && letter != 'y');
}

Solutions

Expert Solution

Here is the solution,

// geometry.cpp file

#include "formulas.h"
#include <iostream>
using namespace std;

int main()
{
// variable decalrations
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double base; //the base of the triangle
double height; //the height of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle

//do loop was chosen to allow the menu to be displayed first
do
{
//call the printMenu function
printMenu();
cin >> choice;

switch (choice)
{
case 1:
cout << "Enter the radius of the circle: ";
cin >> radius;

//call the circleArea function and store the result in value
value = CircleArea(radius);
cout << "The area of the circle is " << value << endl;
break;

case 2:
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;

//call the rectangleArea function and store the result in value
value = RectangleArea(length, width);
cout << "The area of the rectangle is " << value << endl;
break;

case 3:
cout << "Enter the height of the triangle: ";
cin >> height;
cout << "Enter the base of the triangle: ";
cin >> base;

//call the triangleArea function and store the result in value
value = TriangleArea(base,height);
cout << "The area of the triangle is " << value << endl;
break;

case 4:
cout << "Enter the radius of the circle: ";
cin >> radius;

//call the circleCircumference function and store the result in value
value = CircleCircumference(radius);
cout << "The circumference of the circle is " << value << endl;
break;

case 5:
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//call the rectanglePerimeter function and store the result in value
value = RectanglePerimeter(length,width);
cout << "The perimeter of the rectangle is " << value << endl;
break;

case 6:
cout << "Enter the length of side 1 of the triangle: ";
cin >> side1;
cout << "Enter the length of side 2 of the triangle: ";
cin >> side2;
cout << "Enter the length of side 3 of the triangle: ";
cin >> side3;

//call the trianglePerimeter function and store the result in value
value = TrianglePerimeter(side1,side2,side3);
cout << "The perimeter of the triangle is " << value << endl;
break;

default:
cout << "You did not enter a valid choice." << endl;
}

cout << "Do you want to exit the program (Y/N)? ";
cin>>letter;

} while (letter != 'Y' && letter != 'y');
}

// geometry.cpp ends

// your formulas.cpp

#include <iostream>
#include <string>
#include "formulas.h"
using namespace std;

// function to calculate area of circle
// the function requires radius.
float CircleArea(float radius){
return (2 * 3.14 * radius * radius);
}

// function to calculate are of rectangle
// the function requires length and width
float RectangleArea(float length, float width){
return length * width;
}

// function to calculate area of triangle
// the function requires base and height
float TriangleArea(float base, float height){
return (1/2)* base * height;
}

// function to calculate circumference of circle
// the function requires radius
float CircleCircumference(float radius){
return (2 * 3.14 *radius);
}

// function to calculate perimeter of rectangle
// the function requires length and width
float RectanglePerimeter(float length, float width){
return (2*length) + (2*width);
}

// function to calculate perimeter of triangle
// the function requires 3 sides.
float TrianglePerimeter(float side1, float side2, float side3){

return side1 + side2 + side3;
}


// function to display the menu
void printMenu()
{
cout<<"\n\nThis is a geometry calculator."<<endl;
cout<<"Choose what you would like to calculate."<<endl<<endl;
cout<<"1) Find the area of a circle"<<endl;
cout<<"2) Find the area of a rectangle"<<endl;
cout<<"3) Find the area of a triangle"<<endl;
cout<<"4) Find the circumference of a circle"<<endl;
cout<<"5) Find the perimeter of a rectangle"<<endl;
cout<<"6) Find the perimeter of a triangle"<<endl;
cout<<"\n\nEnter the number of your choice:";

}
// formulas.cpp ends here

// your formulas.h

#ifndef FORMULAS_H
#define FORMULAS_H

void printMenu();
float CircleArea(float );
float RectangleArea(float , float);
float TriangleArea(float, float );
float CircleCircumference(float);
float RectanglePerimeter(float, float );
float TrianglePerimeter(float , float , float );

#endif
// formulas.h ends here

here is the sample output:-

Thank You.


Related Solutions

Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file...
Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file Geometry.java. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create it. 2. Below the main method, but in the Geometry class,create a static method called printMenu that has no parameter list...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Take all the methods from this program and put it into single program and compile, and...
Take all the methods from this program and put it into single program and compile, and then test the program with some expressions. Should have two files: Lex.java and a output.txt file package lexicalanalyser; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; enum CharacterClass{ LETTER(0), DIGIT(1), UNKNOWN(99), EOF(-1); private int value; private CharacterClass(int value){ this.value = value; } public int getValue(){ return value; } } enum TokenCodes{ INT_LIT(10), IDENT(11), ASSIGN_OP(20), ADD_OP(21), SUB_OP(22),...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Create a memory diagram for the following C program when the program reaches point 1. void...
Create a memory diagram for the following C program when the program reaches point 1. void foo (int *a); int bar(int *x); int search(int *c); int main(void) { int q, p=10; q= search(&p); return 0; } void foo(int *a) { *a += 2; //point 1 *f *= 10; } int bar(int *x) { return 2* *x; } int search(int *c) { int z= *c + bar(c); foo(c); return z; }
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main() { int i, count, number[25]; printf("Enter some elements (Maximum 25): "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("The Sorted Order is: "); for(i=0;i<count;i++) printf(" %d",number[i]); return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT