“‘error: initializer element is not a compile-time constant”

huangapple go评论54阅读模式
英文:

'" 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 &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
int number_scores = get_int(&quot;How many scores do you need: &quot;);
float average(int array[]);
int main(void)
{
    int number_scores = get_int(&quot;How many scores do you need: &quot;);
    int scores[N];
    for(int i = 0; number_scores &lt; 3; i++)
    {
        scores[i]= get_int(&quot;Scores: &quot;);
    }
    printf(&quot;Average: %f\n&quot;, average(scores));
}
float average(int array[])
{
    int sum = 0;
    for (int i = 0; i &lt; 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 &lt; 3 在你的循环中不会改变,因此会造成无限循环。应该是 i &lt; 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 &lt; 3 doens't change in your loop, so you have an infinite loop. It should be i &lt; number_scores.

#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
int number_scores;
float average(int array[]);
int main(void)
{
    number_scores = get_int(&quot;How many scores do you need: &quot;);
    int scores[number_scores];
    for(int i = 0; i &lt; number_scores; i++)
    {
        scores[i]= get_int(&quot;Scores: &quot;);
    }
    printf(&quot;Average: %f\n&quot;, average(scores));
}
float average(int array[])
{
    int sum = 0;
    for (int i = 0; i &lt; 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 &lt;cs50.h&gt;
#include &lt;stdio.h&gt;

float average(int array[], int num_scores);

int main(void)
{
    int number_scores = get_int(&quot;How many scores do you need: &quot;);
    int scores[number_scores];
    for(int i = 0; i &lt; number_scores; i++)
    {
        scores[i]= get_int(&quot;Scores: &quot;);
    }
    printf(&quot;Average: %f\n&quot;, average(scores), number_scores);
}
float average(int array[], num_scores)
{
    int sum = 0;
    for (int i = 0; i &lt; num_scores;i++)
    {
        sum = sum + array[i]
    }
    return sum / (float)num_scores;
}

huangapple
  • 本文由 发表于 2023年4月4日 04:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定