Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Exclude any source code existing files that may already be in your IDE project and add
three new ones,naming them C1A5E3_ComputeMinimum.cpp, C1A5E3_ComputeMaximum.cpp, and
C1A5E3_main.cpp. Do not use #include to include any of these three files in each other
or in any other file. However, you may use it to include any appropriate header
file(s) you need.

File C1A5E3_ComputeMinimum.cpp must contain a function named ComputeMinimum and
C1A5E3_ComputeMaximum.cpp must contain a function named ComputeMaximum. Each function
must:
1. have exactly two formal parameters, each of type “pointer to const double”;
2. return type “pointer to double” (not “pointer to const double”);
3. contain only one statement;
4. not use variables other than its formal parameters;
5. not use anything that requires #define or #include;
6. not use literal values;
7. not do assignment, addition, subtraction, multiplication, or division;
8. not use if, switch, or looping statements;
9. not call functions or macros;
10. not display anything.
ComputeMinimum must compare the values pointed to by its parameters and return a
pointer to the smallest of those values whereas ComputeMaximum must compare the values
pointed to by its parameters and return a pointer to the greatest of those values.

File C1A5E3_main.cpp must contain function main, which must:
1. use no more than two variables;
2. prompt the user to enter two space-separated decimal values on the same line;
3. pass pointers to the user-entered values to both ComputeMinimum and ComputeMaximum
as arguments;
4. display the results of both function calls using the following 2-line format, where
the question marks represent the values whose pointers are passed to and returned from
the functions and the ampersands are displayed literally to remind the user that
pointers are being passed and returned, not the values themselves:
ComputeMinimum(&?, &?) returned &?
ComputeMaximum(&?, &?) returned &?

What I have tried:

Main.C
#include <iostream>
#include "C:\Users\Shreeji\CLionProjects\C1A5E3\C1A5E3_Compute.h"
using namespace std;

int main()
{
    cout << "Please enter any two number: ";
    double number1, number2, *ptr1, *ptr2;
    cin >> number1 >> number2;
    ptr1 = &number1;
    ptr2 = &number2;
    cout << "ComputeMinimum (" << number1 << ", " << number2 << ") returned " << *ComputeMinimum(ptr1, ptr2) << "\n";
    cout << "ComputeMaximum (" << number1 << ", " << number2 << ") returned " << *ComputeMaximum(ptr1, ptr2) << "\n";
    cout << ptr1 << ptr2;
    return 0;
}



Computeminimum.c
double *ComputeMinimum(const double *number1, const double *number2)
{
    return (double *) (number1 < number2 ? number1 : number2);
}


ComputeMaximum.c
double *ComputeMaximum(const double *number1, const double *number2)
{
    return (double *) (number1 > number2 ? number1 : number2);
}


header file
#ifndef C1A5E2_Compute_H
#define C1A5E2_Compute_H

double &ComputeMinimum(const double &number1, const double &number2);
double &ComputeMaximum(const double &number1, const double &number2);

#endif // C1A5E2_Compute_H
Posted
Updated 4-May-20 15:03pm

1 solution

You must dereference the pointers to compare the values, otherwise you will compare addresses instead:
C++
double* ComputeMinimum(const double* number1, const double* number2)
{
    return (double*) (*number1 < *number2 ? number1 : number2);
}

double* ComputeMaximum(const double* number1, const double* number2)
{
    return (double*) (*number1 > *number2 ? number1 : number2);
}


Moreover, the declarations in the header file do not match their definitions (you used references instead of pointers).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900