gpt4 book ai didi

c++ - 双重选择对数组进行排序 - honeSTLy stumped

转载 作者:行者123 更新时间:2023-11-27 23:54:22 25 4
gpt4 key购买 nike

void dualSort(int [], double [], int);
int main()
{
const int ARRAY_SIZE = 1000; // Array size
double accountNumbers[ARRAY_SIZE]; // Array with 1000 elements
double accountBalances[ARRAY_SIZE]; // Loop counter variable
int count = 0; // Input file stream object
ifstream inputFile;

// Open the file.
inputFile.open("FinalNumbers.txt");

// Read the numbers from the file into the array
while (count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count] ) {
count++;
}

inputFile.close();

// Display the read data
cout << "The bank account numbers are: " << endl;
for (int count = 0; count < ARRAY_SIZE; count++) {
cout << accountNumbers[count] << "\n" << accountBalances[count] << " " << endl;
}

void dualSort(int accountNumbers[], double accountBalances, int ARRAY_SIZE);



}

我需要使用选择排序或冒泡排序算法。

我有Bank Account Numbers,必须与那里的Account Balance进行相应的升序排序,所有数据都是从文件中读取的。

排序数据后,我必须将其重写到另一个文件中,这是我最不担心的。

所以问题是我如何按升序对我的 accountNumbers 以及紧随其后的 accountBalance 进行排序。

最佳答案

您需要根据 accountNumbers 进行排序,但将每个交换操作应用于两个数组。

下面是使用选择排序的代码:

void dualSort(int accountNumbers[], double accountBalances[], int ARRAY_SIZE)
{
int minIndex;

for(int i = 0; i < ARRAY_SIZE - 1; i++)
{
minIndex = i;

for(int j = i + 1; j < ARRAY_SIZE; j++)
{
if(accountNumbers[j] < accountNumbers[minIndex])
{
minIndex = j;
}
}

swap(accountNumbers[i], accountNumbers[minIndex]);
swap(accountBalances[i], accountBalances[minIndex]);
}
}

关于c++ - 双重选择对数组进行排序 - honeSTLy stumped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com