首页 > 解决方案 > 运行此代码没有输出,是否存在无限循环?

问题描述

似乎我在这里有一个无限循环,当我运行它时似乎什么都没有发生。我不确定我是否正确地开始了这个过程,因为什么都没有发生。

Objective-> 1. 写一个函数 struct student* allocate() 为十个学生分配内存并返回指针。2. 编写一个函数 void generate (struct student* students),为十个学生中的每一个生成随机 ID 和分数,并将它们存储在学生数组中。确保 ID 也是唯一的,并且介于 1 和 10(包括两者)之间,并且分数介于 0 和 100(包括两者)之间。要生成唯一 ID,您可以使用暴力随机/检查/重复(生成 1-10 之间的随机整数,然后确认它尚未用于学生 ID),或者您可以使用 Fisher Yates shuffle -( https://en.wikipedia.org/wiki/Fisher%E2%80% 93Yates_shuffle)。3. 编写一个函数 void output (struct student* students),打印所有学生的 ID 和分数。函数的输出不需要排序。4. 编写一个函数void summary(struct student* students),打印十个学生的最低分、最高分和平均分。5. 编写一个函数 void deallocate (struct student* stud),释放分配给学生的内存。在尝试释放学生之前检查学生是否不为 NULL (NULL == 0)。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

struct student{
    int id;
    int score;
};

struct student* allocate(){
     /*Allocate memory for ten students*/
     struct student * students = malloc(10* sizeof(struct student));
    return students;
     /*Return the pointer*/
}

void generate(struct student* students){
     /*Generate random and unique IDs and random scores for ten students, 
IDs being between 1 and 10, scores between 0 and 100*/
    int j;
    int k;
    int i;
    int listScore[100];
    int listId[10];

    for(k=1; k<=10;k++)
        {listId[k]= -1;}    
    for(j=1; j<=100;k++)
        {listScore[j]= -1;}
    for(i=0;i<10;i++)
    {
     int y = i+(rand() % (10-i));
     int temp = listId[i];
     listId[i] = listId[j];
     listId[j]=temp;
    }
    for(i=0;i<10;i++)
    {
        students[i].id = listId[i];  
    }
    for(i=0;i<100;i++)
    {
     int y = i+(rand() % (100-i));
     int temp = listScore[i];
     listScore[i] = listScore[j];
     listScore[j]=temp;
    }
    for(i=0;i<10;i++)
    {
        students[i].score = listScore[i];  
    }
}

void output(struct student* students){
    int i;
    printf("ID1 Score%d\n",students[1].score);
    for(i=2; i<=10; i++ )
    {
        printf("ID%d score%d\n", i, students[i].score);
    }
    /*Output information about the ten students in the format:
              ID1 Score1
              ID2 score2
              ID3 score3
              ...
              ID10 score10*/
}

void summary(struct student* students){
    int i;
    int min = students[1].score;
    int max= students[1].score;
    int sum = 0;
    for(i=2;i<=10;i++)
    {
        if(students[i].score < min)
        {
            min = students[i].score;
        }
    }
    for(i=2;i<=10;i++)
    {
        if(students[i].score >max)
        {
            max = students[i].score;
        }
    }
    for(i=1; i<=10; i++)
    {
        sum += students[i].score;
    }
    sum /=10;
    printf("Minimum score = %d\nMaximum score = %d\nAverage score = %d",min,max,sum);
    /*Compute and print the minimum, maximum and average scores of the 
ten students*/

}

void deallocate(struct student* stud){
     /*Deallocate memory from stud*/
    if(stud != NULL)
    {
        free(stud);
    }
}
int main(){
    srand(time(0));
    struct student* stud = NULL;

    /*Call allocate*/
    stud = allocate();
    /*Call generate*/
    generate(stud);
    /*Call output*/
    output(stud);
    /*Call summary*/
    summary(stud);
    /*Call deallocate*/
    deallocate(stud);
    return 0;
}

/* CS261- Assignment 1 - Q.1*/
/* Name:
 * Date:
 * Solution description:
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

struct student{
    int id;
    int score;
};

struct student* allocate(){
     /*Allocate memory for ten students*/
     struct student * students = malloc(10* sizeof(struct student));
    return students;
     /*Return the pointer*/
}

void generate(struct student* students){
     /*Generate random and unique IDs and random scores for ten students, 
IDs being between 1 and 10, scores between 0 and 100*/
    int j;
    int k;
    int i;
    int listScore[100];
    int listId[10];

    for(k=1; k<=10;k++)
        {listId[k]= -1;}    
    for(j=1; j<=100;k++)
        {listScore[j]= -1;}
    for(i=0;i<10;i++)
    {
     int y = i+(rand() % (10-i));
     int temp = listId[i];
     listId[i] = listId[j];
     listId[j]=temp;
    }
    for(i=0;i<10;i++)
    {
        students[i].id = listId[i];  
    }
    for(i=0;i<100;i++)
    {
     int y = i+(rand() % (100-i));
     int temp = listScore[i];
     listScore[i] = listScore[j];
     listScore[j]=temp;
    }
    for(i=0;i<10;i++)
    {
        students[i].score = listScore[i];  
    }
}

void output(struct student* students){
    int i;
    printf("ID1 Score%d\n",students[1].score);
    for(i=2; i<=10; i++ )
    {
        printf("ID%d score%d\n", i, students[i].score);
    }
    /*Output information about the ten students in the format:
              ID1 Score1
              ID2 score2
              ID3 score3
              ...
              ID10 score10*/
}

void summary(struct student* students){
    int i;
    int min = students[1].score;
    int max= students[1].score;
    int sum = 0;
    for(i=2;i<=10;i++)
    {
        if(students[i].score < min)
        {
            min = students[i].score;
        }
    }
    for(i=2;i<=10;i++)
    {
        if(students[i].score >max)
        {
            max = students[i].score;
        }
    }
    for(i=1; i<=10; i++)
    {
        sum += students[i].score;
    }
    sum /=10;
    printf("Minimum score = %d\nMaximum score = %d\nAverage score = %d",min,max,sum);
    /*Compute and print the minimum, maximum and average scores of the 
ten students*/

}

void deallocate(struct student* stud){
     /*Deallocate memory from stud*/
    if(stud != NULL)
    {
        free(stud);
    }
}
int main(){
    srand(time(0));
    struct student* stud = NULL;

    /*Call allocate*/
    stud = allocate();
    /*Call generate*/
    generate(stud);
    /*Call output*/
    output(stud);
    /*Call summary*/
    summary(stud);
    /*Call deallocate*/
    deallocate(stud);
    return 0;
}

在此处输入代码

标签: c

解决方案


看起来你在这里增加了错误的变量

for(j=1; j <= 100; k++) {
    listScore[j]= -1;
}

尝试使用j++而不是k++.


推荐阅读