gpt4 book ai didi

c - 对数组进行排序会将值替换为零

转载 作者:行者123 更新时间:2023-11-30 15:10:24 25 4
gpt4 key购买 nike

问题开始于数组的最底部附近。我打印百分比[x]值以确保它们存在,然后在排序后,其中三个用零填充。我已经四处寻找了一段时间,但没有找到任何东西。

#include <stdio.h>

int main()
{
int votes[5][4]={192,48,206,37,
147,90,312,21,
186,12,121,38,
114,21,408,39,
267,13,382,29};

char cand[4]={'A','B','C','D'};

int row_totals[5];
int col_totals[4]; //This is left for you to do
//see code below to see how
//row_totals were calculated.

int total_votes;
//use a for loop to calculate
//using either row_totals or col_totals


float percent[4]; //use a for loop to calculate based on

int swap; //total_votes and col_totals

//be sure to print the totals for the candidates


//at the end, you will need to sort the percent array
//bringing along the values from the cand array. That is
//if a swap is made in the percent array, do the same
//swap in the cand array.

//then if percent[3] is greater than 50, declare cand[3] the winner
//if percent[3] is not greater than 50, declare a run-off
//between cand[2] and cand[3]

int row,col;

for(row=0;row<=4;row++) // used to find the row total
{
row_totals[row]=0;

for(col=0;col<=3;col++)
{
row_totals[row] += votes[row][col];
}
}

for(col=0; col <=3; col++)
{
col_totals[col]=0;
for(row =0; row <= 4; row++) // used to find the column total
{
col_totals[col] += votes[row][col];
}
}

for(row =0; row<=4; row++)
{
total_votes += row_totals[row]; //finds the total amount of the votes
}


printf(" Candidate Candidate Candidate Candidate Total\n");
printf(" Precinct: A B C D Votes\n");
for(row=0;row<=4;row++)
{
printf("%6d",row+1);
for(col=0;col<=3;col++)
{
printf("%12d",votes[row][col]);

}
printf("%11d\n",row_totals[row]);

}




printf("Total\nVotes:");
for(col=0; col<=3; col++)
{
printf("%12d", col_totals[col]);

}
printf(" %d", total_votes);
printf("\nPrecentage:");
for(col=0; col <= 3; col++)
{
percent[col] = (col_totals[col] / (double)total_votes); //calculates percentages
printf("%11f", percent[col]);
}
int x,y;
for(x=0; x<=3; x++)
{

printf("\n%f", percent[x]);
}



for(x=0; x<3; x++)
{
for(y=0;y<(3-x); y++)
{
if(percent[y] > percent[y+1])
{
swap = percent[y];
percent[y] = percent[y+1];
percent[y+1]= swap;

}
}
}

for(col=0; col<4; col++)
{

printf("\n%f", percent[col]);
}
return 0;

最佳答案

用于交换的临时变量是一个整数,但交换的值是 0 到 1 之间的 float ,在转换为 int 时将变为零。

这个错误很难发现,因为临时变量是在长 main 函数的顶部声明的,远离实际的交换代码。您可以将临时变量设置为交换范围的局部变量:

for (x = 0; x < 3; x++) {
for (y = 0; y < (3 - x); y++) {
if (percent[y] > percent[y + 1]) {
float swap = percent[y];

percent[y] = percent[y + 1];
percent[y + 1] = swap;
}
}
}

其他问题:

  • 您的total_votes未初始化为零。

  • 请考虑将循环编写为

    for (row = 0; row < nrows; row++) ...

    而不是

    for (row = 0; row <= nrows - 1; row++) ...

    这是一个常见的 C 习惯用法。您的循环使用硬编码值,但稍后您可能需要像上面两个示例一样具有变量限制,其中“小于项目数”变体更具可读性。

  • 打印时,换行符应位于打印格式的末尾,而不是开头。这是自然的打印方式。它还有一个好处是,当将换行符打印到控制台时,输出缓冲区将被刷新。

  • 这是次要的,但请选择一种浮点类型。您使用float,但使用double计算百分比。我推荐 double,它是现代机器上的标准浮点类型。

关于c - 对数组进行排序会将值替换为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36185749/

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