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

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

'" error: initializer element is not a compile-time constant

问题

I'll provide the translation of the code portion as you requested:

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. int number_scores = get_int("需要多少个分数: ");
  4. float average(int array[]);
  5. int main(void)
  6. {
  7. int number_scores = get_int("需要多少个分数: ");
  8. int scores[N];
  9. for(int i = 0; i < number_scores; i++)
  10. {
  11. scores[i] = get_int("分数: ");
  12. }
  13. printf("平均分: %f\n", average(scores));
  14. }
  15. float average(int array[])
  16. {
  17. int sum = 0;
  18. for (int i = 0; i < number_scores; i++)
  19. {
  20. sum = sum + array[i];
  21. }
  22. return sum / (float)number_scores;
  23. }

Please note that I've translated the comments and variable names, but the structure of the code remains the same.

英文:
  1. #include &lt;cs50.h&gt;
  2. #include &lt;stdio.h&gt;
  3. int number_scores = get_int(&quot;How many scores do you need: &quot;);
  4. float average(int array[]);
  5. int main(void)
  6. {
  7. int number_scores = get_int(&quot;How many scores do you need: &quot;);
  8. int scores[N];
  9. for(int i = 0; number_scores &lt; 3; i++)
  10. {
  11. scores[i]= get_int(&quot;Scores: &quot;);
  12. }
  13. printf(&quot;Average: %f\n&quot;, average(scores));
  14. }
  15. float average(int array[])
  16. {
  17. int sum = 0;
  18. for (int i = 0; i &lt; number_scores;i++)
  19. {
  20. sum = sum + array[i]
  21. }
  22. return sum / (float)number_scores;
  23. }

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.

  1. #include &lt;cs50.h&gt;
  2. #include &lt;stdio.h&gt;
  3. int number_scores;
  4. float average(int array[]);
  5. int main(void)
  6. {
  7. number_scores = get_int(&quot;How many scores do you need: &quot;);
  8. int scores[number_scores];
  9. for(int i = 0; i &lt; number_scores; i++)
  10. {
  11. scores[i]= get_int(&quot;Scores: &quot;);
  12. }
  13. printf(&quot;Average: %f\n&quot;, average(scores));
  14. }
  15. float average(int array[])
  16. {
  17. int sum = 0;
  18. for (int i = 0; i &lt; number_scores;i++)
  19. {
  20. sum = sum + array[i]
  21. }
  22. return sum / (float)number_scores;
  23. }

Even better would be to not use a global variable at all, and make this a parameter of the average function.

  1. #include &lt;cs50.h&gt;
  2. #include &lt;stdio.h&gt;
  3. float average(int array[], int num_scores);
  4. int main(void)
  5. {
  6. int number_scores = get_int(&quot;How many scores do you need: &quot;);
  7. int scores[number_scores];
  8. for(int i = 0; i &lt; number_scores; i++)
  9. {
  10. scores[i]= get_int(&quot;Scores: &quot;);
  11. }
  12. printf(&quot;Average: %f\n&quot;, average(scores), number_scores);
  13. }
  14. float average(int array[], num_scores)
  15. {
  16. int sum = 0;
  17. for (int i = 0; i &lt; num_scores;i++)
  18. {
  19. sum = sum + array[i]
  20. }
  21. return sum / (float)num_scores;
  22. }

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:

确定