开发者

trying to compile my C++ program.

开发者 https://www.devze.com 2023-01-24 14:25 出处:网络
This program \"FindMIn\" takes two values as its parameters and returns the smallest value.  I\'m trying to change my program so that it takes in three values as parameters and returns the smallest o

This program "FindMIn" takes two values as its parameters and returns the smallest value.  I'm trying to change my program so that it takes in three values as parameters and returns the smallest of three values. This is what I have so far But how??

#include <iostream>        
using namespace std; 
  

//function prototypes go before main - parameters much match those of the function definition

int FindMin(int x, int y);

//place prototype for PrintOutput here

int main()
{
    int n1, n2, n3;
    int result;
    char answ = 'Y';

    while (answ == 'Y')
    {
        cout << "Please enter three numbers..." << endl;
        cin >> n1 >> n2 >> n3;
        result = FindMin(n1, n2); //function call
        
        //place call to function PrintOutput here

        cout << "Would you like to run the program again?  Enter Y or N" << endl;
        cin >> answ;
     }
    

    return(0);
}

//***************************************************************
//FindMin - returns the minimum of two values
//***************************************************************
int FindMin(int a, int b)
{
    if (a < b)
        return a;
    else
        return b;
}
//******************************************************************
//PrintOutput - prints the values input and the smallest of the 
//three values开发者_运维技巧
//******************************************************************
void PrintOutput(int first, int second, int min)
{
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "The minimum value of " << first << ", " <<
        second << ", is "  << min << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
}


Work it out on paper first.

You have three numbers, lets say: 1 2 3.

You have a function findMin(int a, int b) which returns the smallest of the two given. You can't use the function directly as it only has two arguments - but you can compose it in another way give you the minimum of three numbers. This is basic math function use.

Here is a hint:

min(1,2) = 1
min(1,3) = 1
min(2,3) = 2
min(1,min(2,3)) = 1


You will need to expand the FindMin parameters to take 3 ints:

int FindMin(int x, int y, int z);

// ...

int main()
{
  // ...
        cin >> n1 >> n2 >> n3;
        result = FindMin(n1, n2, n3); //function call
  // ...
}

// ...

int FindMin(int x, int y, int z)
{
  // Place the code that compares the three numbers and returns minimum here
}


cin >> n1 >> n2 >> n3;

That might be an issue, since n1/n2/n3 are ints. You'll want to take in an ASCII string and convert it to an int. For something this simple, you could just use atoi, which would look something like:

char buffer[1024];
cin >> buffer;
n1 = atoi(buffer);

The trick is converting the text representation of the number into an actual number, which is required for your function. Just using the characters will almost certainly yield some odd results.

0

精彩评论

暂无评论...
验证码 换一张
取 消