未能正确计数正确添加_PAIRS TIDEMAN CS50

发布于 2025-01-29 07:43:48 字数 3759 浏览 3 评论 0原文

有人可以帮助我了解为什么我的代码失败了以下要点吗?此功能旨在通过获胜对更新对阵列,并相应地增加pair_count变量。我的函数代码如下:

void add_pairs(void)
{
    float cc = candidate_count;// have to set to float to make sure a float number is given as the result
    float majority = (cc / 2);
    // loop through preferences array using nested loop, find preferences for candidates, assign winner or loser depending on scores, increment pair count for each
    // loop nested for, conditional if preferences[i][j] > candidate_count / 2, winner = i loser =j, pair_count += 1. Add pair to pairs array.
    for (int i = 0; i < candidate_count; i++)// loops through preferences array
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] > (majority)) // increments pair count in the event there is a majority for one candidate over another
            {
                pair_count += 1;
                for (int k = 0; k < pair_count; k++) // adds that pair to the pairs array
                {
                    pairs[k].winner = i;
                    pairs[k].loser = j;
                }
            }
        }
    }
    printf("pc:%i\n", pair_count);
    return;
}

拟合较大的程序:

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

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];// if x prefer then the margin is x/voters - x

// locked[i][j] means i is locked in over j - adjacency matrix
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

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

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

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

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

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

以下是与上面的add_pairs代码相对应的check50结果

:( add_pairs在没有纽带时会生成正确的对计数

add_pairs function did not produce 3 pairs

:( add_pairs在存在纽带时会生成正确的对时计数

add_pairs function did not produce 2 pairs

:)带有获胜对的填充对阵列

:) add_pairs

如果我以./tideman asd为./tideman asd,则在以下偏好a,s,s,d -s,a,a,d -a,a,s,s,s, d。我在函数末尾的printf语句显示了一个正确的paim_count_ 3,但我仍然按照上述点失败

Could someone please help me to understand why my code fails the points below? This function is meant to update the pairs array with winning pairs and increment the pair_count variable accordingly. My code for the function is as follows:

void add_pairs(void)
{
    float cc = candidate_count;// have to set to float to make sure a float number is given as the result
    float majority = (cc / 2);
    // loop through preferences array using nested loop, find preferences for candidates, assign winner or loser depending on scores, increment pair count for each
    // loop nested for, conditional if preferences[i][j] > candidate_count / 2, winner = i loser =j, pair_count += 1. Add pair to pairs array.
    for (int i = 0; i < candidate_count; i++)// loops through preferences array
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] > (majority)) // increments pair count in the event there is a majority for one candidate over another
            {
                pair_count += 1;
                for (int k = 0; k < pair_count; k++) // adds that pair to the pairs array
                {
                    pairs[k].winner = i;
                    pairs[k].loser = j;
                }
            }
        }
    }
    printf("pc:%i\n", pair_count);
    return;
}

This fits into the larger program:

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

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];// if x prefer then the margin is x/voters - x

// locked[i][j] means i is locked in over j - adjacency matrix
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

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

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

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

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

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

Here are the check50 results corresponding to the add_pairs code above

:( add_pairs generates correct pair count when no ties

add_pairs function did not produce 3 pairs

:( add_pairs generates correct pair count when ties exist

add_pairs function did not produce 2 pairs

:) add_pairs fills pairs array with winning pairs

:) add_pairs does not fill pairs array with losing pairs

If I run the program as ./tideman a s d with 3 voters at the following preferences a, s, d - s, a, d - a, s, d. my printf statement at the end of the function shows a correct pair_count of 3 yet I still fail as per the points above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文