CS50 pset3 多个程序为 print_winner 函数给出错误(没有打印两个选举获胜者)

qwert9988

我已经完成了 cs50 的 pset3 的多个程序,但是在我的代码上运行 check50 时,我收到以下针对 print_winner 函数的错误:

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:) print_winner identifies Alice as winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:( print_winner prints multiple winners in case of tie
    print_winner function did not print both winners of election
:) print_winner prints all names when all candidates are tied

即使我的函数确实在出现平局的情况下打印了多个获胜者(并且我针对各种情况对其进行了多次测试),但此错误仍然存​​在,我不知道为什么。有人可以检查我下面的代码并详细说明代码有什么问题吗?(如果它非常混乱/低效,我很抱歉 - 我花了一段时间来写它,因为我花了一段时间才弄清楚如何打印多个获奖者):

void print_winner(void)
{
    int j = 0;
    int max = candidates[0].votes;
    int competitor;
    int competitor2;
    string winner = candidates[0].name;

    // loop over all candidates + determine candidate with most votes (max)
    // + name candidate with most votes (winner)
    for (j = 1; j < candidate_count; j++)
    {
        competitor = candidates[j].votes;
        if (max < competitor)
        {
            max = competitor;
            winner = candidates[j].name;
        }
    }

    // loop over candidates again to determine if multiple winners + print winner/winners
    for (int f = 0; f < candidate_count; f++)
    {
        competitor2 = candidates[f].votes;
        if (max == competitor2 && candidates[f].name != winner)
        {
            for (int i = 0; i < candidate_count; i++)
            {
                printf("%s\n", candidates[i].name);
            }
            return;
        }
    }
    printf("%s\n", winner);
    return;
}

编辑:正如 DinoCoderSaurus 指出的那样,我的i循环有问题。我意识到这甚至没有必要,因为我的f循环已经提供了相同的功能。下面,我删除了i循环,稍微修改了f循环,并将该printf("%s\n", winner);行移到f循环之前,所以如果有多个获胜者,他们会按正确的顺序打印:

void print_winner(void)
{
    int j = 0;
    int max = candidates[0].votes;
    int competitor;
    int competitor2;
    string winner = candidates[0].name;

    // loop over all candidates + determine candidate with most votes (max)
    // + name candidate with most votes (winner)
    for (j = 1; j < candidate_count; j++)
    {
        competitor = candidates[j].votes;
        if (max < competitor)
        {
            max = competitor;
            winner = candidates[j].name;
        }
    }

    printf("%s\n", winner);
    // loop over candidates again to determine if multiple winners + print winner/winners
    for (int f = 0; f < candidate_count; f++)
    {
        competitor2 = candidates[f].votes;
        if (max == competitor2 && candidates[f].name != winner)
        {
            printf("%s\n", candidates[f].name);
        }
    }
    return;
}
DinoCoderSaurus

试试这个选举:3 个候选人:a、b、c。投票的五个选民:a、a、b、b、c。

什么是预期的输出?程序给出什么输出?

问题出在i循环中。如果找到“第二个”获胜者,将打印哪些候选人姓名?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CS50 多种气味测试。关于如何开发这个 void print_winner(void) 函数的最佳实践是什么

如何正确打印 JPEG 文件的字节?- CS50 PSET3 恢复

CS50 Pset3错误:预期的标识符或'('

卡在CS50 PSET3频率中

CS50x pset3(为什么我的程序未通过Check50?)

CS50 多个,在平局的情况下不打印多个获胜者

CS50 Pset3音乐-sizeof(string)在做什么?

CS50的pset3 Tideman ...我的锁定功能似乎不起作用

如何实现cs50的pset3频率并理解notes.c

cs50 pset 1 Cash仅打印出零

cs50缩写pset2意外错误

CS50 (pset2) - 在打印首字母的 C 程序中无法理解的行为

获取分段错误。Valgrind 上大小为 1 的内存写入无效(CS50 PSET5 拼写器)

根据 CS50 的 pset3 潮汐人中的 check50 排序对不起作用

在c的拼写程序中实现多个功能会返回所有拼写错误的单词。在cs50 pset5拼写器中

CS50 (2020) 恢复程序中的分段错误

cs50 pset7 PHP查询函数where子句中的未知列

错误:“free():在 tcache 2 中检测到双重空闲” - CS50 PSET 4:恢复

CS50 PSet 1贪婪

CS50 Pset7财务

Tideman中的投票功能(CS50的pset 3)

CS50 - PSET3 跑掉 - 無法理解投票功能,更具體地說,偏好[voter][rank] = i; 做

CS50泰德曼“当几双并列时会打印选举的获胜者”失败

我的CS50 Vigenere密码程序有什么问题?

CS50:Check50没有得到任何输出。程序对我有效

CS50 Caesar 输出为 aaaaa

为什么我在 CS50 的 filter(less) (PSET 4) 中的模糊功能没有返回正确的值?

CS50 Pset4边缘编码-类型'RGBTRIPLE [width]'的索引601错误超出范围

我在退出时有字节在使用,但似乎无法确定确切位置。(cs50, pset5)