如何对float数组进行排序? [英] How do I sort the float array ?
问题描述
//以下是根据获得的分数对学生数据进行排序的功能。所以我首先尝试对创建的临时数组进行排序,但每个循环的输出仅为0.000000。 //
//The following is a function that sorts a students data according to the score that got. So I first try to sort the temp array created but the output is just 0.000000 for each loop. //
void sortbyscore(int entry,char name[entry][19],char surname[entry][19],float score[entry])
{
float temp[entry],tempvar;
int i,j;
for(i=0;i<entry;i++)>
{
temp[i]=score[i];
}
for(i=0;i<entry-1;i++)>
{
for(j=0;j=entry-i-1;j++)
{
if(temp[j]<temp[j+1])>
{
tempvar=temp[j];
temp[j]=temp[j+1];
temp[j+1]=tempvar;
}
}
}
for(i=0;i<entry;i++)>
{
printf("%f",&temp[i]);
}
}
推荐答案
首先查看得分中的值
数组,以及条目
值的大小。
在函数开头添加此代码:
Start by looking at the values in thescore
array, and the size of theentry
value.
Add this code at the start of the function:
printf("%d\n",&entry);
for(i=0;i<entry;i++)>
{
printf("%f\n",&score[i]);
}
如果一切看起来都不错,请使用调试器来查看代码,看看循环中到底发生了什么。
If that all looks good, use the debugger to follow your code through and look at what exactly happens in your loops.
您的程序根本不会结束。
你的嵌套for循环条件无效。
Your program is not suppose to end at all.
your nested for loop condition is invalid.
j=entry-i-1
//it should be
j<entry-i-1>
</entry-i-1>
i猜这可能是复制粘贴错误。 />
但是获得0.00000000是printf函数不正确。
它应该是
i guess this can be copy paste error.
But getting 0.00000000 is printf function is not correct.
it should be
for(i=0;i<entry;i++)>
{
// printf("%f",&temp[i]);
printf("%f",temp[i]);
}
for(i = 0 ; i< entry-1; i ++)>
{
for(j = 0; j = entry-i-1; j ++)
{
for(i=0;i<entry-1;i++)>
{
for(j=0;j=entry-i-1;j++)
{
更改为
change to
for(i = 0; i < entry-1; i++)
{
for(j = i+1; j < entry; j++)
{
这篇关于如何对float数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!