c - RUNOFF PSET3 - bool is_tie(int min) 函数在 Check50 中给出错误,无法删除

标签 c function boolean cs50

我尝试了多种方法来执行bool is_tie函数中的代码;但是,我似乎总是在 check50 中收到相同的错误。

也就是说,我已经使用以下函数运行了该程序,使用最大候选人数(10 名选民),没有任何问题,并且有 7 名被淘汰的候选人或 2 名候选人,程序毫无问题地打印出 2 名并列候选人。

我已经通过 debug50 运行了代码,在消除了另外两个候选人后,通过显示并列候选人的名字,它似乎可以正常工作;但是,在 check50 工具中,上述错误继续出现。

我还使用了 printf 函数来显示函数内的所有值,所有这些值似乎都是正确的。

我还在下面的 is_tie 函数中使用了 else {istie = false;} 我遇到了问题;但是,我收到了与错误下方的代码相同的错误。

**:( is_tie 在一些候选者被淘汰后检测到平局 - is_tie 没有返回 true **

bool is_tie(int min)
{
    int ties = 0;
    bool istie = false;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min && candidates[i].eliminated == false)
        {
            ties++;
            int elim_cands = candidate_count - candidates_out;

            if (ties == candidate_count || ties == elim_cands)
            {
                istie = true;
                break;
            }
            else
            {
                istie = false;
            }

        }

    }
    return istie;
}

同样,我已经能够使用下面的代码消除错误;但是,我只收到这两个错误。

:( 当选举不平局时 is_tie 返回 false
is_tie 没有返回 false
:( 当只有部分候选人并列时 is_tie 返回 false
is_tie 没有返回 false

 bool is_tie(int min)
{
    int ties = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min && candidates[i].eliminated == false)
        {
            ties++;
            return true;
        }
    }
    return false;
}

如果您有任何想法,请告诉我可以尝试什么。我一整天都在处理同样的错误,尽管该程序对我有用,但我无法提交带有错误的程序。我们将不胜感激您的想法。

所有其他功能都正常工作,问题集中的主要代码与最初提供的相同。我做到了;但是,添加一个名为candidates_out的全局变量来跟踪被淘汰的候选人的数量。

这是主要程序

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9


// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
    string name;
    int votes;
    bool eliminated;
}
candidate;

// Array of candidates
candidate candidates[MAX_CANDIDATES];

// Numbers of voters and candidates
int voter_count;
int candidate_count;
int candidates_out = 0;

// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: runoff [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX_CANDIDATES)
    {
        printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
        candidates[i].eliminated = false;
    }

    voter_count = get_int("Number of voters: ");
    if (voter_count > MAX_VOTERS)
    {
        printf("Maximum number of voters is %i\n", MAX_VOTERS);
        return 3;
    }

    // Keep querying for votes
    for (int i = 0; i < voter_count; i++)
    {

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            // Record vote, unless it's invalid
            if (!vote(i, j, name))
            {
                printf("Invalid vote.\n");
                return 4;
            }
        }

        printf("\n");
    }

    // Keep holding runoffs until winner exists
    while (true)
    {

        // Calculate votes given remaining candidates
        tabulate();

        // Check if election has been won
        bool won = print_winner();
        if (won)
        {
            break;
        }

        // Eliminate last-place candidates
        int min = find_min();
        bool tie = is_tie(min);

        // If tie, everyone wins
        if (tie)
        {
            for (int i = 0; i < candidate_count; i++)
            {
                if (!candidates[i].eliminated)
                {
                    printf("%s\n", candidates[i].name);
                }
            }
            break;
        }

        // Eliminate anyone with minimum number of votes
        eliminate(min);

        // Reset vote counts back to zero
        for (int i = 0; i < candidate_count; i++)
        {
            candidates[i].votes = 0;
        }
    }
    return 0;
}

**SPOLER 警报 - 以下是与程序相关的功能代码:

bool vote(int voter,int order, string name) (void)tabulate(void) bool print_winner(void) bool is_tie(int min) int find_min(void) Elimination(int min)

   bool vote(int voter, int rank, string name)
   {
    for (int number = 0; number < candidate_count; number++)
    {
        if (strcmp(name, candidates[number].name) == 0)
        {
            preferences[voter][rank] = number;
            return true;
        }


    }
    return false;
}

    // Tabulate votes for non-eliminated candidates
    void tabulate(void)
{
    for (int j = 0; j < 1; j++)  //loop to record each voters rank 1
    {
        for (int i = 0; i < voter_count; i++)
        {
            int number = preferences[i][j];

            if (candidates[number].eliminated == false) //if candidate is not eliminated, add 1 to candidates votes
            {
                candidates[number].votes += 1;
                j = 0;
            }
            else                                        //if candidate is eliminated, go to the same voters next preference
            {
                i--;
                j++;
            }
        }

    }
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    float majority = (float)voter_count / 2;

    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > majority)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
    }
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    int min = candidates[0].votes;
    for (int i = 1; i < candidate_count; i++)
    {
        if (min > candidates[i].votes && candidates[i].eliminated == false)
        {
            min = candidates[i].votes;
            int Position = i;
        }
    }
    return min;
}


// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{

    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min)
        {
            candidates[i].eliminated = true;
            candidates_out++;
        }
    }

}

最佳答案

问题的根源:

add a global variable called candidates_out to keep track of the number of eliminated candidates.

来自the spec :

you should not modify anything else in plurality.c

check50 使用它自己的黑魔法来运行测试并验证结果。 IMO 在本类(class)中学到的重要技能之一是严格遵循规范。在商业环境中,不这样做可能会转化为真正的金钱。

关于c - RUNOFF PSET3 - bool is_tie(int min) 函数在 Check50 中给出错误,无法删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728647/

相关文章:

有人能告诉我这个 C 程序做错了什么吗?

c# - 有人可以破译 timeGetTime() 或 QueryPerformanceCounter/QueryPerformanceFrequency 是否具有较低的开销或/和准确性?

java - 如何将数组放入 IF 语句中

string - 如何将 "y"/"n"列映射到 Scala 的 Slick 中的 boolean 值?

cuda for循环疑惑

c++ - 在 vscode 中使用特定缩进/对齐 C/C++ 自动格式化

c - 我正在尝试使用 C 解决给定总线数量的最大平台数量问题。我遇到错误并卡在那里

python - 我应该在函数内部编写函数还是将它们全部写在全局框架中?

javascript - 函数内定义的变量导致引用错误 : not defined

php - 绑定(bind) boolean 值的 bind_param 问题