在最后一个函数 "maximum "中,我试图通过比较两个双数来打印出最大的数字,但它不让我把它分配给一个变量,并给我一个错误 "Initializing 'double' with an expression of in incompatible type 'double [10]'"。我不知道问题出在哪里,也不知道如何找到得分最高的选手。
#define MAX 10
// Main struct
struct players{
char name[20];
double score[MAX][MAX];
};
// function declaration
int playerName(struct players *test);
int playerScore(struct players *test, int nop);
double largest(struct players *test, int nof, int nop);
int main(void) {
int i,j;
int nop, nof;
struct players mainPlayers[MAX];
nop = playerName(mainPlayers);
nof = playerScore(mainPlayers, nop);
for (i = 0; i < nop; i++) {
printf("%s ",mainPlayers[i].name);
for (j = 0; j < nof; j++) {
printf("%.2lf ",*mainPlayers[i].score[j]);
}
}
return 0;
}
// gets players name
int playerName(struct players *test){
int i;
int nop = 0;
printf("Enter Number of players: ");
scanf("%d", &nop);
for (i = 0 ; i<nop; i++) {
printf("Enter player %d's name: ", i+1);
scanf("%s", test[i].name);
}
return nop;
}
// gets players score
int playerScore(struct players *test, int nop){
int i,j;
int nof = 0;
printf("Enter Number of fields: ");
scanf("%d", &nof);
for (i = 0 ; i<nop; i++) {
for (j = 0; j < nof; j++) {
printf("Enter player %s's score %d: ", test[i].name, j+1);
scanf("%lf", test[i].score[j]);
}
}
return nof;
}
// supposed to get the largest score out of all the scores and print that players name and score in the main function.
float largest(struct players *test, int nof, int nop){
int i,j;
double largest = test[i].score[j];
for (i = 0; i < nop; i++) {
for (j = 0; j < nof; j++) {
if (test[i].score[j] < test[i].score[j+1]) {
largest = test[i].score[j];
}
}
}
}
最好是限制变量的范围,因此,建议去掉诸如:
int i,j;
和,在每个 for()
循环替换表达式,如
for (i = 0; i < nop; i++)
用:
for ( int i = 0; i < nop; i++ )
同样地,对每一个 for()
环路
在功能上。largest()
关于:
int i,j;
double largest = test[i].score[j];
本地变量 i
和 j
在没有被初始化为任何特定值的情况下被使用。 其结果是未定义行为。
关于。
for (j = 0; j < nof; j++) {
if (test[i].score[j] < test[i].score[j+1])
由于局部变量 j
递增到数组的最大有效索引,表达式。test[i].score[j+1]
将会超出数组的末端。 记住:任何数组的有效索引(在C语言中)都是0...(数组中的元素数-1)。
关于。
scanf("%s", test[i].name);
本 %s
将允许用户输入超过19个字符。 用户这种操作的结果会导致缓冲区溢出和未定义行为。 同时应该检查返回值,以确保操作成功。 建议。
if( scanf("%19s", test[i].name) != 1 )
{
fprintf( stderr, "scanf for player %d name failed\n", i );
exit( EXIT_FAILURE );
}
其中19是因为字段 name[]
长20个字符,并且 %s
总是给输入添加一个NUL字节。
在函数中。largest()
关于:
double largest = test[i].score[j];
由于变量 i
和 j
建议使用:
double largest = 0.0;
并将这一行移到紧接着的位置
for (i = 0; i < nop; i++) {
这样每一个新的玩家都会被 "重置"。
现在说说你的问题。
关于:
float largest(struct players *test, int nof, int nop){
....
double largest = test[i].score[j];
不要在函数中使用函数名作为局部变量。 编译器将其视为 "递归 "调用,而不是局部变量。
建议为局部变量使用一个唯一的名字。
在你的播放器的定义中。
struct players{
char name[20];
double score[MAX][MAX];
};
每一个玩家都有一个名字 还有一个100分的二维网格
我不知道这是要代表什么样的游戏,但通常情况下,一个玩家只有一个分数。 为什么你要给每个玩家一个100分的大矩阵?
#include <stdio.h>
#define MAX 10
// Main struct
struct players{
char name[20];
double score[MAX][MAX];
};
// function declaration
int playerName(struct players *test);
int playerScore(struct players *test, int nop);
double largest(struct players *test, int nof, int nop);
void ppg(struct players *test, int nof, int nop);
int main(void) {
int nop, nof;
struct players mainPlayers[MAX] = { 0 };
nop = playerName(mainPlayers);
printf("\n");
nof = playerScore(mainPlayers, nop);
printf("\n");
ppg(mainPlayers, nof, nop);
return 0;
}
// gets players name
int playerName(struct players *test){
int i;
int nop = 0;
printf("Enter Number of players: ");
scanf("%d", &nop);
for (i = 0 ; i<nop; i++) {
printf("Enter player %d's name: ", i+1);
scanf("%s", test[i].name);
}
return nop;
}
// gets players score
int playerScore(struct players *test, int nop){
int i,j;
int nof = 0;
printf("Enter Number of fields: ");
scanf("%d", &nof);
for (i = 0 ; i<nop; i++) {
for (j = 0; j < nof; j++) {
printf("Enter player %s's score %d: ", test[i].name, j+1);
scanf("%lf", &test->score[i][j]);
}
}
return nof;
}
// finds the largest number (Did not use)
double largest(struct players *test, int nof, int nop){
int i = 0,j = 0;
double largest2 = test->score[i][j];
for (i = 0; i < nop; i++) {
for (j = 0; j < nof; j++) {
if (test->score[i][j] > test->score[i][j+1] && test->score[i][j+1] != '\0') {
largest2 = test->score[i][j];
}
}
}
return largest2;
}
// Calculates and prints the player name and their ppg
void ppg(struct players *test, int nof, int nop){
double sum = 0;
int div = 0;
double ppg = 0;
char *name = NULL;
for (int i = 0; i<nop; i++) {
for (int j = 0; j<nof; j++) {
if (test->score[i][j] == 0) {
div++;
}
sum+= test->score[i][j];
}
sum/= (nof-div);
if (ppg < sum) {
ppg = sum;
name = test[i].name;
}
sum = 0;
div = 0;
}
printf("%s had the highest ppg with %.2lf points\n", name, ppg);
}