[英]Function in C to find max element in an array
You are participating in a game in which players collect points for various solved puzzles.您正在参与一个游戏,玩家在游戏中为各种已解决的谜题收集积分。 In the end, the player with the highest score wins.最后,得分最高的玩家获胜。 You would like to know how far behind the highest-scoring person everyone else is in order to know whether you still have a chance at winning.您想知道得分最高的人与其他人相差多远,才能知道您是否还有机会获胜。
Please write a C program that uses a function "behind()" (which you also have to write) in order to help with this task.请编写一个 C 程序,该程序使用 function “behind()”(您也必须编写)以帮助完成此任务。 Your program should first read, from the user input, the number of players participating in the game.您的程序应该首先从用户输入中读取参与游戏的玩家数量。 There are never more than 10 players in the game.游戏中的玩家永远不会超过 10 人。 Next, your program should read the current scores of each player and store them in an array.接下来,您的程序应该读取每个玩家的当前分数并将它们存储在一个数组中。 Then you should call the function behind(), to which you pass as a first argument, the array holding the player's scores, and as a second argument the number of players in the game.然后你应该调用 function behind(),作为第一个参数传递给它,数组保存玩家的分数,第二个参数是游戏中的玩家数量。 The function behind should replace the scores stored in the array with the number of points by which each individual player is behind the top-scoring player.后面的 function 应该将存储在数组中的分数替换为每个玩家落后于得分最高的玩家的分数。
To help you out, the main function of the program has already been written, so your job is simply to write the function behind(), whose protoype is also given to you.为了帮助你,程序的主要function已经写好了,所以你的工作就是在后面写function,它的原型也给你了。
#include <stdio.h>
void behind(int *, int);
int main(void) {
int array[10];
int N, i;
scanf("%d", &N);
for (i=0; i<N; i++) {
scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
printf("%d\n", array[i]);
}
return 0;
}
My question is how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?我的问题是我如何编写代码来查找数组元素的最大值,而不将最后一个元素与 memory 块之外的东西进行比较? I have something in mind like:我有一些想法,例如:
if(array[i] > array[i+1]) {
max = array[i];
} else {
max = array[i+1];
}
but realize it goes outside the bounds of the array once reaching the last element.但意识到一旦到达最后一个元素,它就会超出数组的范围。
" How I write the code to find the max value of the array elements without comparing the last element to something outside the memory block? " “我如何编写代码来查找数组元素的最大值而不将最后一个元素与 memory 块之外的东西进行比较? ”
You just need to set max
before going into the loop to 0
.您只需在进入循环之前将max
设置为0
。 Then you compare if array[i]
is greater than max
at each iteration.然后比较每次迭代时array[i]
是否大于max
。 If it is, max
gets assigned by array[i]
.如果是,则max
由array[i]
分配。 The loop condition i < len
checks whether there are more elements or not in array
:循环条件i < len
检查array
中是否有更多元素:
int max = 0;
for (unsigned int i = 0; i < N; i++) {
if (array[i] > max) {
max = array[i];
}
}
Another thing is that you have first a fixed size array of 10 elements with另一件事是,您首先有一个由 10 个元素组成的固定大小的数组
int array[10];
and then later ask the user to input the amount of elements:然后要求用户输入元素的数量:
scanf("%d", &N);
If the user inputs a number above 11
for N
, you accessing memory beyond the bounds of the array in the loops.如果用户为N
输入大于11
的数字,则您在循环中访问 memory 超出了数组的边界。
In this case, a declaration of array
as variable length array after the scanf()
call would be more appropriate:在这种情况下,在scanf()
调用之后将array
声明为可变长度数组会更合适:
int N, i;
scanf("%d", &N);
int array[N];
Since there will never be more than 10 players: " There are never more than 10 players in the game " - You could also leave array
of fixed size with 10
elements and analyze the input of N
in a loop and ask the user to reinput a valid number between 0 and 10 if N
isn't in this range.由于永远不会超过 10 名玩家:“游戏中永远不会超过 10 名玩家” - 您也可以将固定大小的array
保留为10
元素,并在循环中分析N
的输入并要求用户重新输入如果N
不在此范围内,则为 0 到 10 之间的有效数字。 You use N
for the iterations only but it doesn't influence the array size.您仅将N
用于迭代,但它不会影响数组大小。
int array[10];
while (1) {
scanf("%d", &N);
if ( N >= 0 || N <= 10 ) {
break;
}
printf("Please reinput a valid number!\n");
}
Side notes:旁注:
Always check the return value of scanf()
for an input error.始终检查scanf()
的返回值是否存在输入错误。 This happens frequently and to check is a very good practice and makes your program way more safe.这种情况经常发生,检查是一种非常好的做法,可以使您的程序更加安全。
VLAs are not portable and only possible to compiler compliant to C99 and above. VLA 不可移植,只能用于符合 C99 及更高版本的编译器。 To maintain portability and to keep safety use an dynamically allocated array instead.为了保持可移植性和安全性,请改用动态分配的数组。
Related:有关的:
how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?我如何编写代码来查找数组元素的最大值而不将最后一个元素与 memory 块之外的东西进行比较?
You can code the behind()
something like:您可以对behind()
进行编码,例如:
void behind(int *arr, int n) {
int max_value = 0;
for (int i = 0; i < n; i++)
if (arr[i] > max_value)
max_value = arr[i];
printf("Max value: %d\n", max_value);
}
Rather than defining an array, you may declare the array
as a pointer variable, that'd be quite helpful.您可以将array
声明为指针变量,而不是定义数组,这会很有帮助。 You can then dynamically allocate the memory and then use it like an array thereafter, also note that you're trying to pass a pointer, so it's better take some benefit of pointer declaration.然后,您可以动态分配 memory ,然后像数组一样使用它,还要注意您正在尝试传递指针,因此最好从指针声明中受益。
The full approach to do that:完整的方法:
#include <stdio.h>
#include <stdlib.h> // for dynamic memory allocation
void behind(int *, int);
int main(void) {
int *array; // declaring as pointer rather than array
int N, i;
scanf("%d", &N);
if (N > 11) { // must be less than 10
printf("Must be lesser than or equal to 10!\n");
return -1;
}
// dynamically allocating the required memory
array = (int *)malloc(sizeof(int) * N);
for (i = 0; i < N; i++)
scanf("%d", &array[i]);
// here we go
behind(array, N);
for (i = 0; i < N; i++)
printf("%d\n", array[i]);
return 0;
}
void behind(int *arr, int n) {
int max_value = 0;
for (int i = 0; i < n; i++)
if (arr[i] > max_value)
max_value = arr[i];
printf("Max value: %d\n", max_value);
}
An example output is as follows: output 示例如下:
5 // ------------ INPUT
12
45
23
1
05
Max value: 45 // --- OUTPUT
12
45
23
1
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.