Answer to Question #325161 in C++ for Amy

Question #325161

Create a program that will overload a function four (4) times. The function takes a single parameter but each parameter is different in each function.


1
Expert's answer
2022-04-07T07:30:20-0400
#include <iostream>
#include <cmath>


// Absolute value of integer
int modul(int i) {
    if (i >= 0) 
        return i;
    else
        return -1;
}


// Absolute value of real number
double modul(double x) {
    if (x >= 0)
        return x;
    else
        return -x;
}


struct complex_t {
    double real;
    double imag;
};
// Absolute value of complex number
double modul(complex_t z) {
    return std::sqrt(z.real*z.real + z.imag*z.imag);
}


// Module of 3-d vector
double modul(double v[]) {
    return std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}


int main() {
    int i = 5;
    double x = -12;
    complex_t z = {1, 1};
    double v[3] = { 1, 2, 3};

    std::cout << "modul(i) = " << modul(i) << std::endl;
    std::cout << "modul(x) = " << modul(x) << std::endl;
    std::cout << "modul(z) = " << modul(z) << std::endl;
    std::cout << "modul(v) = " << modul(v) << std::endl;

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS