gpt4 book ai didi

c++ - 选择排序指针问题

转载 作者:太空狗 更新时间:2023-10-29 21:35:28 25 4
gpt4 key购买 nike

我的选择排序有问题。它没有按升序对列表进行排序,实际上根据我的调试器它没有做任何事情。我注意到当我在第 42 行调试它时它跳过了条件语句,我不明白为什么要这样做。我认为问题出在 if 语句的第二个 for 循环中。条件语句在本应为真时却为假。如果您能引导我走向正确的方向,我将不胜感激。我是来学习的。

#include <iostream>
void sortAscendingOrder(int*, int );
int main()
{
int* array = nullptr;
int input;

std::cout << "Enter the number of testscores you want to enter." <<std::endl;
std::cin >> input;

array = new int[input];

for(int count =0; count < input; count++)
{
std::cout << "Enter the test score" << (count +1) <<":" <<std::endl;
std::cin >> *(array+count);
}

sortAscendingOrder(array,input);

for(int count =0; count < input;count++)
{
std::cout << "\n" << *(array+count);
std::cout << std::endl;
}




return 0;
}
void sortAscendingOrder(int* input,int size)
{
int startScan,minIndex,minValue;

for(startScan =0; startScan < (size-1);startScan++)
{
minIndex = startScan;
minValue = *(input+startScan);
for(int index = startScan+1;index<size;index++)
{
if(*(input+startScan) < minValue) // Here this condition is false.
{
minValue = *(input+index);
minIndex = index;
}
}
*(input+minIndex)=*(input+startScan);
*(input+startScan)=minValue;
}
}

最佳答案

startScan是寻找最小值的起始位置。

if(*(input+startScan) < minValue)

永远不会为真,左边和右边总是相等的。

看来你想说

if(*(input + index) < minValue)

您的调试器应该能够生成 *(input + startScan) 的值作为监视或作为 future 调试 session 的表达式。

关于c++ - 选择排序指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290778/

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