sorting values
hello world
im having trouble sorting some values
i got the inches to be sorted and now i need to sort the dates to go with that value
if the input is:
december 12 == 14
december 13 == 7
i need it to switch like this
december 13 == 7
december 12 == 14
instead of
december 12 == 7
december 13 == 14
|
heres what i have so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
struct SnowInfo
{
int date; //month
double inches; //snow depth
};
int main()
{
string months[13] =
{" ", "Janurary", "Feburary", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December" };
const int week = 7;
int index;
SnowInfo days[week];
SnowInfo snow[week];
int month;
int day;
int first;
int last;
int count;
int minIndex;
int minValue;
//user input
cout << "This program will allow you to input the amount of snow fall for any given month on a seven day period" << endl;
cout << "Enter the number of the month you wish to start with: " << flush;
cin >> month;
cout << "Now enter the first day you wish to record: ";
cin >> days[0].date;
for (index = 1; index < week; index++)
{
days[index].date = days[0].date + index;
}
cout << "Enter the amount of snow ";
for (int day = 0; day < week; day++)
{
cout << "for " << months[month] << " " << days[day].date << " : " << flush;
cin >> snow[day].inches;
}
cout << endl;
//Display report
cout << "Snow report for " << months[month] << " " << days[0].date << "-" << days[6].date << endl;
cout << fixed << showpoint << setprecision(1);
cout << right << " " << setw(5) << "Date" << setw(6) << "Base" << endl;
//Sort Inches of Snow
for (count = 0; count < (week - 1); count++)
{
minIndex = count;
minValue = snow[count].inches;
for(int index = count + 1; index < week; index++)
{
if (snow[index].inches < minValue)
{
minValue = snow[index].inches;
minIndex = index;
}
}
snow[minIndex].inches = snow[count].inches;
snow[count].inches = minValue;
}//end sort
//display numbers
for (index = 0; index < week; index++)
{
cout << right << " " << setw(5) << days[index].date << setw(7) << snow[index].inches << endl;
}
system("PAUSE");
return 0;
}
|
Try this one, find changes yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
#define week 7
struct SnowInfo
{
int date; //month
double inches; //snow depth
};
int main()
{
string months[13] =
{" ", "Janurary", "Feburary", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December" };
int index;
SnowInfo days[week];
SnowInfo temp;
int month;
int day;
int first;
int last;
int count;
int minIndex;
int minValue;
//user input
cout << "This program will allow you to input the amount of snow fall for any given month on a seven day period" << endl;
cout << "Enter the number of the month you wish to start with: " << flush;
cin >> month;
cout << "Now enter the first day you wish to record: ";
cin >> days[0].date;
for (index = 1; index < week; index++)
{
days[index].date = days[0].date + index;
}
cout << "Enter the amount of snow ";
for (int day = 0; day < week; day++)
{
cout << "for " << months[month] << " " << days[day].date << " : " << flush;
cin >> days[day].inches;
}
cout << endl;
//Display report
cout << "Snow report for " << months[month] << " " << days[0].date << "-" << days[6].date << endl;
cout << fixed << showpoint << setprecision(1);
cout << right << " " << setw(5) << "Date" << setw(6) << "Base" << endl;
//Sort Inches of Snow
for (count = 0; count < (week - 1); count++)
{
minIndex = count;
minValue = days[count].inches;
for(int index = count + 1; index < week; index++)
{
if (days[index].inches < minValue)
{
temp = days[index];
minIndex = index;
days[minIndex] = days[count];
days[count] = temp;
break;
}
}
}//end sort
//display numbers
for (index = 0; index < week; index++)
{
cout << right << " " << setw(5) << days[index].date << setw(7) << days[index].inches << endl;
}
cin.get();
cin.get();
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.