我如何让它理解aos等于amount,以便用户可以在c中输入数组的数量?

fsi0uk1n  于 2023-10-16  发布在  其他
关注(0)|答案(2)|浏览(92)
#include <cs50.h>
#include <stdio.h>

int amount();
float average(int array[]);

int main(void)

//me begging to get the code to understand that aos is equal to amount();
int aos = amount();
{
    //ask for scores and print them
    int scores[aos];
    for (int i=0;i<aos;i++)
    {
        scores[i] = get_int("score: ");
    }
    printf("average: %f\n", average(scores));
}

//get amount of scores
int amount()

{
    int amt;
    do
    {
        amt = get_int("how many scores: ");
    }
    while (amt < 0);
    return amt;
}

//get amount of arrays and average
float average(int array[])

{
    int sum = 0;
    for (int i = 0; i<aos; i++)
    {
        sum += array[i];
    }
    return sum / (float) aos;
}

特别是int aos = amount();让我头疼,因为无论我把它放在哪里,它通常都会给我一条消息“初始化器元素不是编译时常量”,我不知道如何修复它。
我试着把这一行放在不同的地方,并试图以一种不会激怒编译器的方式来构建它。在这一点上,我不确定它是否也可能只是一个形式错误,我错过了。无论如何,我都是新的编码,并开始学习arras abour 3小时前,所以我所有的选择

bzzcjhmw

bzzcjhmw1#

您的问题是,根据C语法,语句int aos = amount();不正确。只有进一步滚动到代码中,才能清楚地看到您希望aos可用于函数average
下面是你的代码修改和注解:

#include <stdio.h> // better to list most generic ahead of others
#include <cs50.h>

// Defining functions ahead of their use obviates the need for function prototypes.
// This eliminates an extra point of maintenance
float average( int array[], int aos ) // pass all the required values as arguments
{
    int sum = 0;
    for (int i = 0; i<aos; i++)
    {
        sum += array[i];
    }
    return sum / (float) aos; // or sum could be float the whole time
}

int amount( void ) // be specific about arguments
{
    int amt;
    do
    {
        amt = get_int("how many scores: ");
    }
    while (amt < 1); // Changed from 0 to 1. Dividing by zero is undefined!

    return amt;
}

// Notice the use of additional whitespace below to aid legibility.
int main(void)
{
    int nScores = amount(); // changed variable name here

    int scores[ nScores ];
    for ( int i = 0; i < nScores; i++ )
    {
        scores[ i ] = get_int("score: ");
    }
    printf( "average: %f\n", average( scores, nScores ) );

    return 0; // optional, but I prefer this be present.
}

此格式表示使用的几种 * 常规 * 样式之一。不鼓励使用个人差异(例如在函数体前面多加一个空行),特别是当其他人会阅读您的代码时。
你可以继续写关于使用size_t变量或const参数声明的文章,但现在已经足够了。

g52tjvyc

g52tjvyc2#

int main(void)

    // me begging to get the code to understand that aos is
    // equal to amount();
    int aos = amount();
{

这个{应该在main的开头,

int main(void)
{
    // me begging to get the code to understand that aos is
    // equal to amount();
    int aos = amount();
    // ask for scores and print them
    int scores[aos];
    for (int i = 0; i < aos; i++)
    {
        scores[i] = get_int("score: ");
    }
    printf("average: %f\n", average(scores));
}
  • “初始化器元素不是编译时常量”*

指的是这条线

int scores[aos];

编译器告诉你aos,你用来初始化scores数组大小的值,不是编译时常量。”在这里没有惊喜。不是**:它是函数在运行时返回的值。你必须在写程序时提供一个值,或者在程序运行时,一旦你知道这个值就分配数组。有一种东西叫做VLA,但你不应该指望它的存在或在你的编译器中实现。
您可以**[1]估计scores[2]**在运行时分配数组。

[1]只需使用最大大小并调整aos

#include <stdio.h>
int   amount();
float average(int array[], int size);

int main(void)
{
    // me begging to get the code to understand that aos is
    // equal to amount();
    int aos = amount();
    // now the code understanding that you can have at most
    // 30 scores...
    if (aos > 30) aos = 30;
    // sure, you could write
    // aos = (aos > 30) ? 30 : aos;
    int scores[30];
    for (int i = 0; i < aos; i++)
        scores[i] = get_int("score: ");
    // now you tell average the # of scores
    printf("average: %f\n", average(scores,aos));
}

// get amount of scores
int amount()

{
    int amt;
    do {
        amt = get_int("how many scores: ");
    } while (amt < 0);
    return amt;
}

// get amount of arrays and average
float average(int array[], int aos)
{
    int sum = 0;
    for (int i = 0; i < aos; i++) { sum += array[i]; }
    return sum / (float)aos;
}

[2]仅在aos已知时创建scores[]

int main(void)
{
    int aos = amount();
    // now that you know the size, tell it
    // to the compiler
    int* scores = malloc(aos * sizeof(aos));
    // aos can even be negative as you used `int`
    // so malloc can fail
    if (scores == NULL) return -1; // as an error
    for (int i = 0; i < aos; i++)
        scores[i] = i;
    //scores[i] = get_int("score: ");
    // now you tell average the # of scores
    printf("average: %f\n", average(scores, aos));
    // now socres[] has no use: you must free the memory
    free(scores);
    // this can save you from using an adress of a thing
    // that does not exist anymore
    scores = NULL; // free memory, invalidate pointer
    return 0;
}

总之

int aos = amount();
    int* scores = malloc(aos * sizeof(aos));

通过这种方式,只要知道aos,就可以调用malloc并获得一个指向所需大小的区域的指针。
然后将average更改为

float average(int* array, int size);

数组的地址。
在使用scores之后,调用free来释放分配的内存。将指针设置为NULL是一个好习惯。不是你所有的程序都会有20行,scores指向的内存在你调用free后就不再有效了,所以它可以保存你从几个bug中.

相关问题