| | |
Selectoin SOrt
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
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.
my files do exist.
C++ Syntax (Toggle Plain Text)
#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; } }
Only define Cars once. Then figure out which way to use them.
struct Car
{
int number, miles;
float gallons;
float averagegallons;
}Cars;
Car Cars[MAXCARS];getdata(Cars.number, Cars.miles, Cars.gallons); putdata(Cars.number, Cars.averagegallons);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Selection Sort in java (Java)
- radix sort (C++)
- Some help needed with Selection Sort in java, Please!! (Java)
- sort on field names; refresh page? (PHP)
- How to sort the rows in the datagrid and update it successfully? (VB.NET)
- Need Outlook 2003 addon: sort emails in special folders? (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Finding the mode value and its frequency!
- Next Thread: How to read end of line?
Views: 1519 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






