You can say: “我应该声明一个新变量,还是修改我的代码?”

huangapple go评论64阅读模式
英文:

shoud I declare a new variable, or modify my code?

问题

编译错误:

7066839625989131.c: 在函数‘main’中:
7066839625989131.c:22:9: 错误: ‘team2_Weight’ 在此函数中可能未初始化 [-Werror=maybe-uninitialized]
         printf("Total weight for team 2: %d", sum2);
         ^
7066839625989131.c:21:9: 错误: ‘team1_Weight’ 在此函数中可能未初始化 [-Werror=maybe-uninitialized]
         printf("Total weight for team 1: %d\n", sum1);
         ^
cc1: 某些警告被视为错误

需要初始化 team1_Weightteam2_Weight 变量,否则编译器会报错。您可以在程序开始时将它们初始化为0,如下所示:

#include<stdio.h>

int main(){
    int nop, i, sum1 = 0, sum2 = 0; // 初始化变量
    int player1, player2, answer;
    
    scanf("%d", &nop);
    
    for (i = 0; i < nop; ++i){
        scanf("%d", &player1);
        team1_Weight = team1_Weight + player1;
        scanf("%d", &player2);
        team2_Weight = team2_Weight + player2;
    }
    
    sum1 = team1_Weight;
    sum2 = team2_Weight;
    answer = (sum1 > sum2);
    
    if (answer){
        printf("Team 1 has an advantage\n");
        printf("Total weight for team 1: %d\n", sum1);
        printf("Total weight for team 2: %d", sum2);
    } else {
        printf("Team 2 has an advantage\n");
        printf("Total weight for team 1: %d\n", sum1);
        printf("Total weight for team 2: %d", sum2);
    }
    
    return 0;
}

请注意,我添加了 return 0; 来指示程序成功结束。这是一个修复编译错误的方法。

英文:

The question states:

> You decide to bet on the final match of the Tug of War National
> Championship.
>
> Prior to the match the names and weights of the players are presented,
> alternating by team (team 1 player 1, team 2 player 1, team 1 player
> 2, and so on). There is the same number of players on each side. You
> record the player weights as they are presented and calculate a total
> weight for each team to inform your bet. You write a C program to
> assist with this.
>
> Your program should first read an integer indicating the number of
> members per team. Then, the program should read the player weights
> (integers representing kilograms) alternating by team.
>
> After calculating the total weight of each team, the program should
> display the text "Team X has an advantage" (replacing X with 1 or 2
> depending on which team has a greater total weight).
>
> You will then display the text "Total weight for team 1:" followed by
> the weight of team 1, then "Total weight for team 2:" followed by the
> weight of team 2 (see example below).
>
> You are guaranteed that the two teams will not have the same total
> weight.

I did this on an online compiler and got the right answer

#include&lt;stdio.h&gt;
int main(){
    int nop, i, sum1, sum2;
    int team1_Weight, team2_Weight, player1, player2, answer;
    scanf(&quot;%d&quot;, &amp;nop);
    for (i=0;i&lt;nop;++i){
        scanf(&quot;%d&quot;, &amp;player1);
        team1_Weight=team1_Weight+player1;
        scanf(&quot;%d&quot;, &amp;player2);
        team2_Weight=team2_Weight+player2;
    }
    sum1=team1_Weight;
    sum2=team2_Weight;
    answer=(sum1 &gt; sum2);
    if (answer){
        printf(&quot;Team 1 has an advantage\n&quot;);
        printf(&quot;Total weight for team 1: %d\n&quot;, sum1);
        printf(&quot;Total weight for team 2: %d&quot;, sum2);
    }else{
        printf(&quot;Team 2 has an advantage\n&quot;);
        printf(&quot;Total weight for team 1: %d\n&quot;, sum1);
        printf(&quot;Total weight for team 2: %d&quot;, sum2);
    }
    
}

When I submitted this as the answer, I got this:

Compilation error :

7066839625989131.c: In function ‘main’:
7066839625989131.c:22:9: error: ‘team2_Weight’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
         printf(&quot;Total weight for team 2: %d&quot;, sum2);
         ^
7066839625989131.c:21:9: error: ‘team1_Weight’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
         printf(&quot;Total weight for team 1: %d\n&quot;, sum1);
         ^
cc1: some warnings being treated as errors

答案1

得分: 2

你在注释中提到,在初始化之前使用了 team_weight1team_weight2。这些变量的初始值是不确定的,这使得你的程序的输出无法确定。

在线评测编译器可能会给这些变量赋予你期望的值(可能是 0),但不能依赖这一点。解决方案很简单,就是显式初始化它们。

英文:

As noted in comments, you have used team_weight1 and team_weight2 before initializing them. The initial values of these variables are indeterminate, which makes the output of your program impossible to determine.

It's possibly the online judge compiler gave these the values you were expecting (likely 0) but this cannot be counted upon. The solution is simply to explicitly initialize them.

huangapple
  • 本文由 发表于 2023年7月18日 10:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定