Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 数组长度超过所需长度_C - Fatal编程技术网

C 数组长度超过所需长度

C 数组长度超过所需长度,c,C,在下面的节目中, int * accepted_ids = (int *) malloc(sizeof(int)*N); double * accepted_scores = (double *)malloc(sizeof(double)*N); int * unaccepted_ids = (int *) malloc(sizeof(int)*N); double * unaccepted_scores = (double *) malloc(sizeof(d

在下面的节目中,

    int * accepted_ids = (int *) malloc(sizeof(int)*N);
    double * accepted_scores = (double *)malloc(sizeof(double)*N);

    int * unaccepted_ids = (int *) malloc(sizeof(int)*N);
    double * unaccepted_scores = (double *) malloc(sizeof(double)*N);
这些内存分配正在为它们中的每一个创建大小为
N
的数组,即使所需元素的数量远低于
N
的数量

由于该程序使用的是随机数生成器,因此我们无法事先知道每个随机数生成器需要多少内存

我怎样才能解决这个困境

(最多5分)
一维数组分数存储 N他们在高中时获得的大学候选人。元素索引 数组的第一个元素是这些候选对象的ID。这所大学接受 平均分数大于或等于的候选人的申请 到4.0

编写一个简短的程序,显示:

•接受的候选人名单及其ID号和平均分数。 •不合格候选人名单及其身份证号码和身份证明 平均分 •被接受和未被接受的候选人人数。•结果排序 按升序

应计算平均分数 从一个范围使用随机数生成器。总数 候选项应作为命令行参数传递给程序。 使您的程序对错误参数的输入敏感

不允许使用struct

#包括
#包括
#包括
#包括
//此函数测试是否可能
//是否将字符串转换为整数。
//
//需要此功能来检查
//如果键入,则输入参数
//C:\>myapp.exe abc
//你的程序将崩溃。
int是_整数(常量字符*s)
{
char*endptr;
int基数=10;//十进制
//尝试将s转换为整数
strtol(s和endptr,基数);
errno=0;
//如果该条件已满,
//也就是说,s不能被转换
//到一个整数。
如果(endptr==s | |*endptr!='\0')
{
//参数必须是整数值
返回0;//失败
}
if(errno==ERANGE)
{
//int参数超出范围
返回0;//失败
}
返回1;//成功
}
//转换时需要此函数
//将字符串转换为整数值。
整数字符串到整数(常量字符*s)
{
char*endptr;
int基数=10;//十进制
//将s转换为整数
返回strtol(s和endptr,基数);
}
//在M和N之间生成一个随机数。
//
//这个函数是必需的,因为rand()可以
//仅生成整数值。
双圆在m到n之间(双m,双n)
{
返回M+(rand()/(rand_MAX/(N-M));
}
//这是冒泡排序算法
//这是作为用户定义的函数实现的,
//因为,你必须用这个两次。
//首先是接受分数,
//然后是不合格的分数。
无效排序(整数*ID、双*分数、整数计数)
{
for(int i=0;i=接受_阈值)
{
接受的\u ID[接受的\u分数\u计数]=i;
接受分数[接受分数\计数]=分数;
接受的_分数_计数++;
}
//…否则,他们将被拒绝。
其他的
{
不合格_id[不合格_分数_计数]=i;
不合格分数[不合格分数计数]=分数;
不合格的_分数_计数++;
}
}
//对录取的学生进行分类
排序(已接受的\u ID、已接受的\u分数、已接受的\u分数\u计数);
//对未通过考试的学生进行分类
排序(未接受的_ID、未接受的_分数、未接受的_分数计数);
//打印录取学生
printf(“\n合格学生\n”);
打印(已接受的\u ID、已接受的\u分数、已接受的\u分数\u计数);
//打印不合格学生
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>

// This function tests whether it is possible 
// to convert a string into integer or not.
//
// This function is needed to check the 
// input argument otherwise if you type 
//      C:\>myapp.exe abc
// your program will crash.
int is_integer(const char * s)
{
    char * endptr;
    int radix = 10;//decimal number system

    // try to convert s to integer
    strtol(s, &endptr, radix);

    errno = 0;
    // if this conditions are fullfilled,
    // that means, s can't be converted 
    // to an integer.
    if (endptr == s || *endptr != '\0')
    {
        // argument must be an integer value        
        return 0; // failure
    }
    if (errno == ERANGE)
    {
        // int argument out of range        
        return 0; // failure
    }

    return 1; //success
}

// This function is needed to convert
// a string to an integer value.
int string_to_integer(const char * s)
{
    char * endptr;
    int radix = 10;//decimal number system

    // convert s to integer
    return strtol(s, &endptr, radix);
}

// Generte a random number between M and N.
//
// This function is needed coz rand() can 
// generate only integer values.
double round_between_m_to_n(double M, double N)
{
    return M + (rand() / (RAND_MAX / (N - M)));
}

// This is Bubble sort algorithm
// This is implemented as a user-defined function, 
// coz, you have to use this twice.
// First for accepted scores, 
// then for unaccepted scores.
void sort(int * ids, double * scores, int count)
{
    for (int i = 0; i < count; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (scores[i] < scores[j])
            {
                // Swap scores
                double temp = scores[i];
                scores[i] = scores[j];
                scores[j] = temp;

                // Swap ids
                int temp2 = ids[i];
                ids[i] = ids[j];
                ids[j] = temp2;
            }
        }
    }
}

// This function is to print ids and scores
// as a table.
// This is implemented as a user-defined function, 
// coz, you have to use this twice.
// First for accepted scores, 
// then for unaccepted scores.
void print(int * ids, double * scores, int count)
{

    printf("id\tavg_score\n");
    printf("-------------------\n");
    for (int i = 0; i < count; i++)
    {
        printf("%i\t%.1f\n", ids[i], scores[i]);
    }
}

int main(int argc, char ** argv)
{
    // Program can proceed only if 
    // the # of arguments is exactly 2. 
    // The 1st arg is always app-name.
    if (argc != 2)
    {
        printf("insufficient argument\n");
        return EXIT_FAILURE;
    }

    int N = 0;
    int accepted_scores_count = 0;
    int unaccepted_scores_count = 0;
    double acceptance_threshhold = 4.0;

    if (!is_integer(argv[1]))
    {
        printf("incorrect argument type\n");
        return EXIT_FAILURE;
    }
    else
    {
        N = string_to_integer(argv[1]);
        printf("Total %d students\n", N);
    }

    // Pair of variables are needed to
    // keep track of student-ids.
    // Otherwise, you can't tell what id a 
    // student has when data are sorted.
    int * accepted_ids = (int *)malloc(sizeof(int)*N);
    double * accepted_scores = (double *)malloc(sizeof(double)*N);

    int * unaccepted_ids = (int *)malloc(sizeof(int)*N);
    double * unaccepted_scores = (double *)malloc(sizeof(double)*N);

    //Initialize random seed.
    //If you don't use this, rand() will generate
    //same values each time you run the program.
    srand(time(NULL));

    // Simultaneously generate scores, ids, and
    // store them is sepaerate arrays.
    for (int i = 0; i < N; i++)
    {
        int id = i;
        double score = round_between_m_to_n(2, 6);

        // if the score is greater than or
        // equal to 4.0...
        if (score >= acceptance_threshhold)
        {
            accepted_ids[accepted_scores_count] = i;
            accepted_scores[accepted_scores_count] = score;

            accepted_scores_count++;
        }
        // ... otherwise they are unaccepted.
        else
        {
            unaccepted_ids[unaccepted_scores_count] = i;
            unaccepted_scores[unaccepted_scores_count] = score;

            unaccepted_scores_count++;
        }
    }

    // sort accepted students
    sort(accepted_ids, accepted_scores, accepted_scores_count);
    // sort unaccpeted students
    sort(unaccepted_ids, unaccepted_scores, unaccepted_scores_count);

    // print accepted students
    printf("\naccepted  students\n");
    print(accepted_ids, accepted_scores, accepted_scores_count);

    // print unaccepted students
    printf("\nunaccepted  students\n");
    print(unaccepted_ids, unaccepted_scores, unaccepted_scores_count);

    printf("\nEnd of program.\n");

    free(accepted_ids);
    free(accepted_scores);
    free(unaccepted_ids);
    free(unaccepted_scores);


    return EXIT_SUCCESS;
}
    int * all_ids = (int *)malloc(sizeof(int)*N);
    double * all_scores = (double *)malloc(sizeof(int)*N);
    for (int i = 0; i < N; i++)
    {
        int id = i;
        double score = round_between_m_to_n(2, 6);

        all_ids[i] = id;
        all_scores[i] = score;

        // if the score is greater than or
        // equal to 4.0...
        if (score >= acceptance_threshhold)
        {
            accepted_scores_count++;
        }
        // ... otherwise they are unaccepted.
        else
        {
            unaccepted_scores_count++;
        }
    }
    int * accepted_ids = (int *)malloc(sizeof(int) * accepted_scores_count);
    double * accepted_scores = (double *)malloc(sizeof(double) * accepted_scores_count);

    int * unaccepted_ids = (int *)malloc(sizeof(int) * unaccepted_scores_count);
    double * unaccepted_scores = (double *)malloc(sizeof(double) * unaccepted_scores_count);
    for (int i = 0, j = 0; (i+j) < N;)
    {
        int id = all_ids[i+j];
        double score = all_scores[i+j];

        // if the score is greater than or
        // equal to 4.0...
        if (score >= acceptance_threshhold)
        {
            accepted_ids[i] = id;
            accepted_scores[i] = score;

            i++;
        }
        // ... otherwise they are unaccepted.
        else
        {
            unaccepted_ids[j] = id;
            unaccepted_scores[j] = score;

            j++;
        }
    }