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

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




finding max, min, and second biggest number

 
Reply to this topicStart new topic

finding max, min, and second biggest number

M_tin
post 10 Apr, 2008 - 04:00 PM
Post #1


New D.I.C Head

*
Joined: 20 Mar, 2008
Posts: 5

I'm doing a program that uses an input file with 587 lines. Each line has four numbers. For each line we're supposed to find the largest, second largest, and smallest numbers and we're supposed to use a seperate function for this. I tried using if statements but ended up confusing myself. Is there any better way to this.

CODE

#include <iostream>
#include <fstream>
int Find(float,float,float,float&,float&,float&);

using namespace std;

int main();
{
    int x1;
        int x2;
        int x3;
    int x4;
    
    ifstream Find;
        ofstream Output;


    Find.open("f:/prj5in.txt");
        Output.open("f:/prj5out.txt")

    Find<<x1<<x2<<x3<<x4<<endl;

    cout<<Find(x1,x2,x3,x4,greatest,second_greatest,smallest);
    
    cin.get();cin.get();
    return 0;
}

int Find(float x1,float x2,float x3,float x4 float& greatest, float& second_greatest,float& smallest)
{
    int count=1;
    float t, u;

    while(Find)
    {
        if (x1>x2)
        {
        t = x1;
        u = x2;
        }
        else
        {
        t = x2;
        u = x1;
        }
        if (x3>t && x3>u)
        {
        greatest = x3;
        if (x3>t || x3>u)
        {
            second_greatest=x3
                 }
                else
        greatest = t;
        if (x1<x2)
        u = x1;
        else
        u = x2;
        if (x3<u)
        smallest = x3;
        else
        smallest = t;






User is offlineProfile CardPM

Go to the top of the page


jeronimo0d0a
post 10 Apr, 2008 - 04:34 PM
Post #2


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


I tried to comment your code and leave it for you to see. I didn't have the file so I used random numbers. I thought it was easier to sort the lot and print them by their indices. If you work with them as an array it's a little cleaner.
Hope this helps.
Jeronimo

CODE

#include <iostream>
#include <fstream>
#include <stdlib.h>

int Sort(float y[]);

using namespace std;
const int four = 4; /// for easy search/replace
int main()
{
    float x[four] = {1,8,3,0};
    int junk = 0;
    ifstream Find;
        ofstream Output;


    //Find.open("f:/prj5in.txt");
    //    Output.open("f:/prj5out.txt")
    // note this should be in a while not eof loop
    while( junk < 10 )
    { junk++; // so this will end
        for(int i=0; i<four; i++)
        {
            //Find >> x[i];
            x[i] = (float)(rand()%100);
        }
        Sort(x);
        cout << "\nMinimum: " << x[0] << " Maximum: " << x[3] << " Second Largest: " << x[2];
    }
    //Find<<x1<<x2<<x3<<x4<<endl;   >> not <<
    //Find.close() // always
    //cout<<Find(x1,x2,x3,x4,greatest,second_greatest,smallest);
    cout << "\n\n\t";
    system("pause"); // cin.get();cin.get();
    return 0;
}

int Sort( float y[] )
{
    bool sorted = false;
    float t;

    while( ! sorted )
    {
        sorted = true;
        for( int i=0; i<four-1; i++ )
        {
            if(y[i] > y[i+1])
            {
                t = y[i]; // swap the numbers
                y[i] = y[i+1];
                y[i+1] = t;
                sorted = false; // if we had to sort, we're not done
            }
        }
    }
    return(0);
}


User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 06:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month