Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,151 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,121 people online right now. Registration is fast and FREE... Join Now!




Finding Minimum Value

 
Reply to this topicStart new topic

Finding Minimum Value

anthony9820
17 Apr, 2007 - 09:46 AM
Post #1

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
i am trying to write a function that will identify the lowest of 4 numbers. I am clueless please help!!!!!! crazy.gif crazy.gif crazy.gif !

this is what i have so far:


CODE

#include <iostream>
#include <conio>
#include <iomanip>
#include <cstdlib>

using namespace std;

int getScores ( int &, int &, int & );
int findLowest ( int, int ,int, int);
void calcAvg (float &, int, int, int );
void displayAvg ( float );

void main ()
{
        int test1, test2, test3, test4 = 0;
        float avg = 0.0;

        cout << "This program will caculate the average of tree test scores";
        cout << endl << endl;
        test4 = getScores (test1, test2, test3);



        system ("pause");

}
int getScores ( int & test1, int & test2, int & test3 )
{
        int test4;

        cout << "Please enter the grade for test 1: ";
        cin >> test1;
        cout << "\nPlease enter the grade for test 2: ";
        cin >> test2;
        cout << "\nPlease enter the grade for test 3: ";
        cin >> test3;
        cout << "\nPlease enter the grade for test 4: ";
        cin >> test4;
        return test4;
}





it's the findLowest function I'm having trouble with.

thanks.

This post has been edited by jayman9: 17 Apr, 2007 - 11:15 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Finding Minimum Value
17 Apr, 2007 - 09:58 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well the basic way to sell if Number A is lower than Number B is something like

inline int min(int A, int B) {return (A < B) ? A : B;}

then to tell the minimum of 4 numbers you would use:

MinVal=min(A, min( B, min( C, D)))

You could add this to a loop to find the minimum value of an array (only tesing two values at once, the current "minumum" and the current array element.
User is offlineProfile CardPM
+Quote Post

anthony9820
RE: Finding Minimum Value
17 Apr, 2007 - 10:08 AM
Post #3

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
i sorry. I didn't understand. I'm really new at this. Is there an easier way to explain your answer?

thanks

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Finding Minimum Value
17 Apr, 2007 - 10:16 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You have to compare each value to at least one other to determine the lowest...some would advise comparing each value to each other value, but nick has suggested a more optimized manner in which to do it. you can write a function to compare two integers...the function will return the lower of the two. As you may not be familiar with the ternary operator, let's look at an if else structure:
CODE

int lowest(int num1, int num2)
{
   if(num1<num2)
      return num1;
   else
      return num2;
}

You can then run that function on successive pairs, with one of each pair being the returned value from the previous time.
User is offlineProfile CardPM
+Quote Post

anthony9820
RE: Finding Minimum Value
17 Apr, 2007 - 11:29 AM
Post #5

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
Thanks for all the help guys. Here is the final code for the prog. Can u take a look and see if i can tweak it. But please remember that i am in a concepts 1 class and it is very basic.

CODE
#include <iostream>
#include <conio>
#include <iomanip>
#include <cstdlib>

using namespace std;

int getScores ( int &, int &, int & );
int findLowest ( int, int ,int, int);
void calcAvg (float &, int, int, int );
void displayAvg ( float );

void main ()
{
        int test1 =0, test2 = 0, test3 = 0, test4 = 0, low, num1, num2, num3;
        float avg = 0.0;

        cout << "This program will caculate the average of the test grades ";
        cout << "for the semester.\nThe lowest score will be dropped and ";
        cout << "will not count towards the final average.";
        cout << endl << endl;

        test4 = getScores (test1, test2, test3);

        low = findLowest (test1, test2, test3, test4);

        cout << "\n\nThe lowest grade entered was " << low << ".\n";
        cout << "It will be dropped and will not factor into the average\n\n";
        cout << "Please re-enter the three highest test scores:\n";
        cout << "1. ";
        cin  >> num1;
        num1 = test1;
        cout << "\n2. ";
        cin >> num2;
        num2 = test2;
        cout << "\n3. ";
        cin >> num3;
        num3 = test3;
        cout << endl << endl;

        calcAvg (avg, test1, test2, test3);

        cout << "\n";

        displayAvg ( avg );




        system ("pause");

}

// ********************************************************
// This function gets the 4 inital grades, returns one
// value, and changes the values of three variables in the
// function main ().
// *********************************************************

int getScores ( int & test1, int & test2, int & test3 )
{
        int test4;

        cout << "Please enter the grade for test 1: ";
        cin >> test1;
        cout << "\nPlease enter the grade for test 2: ";
        cin >> test2;
        cout << "\nPlease enter the grade for test 3: ";
        cin >> test3;
        cout << "\nPlease enter the grade for test 4: ";
        cin >> test4;
        return test4;
}

// ********************************************************
// This function finds the lowest grade and returns is back
// to main ()
// ********************************************************

int findLowest ( int test1, int test2, int test3, int test4)
{
        int low;
        if (test1 < test2 && test1 < test3 && test1 < test4)

                low = test1;

        if (test2 < test1 && test2 < test3 && test2 < test4)

                low = test2;

        if (test3 < test1 && test3 < test2 && test3 < test4)

                low = test3;

        if (test4 < test1 && test4 < test2 && test4 < test3)

                low = test4;

        return low;
}

// ************************************************************
// This function caculates the average of the three remaining
// grade and changes the value of the variable 'avg' in the
// function main ().
// ************************************************************

void calcAvg (float & avg, int test1, int test2, int test3)
{

        int sum;
        sum = test1 + test2 +test3;

        avg = sum / 3;
}

// *******************************************************************
// This function displays the final average for the grade earned
// for the semester.
// *******************************************************************

void displayAvg (float avg)
{
        cout << "The average of the three grades entered is " << avg << ".\n\n";
        cout << "Please re-run the program to caculate the average of a ";
        cout << "new set of grades.\n\n";
        cout << "Thank You\n\n\n";
}





Thanks ahead of time. I really would have been sunk without u guys!!!!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month