Home > Enterprise >  How to Call function with another function?
How to Call function with another function?

Time:11-12

For a homework assignment we are to calculate the perimeter of a triangle with one function: perimeter()

Then we are supposed to calculate the area of a triangle with the second function: area()

Then we are supposed to create a third function called: perimeterArea() that calls the functions perimeter() and area().

We are then supposed to cout the perimeterArea() function so it looks like this:

Its perimeter is 12
Its area is 6

I am looking for help on how to create the perimeterArea() function, and how to output it correctly.

Currently the program won't run.

#include <iostream>
#include <cmath>

using namespace std;

// Function prototypes

double perimeter (double, double, double, double&); //memory allocation//
double area (double, double, double, double&, double&, double&); // memory allocatation //
void perimeterArea (double&, double&); // memory allocation //


// Beginning of Main Function


int main () {

    double side1, side2, side3, trianglePerimeter, s, triangleArea; // memory allocation //
    char yes;

    do {  // Starts loop
      
        cout << "Enter the lengths of the three sides of a triangle -- ";
        cin >> side1 >> side2 >> side3;

        perimeterArea (trianglePerimeter, triangleArea) ; // Doesn't work!

        //or///

        perimeterArea (perimeter, area); // second idea
   
        cout << "Its perimeter is " << trianglePerimeter << endl;
        cout << "Its area is " << triangleArea << endl;

        // End program loop requirement

        cout << endl;

        cout << "Run again (Y/N)? ";
        cin >> yes;
    } while (tolower(yes) == 'y');

    cout << "Good bye!";
}

// calculate perimeter function

double perimeter (double side1, double side2, double side3, double& trianglePerimeter) 
{
    trianglePerimeter = (side1   side2   side3);
    return trianglePerimeter;
}

// calculate area function

double area (double side1, double side2, double side3, double& trianglePerimeter, double& s, double& triangleArea) 
{ 
    s = trianglePerimeter / 2;
    triangleArea = sqrt (s*(s-side1)*(s-side2)*(s-side3));
    return triangleArea;
}

// calculate perimeterArea function

void perimeterArea (double& trianglePerimeter, double& triangleArea)
{
    Call previous perimeter function?; 
    call previous area function?;

    Output through some type of statement in int main ()
}

I need help with the syntax for calling two functions with a new function and inputting. Not sure what to try, however.

I previously tried:

void perimeterArea (double& trianglePerimeter, double& triangleArea)

But I do not know where to go from there.

CodePudding user response:

here is what perimiterArea should look like

summary of function

  • it takes 3 arguments (sides 1,2,3)
  • it calls perimeter passing the three sides - that returns the perimeter
  • it calls the area function passing the three sides - that returns the area
  • it the outputs a sentence "area = ... perimeter = ..."
  • it doesnt return anything.

So its

void perimeterArea(double,s1, double s2, double s3)  {
  double perimeter = perimeter (s1,s2,s3);
  double area = area(s1,s2,s3);
  cout << "area = " << area << " perim = " << perimeter ;
}

you need to correct the signatures of area and perimiter too

CodePudding user response:

You are not passing the user's input values into perimeterArea() so it can then pass them to the other functions. You need to add parameters to perimeterArea() for that purpose.

You are also making perimeter() and area() take in too many parameters. There is no reason for perimeter() to require the caller to pass in a variable reference for trianglePerimeter, or for area() to require the caller to pass in variable references for s and triangleArea. All of those should be local variables rather than parameters.

Try something more like this instead:

#include <iostream>
#include <cmath>

using namespace std;

// Function prototypes

double perimeter (double, double, double);
double area (double, double, double, double);
void perimeterArea (double, double, double, double&, double&);

// Beginning of Main Function

int main () {

    double side1, side2, side3, trianglePerimeter, triangleArea;
    char yes;

    do {  // Starts loop
      
        cout << "Enter the lengths of the three sides of a triangle -- ";
        cin >> side1 >> side2 >> side3;

        perimeterArea (side1, side2, side3, trianglePerimeter, triangleArea);
   
        cout << "Its perimeter is " << trianglePerimeter << endl;
        cout << "Its area is " << triangleArea << endl;

        // End program loop requirement

        cout << endl;

        cout << "Run again (Y/N)? ";
        cin >> yes;
    } while (tolower(yes) == 'y');

    cout << "Good bye!";
}

// calculate perimeter function

double perimeter (double side1, double side2, double side3) 
{
    return (side1   side2   side3);
}

// calculate area function

double area (double side1, double side2, double side3, double trianglePerimeter)
{ 
    double s = trianglePerimeter / 2;
    return sqrt (s*(s-side1)*(s-side2)*(s-side3));
}

// calculate perimeterArea function

void perimeterArea (double side1, double side2, double side3, double& trianglePerimeter, double& triangleArea)
{
    trianglePerimeter = perimeter(side1, side2, side3);
    triangleArea = area(side1, side2, side3, trianglePerimeter);
}

Alternatively:

#include <iostream>
#include <cmath>

using namespace std;

// Function prototypes

double perimeter (double, double, double);
double area (double, double, double);
void perimeterArea (double, double, double, double&, double&);

// Beginning of Main Function

int main () {

    double side1, side2, side3, trianglePerimeter, triangleArea;
    char yes;

    do {  // Starts loop
      
        cout << "Enter the lengths of the three sides of a triangle -- ";
        cin >> side1 >> side2 >> side3;

        perimeterArea (side1, side2, side3, trianglePerimeter, triangleArea);
   
        cout << "Its perimeter is " << trianglePerimeter << endl;
        cout << "Its area is " << triangleArea << endl;

        // End program loop requirement

        cout << endl;

        cout << "Run again (Y/N)? ";
        cin >> yes;
    } while (tolower(yes) == 'y');

    cout << "Good bye!";
}

// calculate perimeter function

double perimeter (double side1, double side2, double side3) 
{
    return (side1   side2   side3);
}

// calculate area function

double area (double side1, double side2, double side3)
{ 
    double trianglePerimeter = perimeter (side1, side2, side3);
    double s = trianglePerimeter / 2;
    return sqrt (s*(s-side1)*(s-side2)*(s-side3));
}

// calculate perimeterArea function

void perimeterArea (double side1, double side2, double side3, double& trianglePerimeter, double& triangleArea)
{
    trianglePerimeter = perimeter(side1, side2, side3);
    triangleArea = area(side1, side2, side3);
}

CodePudding user response:

Got it thanks for everyone's help :)

  •  Tags:  
  • c
  • Related