运行此代码没有输出,是否存在无限循环?

时间:2019-01-16 08:46:25

标签: c

似乎我这里有一个无限循环,运行它似乎什么也没有发生。我不确定自己是否正在正确地启动该过程,因为什么也没发生。

目标-> 1.编写一个函数struct student * allocate(),该函数为十个学生分配内存并返回指针。 2.编写一个函数void generate(结构为学生*的学生),该函数为十个学生中的每一个生成随机ID和分数并存储 在一系列学生中。确保ID也是唯一的,且ID在1到10之间(包括两端),并且分数在0到100之间(两者都在 包括的)。要生成唯一ID,您可以使用蛮力随机/检查/重复(生成1到10之间的随机整数,然后 确认尚未将其用于学生证),或者可以使用Fisher Yates随机播放-(https://en.wikipedia.org/wiki/Fisher%E2%80% 93Yates_shuffle)。 3.编写一个函数void输出(面向学生*的学生),该输出打印所有学生的ID和分数。函数的输出不需要 进行排序。 4.编写一个功能无效的摘要(面向学生*的学生),打印出十个分数的最低分数,最高分数和平均分数 学生们。 5.编写一个函数void deallocate(构造学生*梭哈),释放分配给学生的内存。检查学生是否不是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;
}

在此处输入代码

2 个答案:

答案 0 :(得分:5)

好像您在此处增加了错误的变量

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

尝试使用j++代替k++

答案 1 :(得分:0)

在第158行中,您增加了错误的变量,导致循环无限期运行。

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