Selection Sort

This is a discussion on Selection Sort within the C++ Programming forums, part of the General Programming Boards category; Can someone help me to make my program sort according to the average gallons used per car I keep geting ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Selection Sort

    Can someone help me to make my program sort according to the average gallons used per car I keep geting 30 - 40 error when i try to compile it. Also
    my files do exist.


    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    const int MAXCARS = 12;
            float avgallons = 0;
    		int numel = 0;
            float totalmiles, totalgallons;
    
    struct Car
    {
    int number, miles;
    float gallons;
    float averagegallons;
    }Cars;
    
    Car Cars[MAXCARS];
    
    
    void getdata(int, int, float);
    void processdata(int, float);
    void sort(float);
    void putdata(int, float);
    
    ifstream HopeData;
    ofstream MikeData;
    
    void main()
    {
    
    HopeData.open("gdata.dat");
    MikeData.open("pdata");
    
    if(HopeData.fail())
            {
            cout << "\n\nFile not successfully opened\n\n";
           }
    cout << "\n\nFile successfully opened\n\n";
    
    MikeData << "\n\n              Car Report" << endl;
    MikeData << "Number       Average Per Car" << endl;
    cout << setiosflags(ios::showpoint);
                    cout << setiosflags(ios::fixed);
                    cout << setprecision(2);
    
    getdata(Cars.number, Cars.miles, Cars.gallons);
    putdata(Cars.number, Cars.averagegallons);	
    
    MikeData << "\n\nAverage Gallons Used by All Cars is " << avgallons << endl;
    }
    
    void getdata(int number, int miles, float gallons)
    {
            while(HopeData.peek() != EOF)
            {
    HopeData >> Cars.number >> Cars.miles >> Cars.gallons;
    HopeData.ignore(80,'\n');
    
    processdata(Cars.miles, Cars.gallons);
            }
    }
    
    void processdata(int miles, float gallons)
    {
    Cars.averagegallons = Cars.miles / Cars.gallons;
    totalmiles = totalmiles + Cars.miles;
    totalgallons = totalgallons + Cars.gallons;
    avgallons = totalmiles / totalgallons;
    numels++;
    sort(Cars.averagegallons);
    }
    
    void sort(float averagegallons, int numels)
    {
    int i,j,min,minidx,temp,moves=0;
    
    for(i=0;i<numels;i++)
      {
    min = Cars[i];
    minidx = i;
     	for(j=i+1;j<numels;j++)
      	{
    		if(Cars[j].averagegallons<Cars[i].averagegallons)
    		{
    		min = Cars[j];
    		minidx=j;
    		}
    		if(min<Cars[i].averagegallons)
    		{
           temp=Cars[i];
           Cars[i]=min;
           Cars[minidx]=temp;
    	   	}
    	}
    }
    
    }
    
    void putdata(int number, float averagegallons)
    {
            while(HopeData.peek() != EOF)
            {
    MikeData << Cars[].number << "\t     " << Cars[].averagegallons << endl;
            }
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    main()'s return type is int, not void.

    Don't declare objects globally; do it locally and then pass them to functions as necessary.

    Don't use EOF to control your input loops.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Instead, place the input function in the condition of the while loop:
    Code:
    while(myifstream >> x )
    {
      // do stuff with x
    }
    You'd make life a lot easier on yourself by using a std::vector rather than an array to hold your data.

    Post your errors.
    Last edited by joshdick; 04-28-2005 at 07:35 AM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    In addition to what joshdick said.

    Code:
    struct Car
    {
    int number, miles;
    float gallons;
    float averagegallons;
    }Cars;
    
    Car Cars[MAXCARS];
    What is Cars? Is it an instance of a struct or is it an array of struct Car objects?

    Code:
    void processdata( 
        ...
        numels++;
        ...
    }
    There is no numels variable in scope anywhere at the time you get here. There is a global numel variable however.

    Your method of passing values into pretty much all your functions is horribly wrong. You should be passing Car objects or your Car array and not ints and floats. You should also be passing them as references/pointers instead of by value. I'm at a loss as to where to begin my explaining.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 04:42 PM
  2. Selection Sort problem #2
    By Twigstar in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 07:27 PM
  3. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21