我目前正在嘗試對數組進行排序(我確實需要處理這個問題,我總是在排序數據時遇到問題)。排序並行數組中的兩組數據
所以基本上我創建了一個包含「date」和「snowDepth」元素的C++結構體。我使用bubblesort邏輯對snowDepth元素進行排序,這非常棒,這正是我需要的。但是,由於元素是分開的,所以日期不會改變。我如何排序並行數組?
數據應該是這樣的:
Snow Report December 12 - 18
Date Base
13 42.3
12 42.5
14 42.8
15 43.1
18 43.1
16 43.4
17 43.8
和我看起來是這樣的:
SNOW REPORT December 12 - 18
=======================================
DATE DEPTH
12 42.3
13 42.5
14 42.8
15 43.1
16 43.1
17 43.4
18 43.8
注意如何日期不與snowDepth元素一起排序。我怎樣才能做到這一點?
好的,謝謝所有的評論傢伙!這是我的代碼:
// Structure
struct Array
{
int date;
double snowDepth;
void SnowData(int d, double sD)
{
date = d;
snowDepth = sD;
}
};
// Main Function
int main()
{
/* *--------------------*
| VARIABLES |
*--------------------* */
// Constant Variables
const int NUM_ELEMENTS = 7;
const int NUM_MONTHS = 12;
// String, Numerical, and Boolean Variables
string month;
int startDate;
int endDate;
int a;
int b;
int flag = 0;
double result;
bool monthMatch = false;
// Array Variables
Array snowData[NUM_ELEMENTS];
string name[NUM_MONTHS] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
/* *--------------------*
| USER INSTRUCTIONS |
*--------------------* */
cout << endl;
cout << "This program will keep track of the local snow conditions for 7 days." << endl;
cout << "This program will ask the user for the month, date range (must be 7 days)," << endl;
cout << "and the inches of snow reported on that date. The program will then sort" << endl;
cout << "the data from least amount of snow reported to the most." << endl;
cout << endl;
/* *--------------------*
| USER PROMPT |
*--------------------* */
// Prompt the user for the month
cout << "Enter the full name of the month: ";
cin >> month;
// Prompt the user for the starting date
cout << "Enter the starting date of the 7-day period: ";
cin >> startDate;
snowData[0].date = startDate;
// Once the user enters the start date, run a loop to initialize the rest of the dates in snowData array
for (int x = 1; x < NUM_ELEMENTS; x++)
snowData[x].date = ++startDate;
// Prompt the user for the ending date
cout << "Enter the ending date of the 7-day period: ";
cin >> endDate;
cout << endl;
// If the user does not enter the correct ending date (which is 7 days from the start date), loop will prompt the user again
if (endDate != snowData[NUM_ELEMENTS - 1].date)
{
do
{
cout << "The end date entered is not 7 days from the start date. Try again." << endl;
cout << "Enter the ending date of the 7-day period: ";
cin >> endDate;
cout << endl;
} while (endDate != snowData[NUM_ELEMENTS - 1].date);
int x = 0;
// Once the user enters the correct ending date, prompt the user for the snow depth numbers
for (int y = 0; y < NUM_ELEMENTS; y++)
{
cout << "Enter the depth of snow measured on " << month << " " << snowData[x].date << ": ";
cin >> snowData[y].snowDepth;
x++;
}
}
// Once the user enters the correct ending date, prompt the user for the snow depth numbers
else
{
int x = 0;
for (int y = 0; y < NUM_ELEMENTS; y++)
{
cout << "Enter the depth of snow measured on " << month << " " << snowData[x].date << ": ";
cin >> snowData[y].snowDepth;
x++;
}
}
/* *--------------------*
| SORTING & FORMAT |
*--------------------* */
// Sorting logic in ascending order
for (a = 1; (a <= NUM_ELEMENTS) && flag; a++)
{
flag = 0;
for (b = 0; b < (NUM_ELEMENTS - 1); b++)
{
if (snowData[b + 1].snowDepth < snowData[b].snowDepth)
{
result = snowData[b].snowDepth;
snowData[b].snowDepth = snowData[b + 1].snowDepth;
snowData[b + 1].snowDepth = result;
flag = 1;
}
}
}
// Formatted Output
cout << endl;
cout << " SNOW REPORT ";
cout << month << " " << snowData[0].date << " - " << snowData[6].date << endl;
cout << "=======================================" << endl;
cout << setw(11) << "DATE" << setw(18) << "DEPTH" << endl;
for (int x = 0; x < (NUM_ELEMENTS); x++)
cout << setw(10) << snowData[x].date << setw(18) << snowData[x].snowDepth << endl;
cout << endl;
}
如果你發佈結構的代碼和你的排序,那麼它會更容易找出什麼是錯的。 – ChrisWue 2011-04-01 06:38:34
同意。即使只是其中的一小部分。 – jonsca 2011-04-01 06:43:34
也許你對這個項目的一些混淆來自於你有*並行數組*。你沒有。你只有一個數組。這是一個結構數組,因此數組中的每個* single *元素都有*多個值*。但仍然只有一個陣列。如果您認爲*有平行數組,那麼您將需要完全丟棄「數組」數據類型。我建議你拿這段代碼給你的老師,以確認你正朝着正確的方向前進,而不是浪費太多時間。 – 2011-04-02 18:40:18