我试图在整数数组中找到最小值。然后用具有该分数的人的姓名显示该值。我可以找到哪个值是最低的,并用像 player 1 或 player 2 这样的索引显示它,但我不能用名称代替那个索引。
#include <string.h>
#include <stdio.h>
#include <string.h>
#define LEN_NAME 34
#define NUM_NAMES 3
void lowest(int array[], char *fullName, int elements);
int main (void) {
int scores[NUM_NAMES] = { 230,330,423};
char firstName[NUM_NAMES][LEN_NAME] = {"john","james","mario"};
char lastName[NUM_NAMES][LEN_NAME] = {"goirgi", "edison", "luca"};
char *fullName[NUM_NAMES][LEN_NAME];
int i;
for (i=0; i < NUM_NAMES; i++) {
strcpy(fullName[i], firstName[i]);
strcat(fullName[i], " " );
strcat(fullName[i], lastName[i]);
printf("Your scores is %d with your full name is %s.\n",scores[i], fullName[i]);
}
lowest(scores,*fullName, NUM_NAMES);
return 0;
}
void lowest (int array[], char *fullName, int elements) {
int i,small = array[0], j;
for (i=0; i< elements; i++) {
if (array[i] < small) {
small = array[i];
j = i;
}
}
printf("The lowest scored %d with score %d.\n", j , small);
}