Ok almost there.. I'm still having the same sorting problems... the program compiles and runs fine except when its sorts. I do get the lowest number but I'm not getting the borough that correspond to it.
here's the new full code
here's the sort functionCode:#include<iostream> #include<iomanip> #include<string> #include<cstring> using namespace std; struct BorData { string borough; int accidents; }; int getNumAccidents(BorData[]); int findLowest(BorData[]); const int SIZE = 5; /*********main*******/ int main() { BorData combine[5] = {{"New York City", 0}, {"Bronx", 0}, {"Queens", 0}, {"Brooklyn", 0}, {"Manhattan", 0}}; int accidents[SIZE]; getNumAccidents(combine); findLowest(combine); return 0; } int getNumAccidents(BorData combine[]) { for(int i=0;i<5;i++) { cout<<"\nEnter Number of Accidents for "<<combine[i].borough<<endl; cin>>combine[i].accidents; if(combine[i].accidents<0) { cout<<"Number has to be greater than 0"; return 0; } } } int findLowest(BorData combine[]) { int temp[5]; for(int i=0;i<5;i++){ if(combine[i].accidents<combine[0].accidents){ temp[i]=combine[i].accidents; combine[i].accidents=combine[0].accidents; combine[0].accidents=temp[i]; } } cout<<"The lowest number of accident is: "<<combine[0].accidents<<endl; cout<<"In the Borough of: "<<combine[0].borough<<endl; return 0; }
Code:int findLowest(BorData combine[]) { int temp[5]; for(int i=0;i<5;i++){ if(combine[i].accidents<combine[0].accidents){ temp[i]=combine[i].accidents; combine[i].accidents=combine[0].accidents; combine[0].accidents=temp[i]; } } cout<<"The lowest number of accident is: "<<combine[0].accidents<<endl; cout<<"In the Borough of: "<<combine[0].borough<<endl; return 0; }




Reply With Quote
