英文:
Logic error in function to search for highest scoring player(s) in a game
问题
以下是您要翻译的代码部分:
/* 函数 display_highest_score() 用于搜索存储一些玩家得分的数组,以找到得分最高的玩家(们)。问题在于,当有多个相同得分时,我的逻辑出现问题。
我的代码如下所示: */
void display_highest_score(char player_array[][STRING_LENGTH], int score_array[], int num_players) {
int high_num = 0; /* 存储最高玩家得分。 */
int position[MAX_PLAYERS] = { 0 }; /* 存储得分最高的玩家的位置。 */
int players = 1; /* 存储具有相同得分的玩家数量。 */
int j = 0; /* 循环变量。 */
/* 循环查找并存储得分最高的玩家(们)。 */
for (int i = 0; i < num_players; i++) {
if (high_num < score_array[i]) {
high_num = score_array[i];
position[j] = i;
j++;
}
else if (high_num == score_array[i]) {
players = players++;
}
}
/* 打印到屏幕的代码。 */
printf("\n得分最高的玩家(得分为 %d)是:\n\n", high_num);
for (j = 0; j < players; j++) {
printf("--> %s\n", player_array[position[j]]);
}
}
如果您需要进一步的帮助,请随时提问。
英文:
I am writing a function to search through an array that stores the score of some players for the highest scoring player(s). The problem is that the I can search and record the highest score easily but it is when there are multiple of a score that my logic falls appart.
My code is attached below:
/* Function display_highest_score() searches the player score array for the highest value and
any repetitions before printing the results to the screen.
Parameters: The player array, score array, and number of players.
Returns: Nothing.*/
void display_highest_score(char player_array[][STRING_LENGTH], int score_array[], int num_players) {
int high_num = 0; /* Stores the highest player score. */
int position[MAX_PLAYERS] = { 0 }; /* Stores the position of the player(s) with the highest score. */
int players = 1; /* Stores the number of players that have that score. */
int j = 0; /* A looping variable. */
/* Loop to find and store the highest scoring player(s). */
for (int i = 0; i < num_players; i++) {
if (high_num < score_array[i]) {
high_num = score_array[i];
position[j] = i;
j++;
}
else if (high_num == score_array[i]) {
players = players++;
}
}
/* Print to screen code. */
printf("\nPlayers with the highest score of %d are:\n\n", high_num);
for (j = 0; j < players; j++) {
printf("--> %s\n", player_array[position[j]]);
}
}
If the function is searching through an array, say score_array[10] = { 3, 1, 0, 10, 4, 8, 10, 0, 3, 16 }
then it will change the maximum score to 16
but it will print the players that have score 10
as having 16
instead.
I've tried to swap the position[j] = i; j++;
segment to the else if
statement but it still doesnt work
I'm still relativley new to c coding so any help would be appreciated.
答案1
得分: 0
你当前正在做的是为每个新的最高分添加一个位置索引。当它看到重复的最高分时,players变量将只增加,但不会记录players的索引。
你需要做的是,如果看到一个新的最高分(high_num < score_array[i]
),那么你应该清除positions数组并将玩家数量重置为0,以清除先前检测到的最高分,并以新值重新开始。
如果在赋值position[j] = i;
之前在if语句中设置j = 0
和players = 1
,它应该只显示最高的分数值。
在else语句中,你需要添加
position[j] = i;
j++
以跟踪所有相等的最高分。在那一点上,你不需要将players作为一个单独的变量,因为players将始终等于j。
英文:
What you are currently doing is adding a position index for every new high score that you see. The players variable will only increment when it sees a repeat high score but will not record that players index.
What you need to do is if a new high score is seen (high_num < score_array[i]
) then you should clear the positions array and reset number of players back to 0, in order to clear out previously detected high scores and start fresh with the new value.
If you set j = 0
and players =1
in the if before assigning position[j] = i;
it should only show the highest score values.
In the else statement, you need to add
position[j] = i;
j++
In order to track all high scores that are equal. At that point you don't need players as a separate variable since players will always equal j.
答案2
得分: 0
你需要在所有情况下都添加当前位置,但在找到新的最高分时清除先前存储的位置。此外,你应该将players
初始化为0,以处理空的玩家集合的特殊情况(在任何情况下,如果players==0
,你应该在最后打印错误消息):
int players = 0;
...
if (high_num <= score_array[i]) {
if (high_num < score_array[i]) {
high_num = score_array[i];
players = 0;
}
position[players++] = i;
}
...
英文:
You need to add the current position in all cases, but clear the previously stored positions when a new high score is found. Also, you should initialize players
to 0 to handle the corner case of an empty set of players (in any case, you should print an error message in case players==0
at the end):
int players = 0;
...
if (high_num <= score_array[i]) {
if (high_num < score_array[i]) {
high_num = score_array[i];
players = 0;
}
position[players++] = i;
}
...
答案3
得分: 0
我强烈建议用不同的逻辑来解决这个问题。最简单的解决方案是使用一个循环来找到最高分,然后使用另一个循环来打印出每个得分最高的玩家。类似这样的伪代码:
for (分数列表):
如果分数是新的最高分:
存储新的最高分
for (玩家列表):
如果玩家有最高分:
打印玩家
如果你仍然坚持要使用你已经有的代码,我首先会查看存储高分玩家数量的 players
变量。我认为每当找到新的最高分时,你应该将它设置为1,然后在出现平局时递增它,但你似乎跳过了这个步骤,只有在有平局的情况下增加了该值。
希望这有所帮助。
英文:
I would strongly recommend approaching this with different logic. The easiest solution would be using one loop to find the highest score, and then another loop to print out every player with that score. Something a bit like this pseudocode:
for (list of scores):
if score is new high score:
store new high score
for (list of players):
if player has high score:
print player
If you still really want to stick with the code you have, the first thing I'd look at is your players
value that stores the number of players with a high score. I think you should be setting it to 1 whenever you find a new high score, and then incrementing it whenever there's a tie, but you appear to be skipping that first part and only increasing the value when there is a tie for the high score.
Hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论