英文:
'" error: initializer element is not a compile-time constant
问题
I'll provide the translation of the code portion as you requested:
#include <cs50.h>
#include <stdio.h>
int number_scores = get_int("需要多少个分数: ");
float average(int array[]);
int main(void)
{
int number_scores = get_int("需要多少个分数: ");
int scores[N];
for(int i = 0; i < number_scores; i++)
{
scores[i] = get_int("分数: ");
}
printf("平均分: %f\n", average(scores));
}
float average(int array[])
{
int sum = 0;
for (int i = 0; i < number_scores; i++)
{
sum = sum + array[i];
}
return sum / (float)number_scores;
}
Please note that I've translated the comments and variable names, but the structure of the code remains the same.
英文:
#include <cs50.h>
#include <stdio.h>
int number_scores = get_int("How many scores do you need: ");
float average(int array[]);
int main(void)
{
int number_scores = get_int("How many scores do you need: ");
int scores[N];
for(int i = 0; number_scores < 3; i++)
{
scores[i]= get_int("Scores: ");
}
printf("Average: %f\n", average(scores));
}
float average(int array[])
{
int sum = 0;
for (int i = 0; i < number_scores;i++)
{
sum = sum + array[i]
}
return sum / (float)number_scores;
}
This is my code I am trying to print of the average between the number of test scores the user is giving us which is int number_scores. But when I try to "make"the program in C i get this error"scores.c:3:21: error: initializer element is not a compile-time constant".
I have looked in the https://stackoverflow.com/questions/6143107/compiler-error-initializer-element-is-not-a-compile-time-constant but I didn't get anything out of that from my program as my program i have already made number_scores a int variable and said get_int for the input for the user. If anybody wants the docs for get_int. Here it is https://manual.cs50.io/ the official documentation for get_int. Thanks in advance if anybody could help me solve this problem.
答案1
得分: 2
把初始化移到全局变量声明之外。在 main()
内应该给变量赋值。
你从未声明数组声明中使用的变量 N
。应该是 number_scores
。
条件 number_scores < 3
在你的循环中不会改变,因此会造成无限循环。应该是 i < number_scores
。
更好的做法是根本不使用全局变量,并将其作为 average
函数的参数。
英文:
Take the initialization out of the global variable declaration. Inside main()
you should assign the variable.
You never declared the variable N
used in the array declaration. That should be number_scores
.
The condition number_scores < 3
doens't change in your loop, so you have an infinite loop. It should be i < number_scores
.
#include <cs50.h>
#include <stdio.h>
int number_scores;
float average(int array[]);
int main(void)
{
number_scores = get_int("How many scores do you need: ");
int scores[number_scores];
for(int i = 0; i < number_scores; i++)
{
scores[i]= get_int("Scores: ");
}
printf("Average: %f\n", average(scores));
}
float average(int array[])
{
int sum = 0;
for (int i = 0; i < number_scores;i++)
{
sum = sum + array[i]
}
return sum / (float)number_scores;
}
Even better would be to not use a global variable at all, and make this a parameter of the average
function.
#include <cs50.h>
#include <stdio.h>
float average(int array[], int num_scores);
int main(void)
{
int number_scores = get_int("How many scores do you need: ");
int scores[number_scores];
for(int i = 0; i < number_scores; i++)
{
scores[i]= get_int("Scores: ");
}
printf("Average: %f\n", average(scores), number_scores);
}
float average(int array[], num_scores)
{
int sum = 0;
for (int i = 0; i < num_scores;i++)
{
sum = sum + array[i]
}
return sum / (float)num_scores;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论