My program is to allow user to input no. of students and their preferred sessions, then sort all of them by the session with most students.
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct student { //*** PART 2 STRUCTURE DEFINITION **********
char studName[50]; // Name of student
long long int studID; // Student ID of student
int session; // Prefered session of each student
};
struct session {
int sessionID;
int NoofStudinSession;
};
FILE * fPointer;
void main(void)
{
// Definitions
struct student *myStudents;
struct session *sessionsPtr;
int Noofsessions = 0;
int NoofStud = 0;
int i;
// User input for number of students and consultation sessions
printf("Enter Number of Students:\n");
scanf("%d", &NoofStud);
printf("Enter number of consultation session:\n");
scanf("%d", &Noofsessions);
// Dynamic allocation for struct
myStudents = (struct student *) malloc(sizeof(struct student) * NoofStud);
if (myStudents == NULL) {
printf("Error, out of memory.\n");
return;
}
sessionsPtr = (int *)malloc(sizeof(struct session) * Noofsessions);
if (sessionsPtr == NULL) {
printf("Error, out of memory.\n");
return;
}
// Initializing struct session before using
for (i = 0; i < Noofsessions; i++) {
(sessionsPtr + i)->sessionID = i + 1;
(sessionsPtr + i)->NoofStudinSession = 0;
}
// User input display
for (i = 0; i < NoofStud; i++) {
printf("Enter Student %d ID: \n", i + 1); // Prompt user to enter the ID of students
scanf("%d", &(myStudents + i)->studID); //**
printf("Enter Student %d Name: \n", i + 1); // Prompt user to enter the Student name
scanf(" %[^\t\n]s", &(myStudents + i)->studName); //**
printf("Enter Student %d's prefered Session(Number of session(s) is %d):\n", i + 1, Noofsessions); // Prefered Session
scanf("%d", &(myStudents + i)->session); //**
(sessionsPtr + ((myStudents + i)->session - 1))->NoofStudinSession++;
}
// Display summary of Part2 on screen
printf("Student ID\t Student Name\t\t Chose Which Session\n");
fprintf(fPointer, "Student ID\t Student Name\t\t Chose Which Session\n"); // Write into Text file
for (i = 0; i < NoofStud; ++i) {
printf("%d\t %15s\t\t\t %d\n", (myStudents + i)->studID, (myStudents + i)->studName, (myStudents + i)->session);
fprintf(fPointer, "%d\t %15s\t\t\t %d\n", (myStudents + i)->studID, (myStudents + i)->studName, (myStudents + i)->session);
}
// Sorting of sessions from MOST students to LEAST students
int total = 0, swapped, pos;
//comparing and sort values
while (1) {
pos = 0;
for (i = 0; i < Noofsessions - 1; ++i) {
if ((sessionsPtr + i)->NoofStudinSession < (sessionsPtr + i + 1)->NoofStudinSession) {
swapped = sessionsPtr;
sessionsPtr = sessionsPtr + 1;
swapped = sessionsPtr + 1;
pos = 1;
}
}
if (pos == 0) {
break;
}
}
// Display the Sorted Table
printf("\nSession\t\t\t No. of Students\n");
fprintf(fPointer, "\nSession\t\t\t No. of Students\n"); // Write into Text file
for (i = 0; i < Noofsessions; i++) {
printf("%d\t\t\t %d\n", (sessionsPtr + i)->sessionID, (sessionsPtr + i)->NoofStudinSession);
fprintf(fPointer, "%d\t\t\t %d\n", (sessionsPtr + i)->sessionID, (sessionsPtr + i)->NoofStudinSession); // Write into Text file
}
free(myStudents);
free(sessionsPtr);
system("pause");
return 0;
}
When the Noofsession
is to be smaller than 3, it runs smoothly.
However when it goes higher, this error occurs (HEAP error Invalid address specified to RtlValidateHeap
).
EDIT: to better explain the problem, here is the display right after crashing. Output Correct me if I'm wrong, so the output at the sorted tables are actually the address? instead of the sorted values.