将用户输入添加到未知大小的数组

拉法·马德

我仍然对编程很陌生,而我在C之前唯一的经验就是Javascript。我正在做《 CS50计算机科学概论》,在一次讲座中,有一个示例代码可以计算某些用户输入的平均值。看起来像这样:

#include <cs50.h>
#include <stdio.h>

const int TOTAL = 3;

float average(int length, int array[])
int main(void)
{
    int scores[TOTAL];
    for (int i = 0; i < TOTAL; i++)
    {
      scores[i] = get_int("Score: ");
    }

    printf("Average: %f\n", average(TOTAL, scores);
}

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

我要添加的功能是根据用户输入动态存储数组的大小,而不是具有一个变量(在这种情况下为TOTAL)。例如:我需要有一个循环,该循环总是向用户询问分数(而不是上面的代码的3倍),并且当用户键入零(0)时,循环中断并且数组的大小为由用户键入某个分数的次数定义。

这是我所做的:

int main(void)
{
    int score;
    // start count for size of array
    int count = - 1;

    do
    {
        score = get_int("Score: ");
        // add one to the count for each score
        count++;
    }
    while (score != 0);
    
    // now the size of the array is defined by how many times the user has typed.
    int scores[count];

    for (int i = 0; i < count; i++)
    {
        // how do I add each score to the array???
    }
}

我的问题是如何将用户键入的每个分数添加到数组中。提前谢谢!!!

用户名

关于:

int main(void)
{
    int score;
    // start count for size of array
    int count = - 1;

    do
    {
        score = get_int("Score: ");
        // add one to the count for each score
        count++;
    }
    while (score != 0);
    
    // now the size of the array is defined by how many times the user has typed.
    int scores[count];

    for (int i = 0; i < count; i++)
    {
        // how do I add each score to the array???
    }
}

这不会编译,并且包含几个逻辑错误

它缺少以下语句:#include <cs50.h>#include <stdio.h>

关于:

    int score;
    // start count for size of array
    int count = - 1;

    do
    {
        score = get_int("Score: ");
        // add one to the count for each score
        count++;
    }
    while (score != 0);

这仅定义了一个变量:score并且每次循环时都会覆盖该单个变量。同样,第一次通过循环时,计数器:count将增加为0,而不是1

在接下来的每个循环中,变量score将被覆盖(即用户输入的所有先前值都将丢失)

建议使用动态内存。注意:要使用动态内存,将需要头文件:stdlib.h用于原型:malloc()free()建议:

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
    
int main( void )
{
    // pointer to array of scores
    int * score = NULL;

    // start count for size of array
    int count = 0;

    while( 1 )
    {
        int score = get_int("Score: ");

        if( score != 0 )
        {  // then user has entered another score to put into array
            count++;
            int * temp = realloc( scores, count * sizeof( int ) )
            if( ! temp )
            { // realloc failed
                // output error info to `stderr`
                // note: `perror()` from `stdio.h`
                perror( "realloc failed" );

                // cleanup
                free( scores );

                // `exit()` and `EXIT_FAILURE` from `stdlib.h`
                exit( EXIT_FAILURE );
            }

            // implied else, 'realloc()' successful, so update the target pointer
            scores = temp;

            // insert the new score into the array
            scores[ count ] = score;
        }

        else
        { // user entered 0 so exit the loop
            break;
        }
    }

注意:退出程序之前,请传递scores给,free()这样就不会发生内存泄漏。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章