arrange course names in alphabetical order

I'm trying to sort student ID's and their courses in numerical and alphabetical orders, but how?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void print_student_courses(struct student student_list[],int *stu_count)
{


	int stu_id, i, stu_index;
	struct class1 *p1;
	cout<<"Please enter student ID to print courses: ";
	cin>>stu_id;
	cout<<"\nThe student with ID "<<stu_id<< " takes the following courses:  \n\n";
	cout<<"Course Name   Section No.   No. of Credit(s) \n";

	for (i=0; i< *stu_count; i++)
		if (stu_id == student_list[i].id) stu_index = i;
	p1 = student_list[stu_index].p_class_from_student;
	while (p1 != NULL) {
	cout << p1->course_name << "            " << p1->section+1 << "              " << p1->credit<<"\n";
		p1 = p1->p_class;
	}
		
}
closed account (28poGNh0)
Can You give us the whole code?
Is this for "learning to invent a wheel", or is the use of standard library allowed?
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

#include <iostream> //cin&cout
//c standard library
#include<cstdio> //printf, scanf
#include<cstdlib> //free, malloc
#include<cmath> //sqrt
#include<conio.h>
#include<ctype.h>
using namespace std;

typedef struct class1
{
char course_name[7];
int section;
int credit;
struct class1 *p_class;
};
typedef struct student
{
int id;
struct class1 *p_class_from_student;
};

void add_student(struct student student_list[],int *stu_count);
void add_course(struct student student_list[],int *stu_count);
void remove_student(struct student student_list[],int *stu_count);
void remove_course(struct student student_list[],int *stu_count);
void print_student_courses(struct student student_list[],int *stu_count);
void print_all_students(struct student student_list[],int *stu_count);
void print_all_courses_all_students(struct student student_list[],int *stu_count);

int main(void){

int stu_count = 0, stu_index=0, i1, i2, choice, end = 0;
struct student student_list[100];
do {
cout<< "\n-----------------------------------------------------------------\n";
cout<< "1: Add a student to the data base\n";
cout<< "2: Add a course to a student in the data base\n";
cout<<"3: Find and remove a student from the data base\n";
cout<<"4: Find and remove a course of a student in the data base\n";
cout<<"5: Print out the courses of a student in the data base\n";
cout<<"6: Print out all the students in the data base\n";
cout<<"7: Print out all students and all their courses in the data base\n\n";
cout<<"Enter 1-7 to select function to preform or enter 0 to quit: ";
if ((cin>>choice) == 0) {
fflush(stdin);
cout<<"Invalid choice.\n";
continue;
}
cout<<"\n";


switch (choice) {

case 0:
end = 1;
break;

case 1:
add_student(student_list,&stu_count);
break;

case 2:
add_course(student_list,&stu_count);
break;

case 3:
remove_student(student_list,&stu_count);
break;
case 4:
remove_course(student_list,&stu_count);
break;

case 5:
print_student_courses(student_list, &stu_count);
break;

case 6:
print_all_students(student_list, &stu_count);
break;

case 7:
print_all_courses_all_students(student_list,&stu_count);
break;

default:
cout<<"Invalid choice\n";
char ignore;
ungetc(ignore, stdin);//consume non-numeric chars from buffer
}
} while (end != 1);
return(0);
}
void add_student(struct student student_list[],int *stu_count)
{


int input_id, found = 0, i, space_found =0, stu_index, stu_size;
cout<<"Please enter your student ID: ";
cin>>input_id;
if (*stu_count == 0) stu_size = 0;
else stu_size = *stu_count -1;
for (i=0; i <=stu_size; i++) {
if (student_list[i].id == input_id)
{
found = 1;
stu_index = i;
cout<<"The student already exists in the record\n";
}
}
if (found == 0) {
for(i=0; i<= stu_size; i++) {
if (student_list[i].id == -1) space_found = 1;
stu_index = i;
}
if (space_found == 0) {
stu_index = *stu_count;
}
student_list[*stu_count].id = input_id;
student_list[*stu_count].p_class_from_student = NULL;
*stu_count = *stu_count +1;

}
}
void add_course(struct student student_list[],int *stu_count)
{

int stu_id, i, found = 0, i1, i2, stu_index;
int sec_no, cre_no, input_id;
struct class1 *p1, *c1;

cout<<"Please enter your student ID to add a course: ";
cin>>stu_id;

for (i=0; i <=*stu_count-1; i++) {
if (student_list[i].id == stu_id) {
found = 1;
stu_index = i;
}
}

if(!found){
cout<<"Student"<<stu_id<< "does not exist";
return;
}

c1 = new class1;//(struct class1 *)malloc(sizeof (struct class1));
cout<<"Please enter course name: ";
cin>>c1 ->course_name;
cout<<"Please enter section number: ";
cin>> c1->section;
cout<<"Please enter credit number: ";
cin>>c1->credit;
p1 = student_list[stu_index].p_class_from_student;
student_list[stu_index].p_class_from_student = c1;
c1->p_class = p1;
}
void remove_student(struct student student_list[],int *stu_count)
{


int stu_id, i, stu_index;
struct class1 *p1;
cout<<"Please enter student ID to remove the student: ";
cin>>stu_id;
for (i=0; i< *stu_count; i++)
if (stu_id == student_list[i].id){
stu_index = i;
break;
}
if(i == *stu_count){
cout<< "Student "<<stu_id<< "does not exist.";
return;
}
while (student_list[stu_index].p_class_from_student != NULL) {
p1 = student_list[stu_index].p_class_from_student;
student_list[stu_index].p_class_from_student = student_list[stu_index].p_class_from_student ->p_class;
delete p1;
}
student_list[stu_index].id = -1;
}

void remove_course(struct student student_list[],int *stu_count)
{


int stu_id,i,stu_index;
char stu_course[7];
struct class1 *p1, *p2;
cout<<"Please enter the ID of the student you want to remove course: ";
cin>>stu_id;

for (i=0; i< *stu_count; i++)
if (stu_id == student_list[i].id) {
stu_index = i;
break;
}

if(i == *stu_count){
cout<< "Student "<<stu_id<< "does not exist.";
return;
}

cout<<"Please enter the course of the student you want to remove: ";
cin>>stu_course;

p1 = student_list[stu_index].p_class_from_student;
if(p1 != NULL){
if (strcmp(p1->course_name, stu_course) == 0) {
student_list[stu_index].p_class_from_student = p1->p_class;
delete p1;
}
else {
while (p1 != NULL && strcmp(p1->course_name, stu_course) != 0) {
p2 = p1;
p1 = p1 ->p_class;
}
if(p1 != NULL){ //if a class matched
p2->p_class = p1->p_class;
delete p1;
}else{//no class match
cout<<"Course does not exist.";
}
}
}
}

void print_student_courses(struct student student_list[],int *stu_count)
{
char course_name[7];
int temp=0;
int stu_id, i, stu_index;
struct class1 *p1;
cout<<"Please enter student ID to print courses: ";
cin>>stu_id;
cout<<"\nThe student with ID "<<stu_id<< " takes the following courses: \n\n";
cout<<"Course Name Section No. No. of Credit(s) \n";

for (i=0; i< *stu_count; i++)
{

for (int j = 0; j < 6; j++)
{
if (course_name[j] > course_name[j+1])
{
temp = course_name[j];
course_name[j] = course_name[j+1];
course_name[j+1] = temp;
}
}/*End inner for loop*/
if (stu_id == student_list[i].id) stu_index = i;
p1 = student_list[stu_index].p_class_from_student;
while (p1 != NULL) {
{
printf("%s %d %d \n", p1->course_name, p1->section, p1->credit);
p1 = p1->p_class;
}

}/*End outer for loop*/
for ( i = 0; i <*stu_count; ++i )
cout << p1->course_name << " " << p1->section+1 << " " << p1->credit<<"\n";
p1 = p1->p_class;

}

}

void print_all_students(struct student student_list[],int *stu_count)
{


int i;
cout<<"\nThe IDs of students in the data base are listed below. \n\n";

for ( int pass = 0; pass < *stu_count - 1 ; ++pass )

for ( int j = 0; j < *stu_count - 1 - pass; ++j )
{
//Perform string compare and return value store as result
int result = strcmp (student_list[j], student_list[j+1]);
//If value is less than 0 then perform swap function to rearrange
if (result > 0)
swap ( student_list[j] , student_list[j+1] );

}//for
if (student_list[i].id != -1)
cout<<student_list[i].id<<"\n";//cout<< "Student "<<stu_id<< "does not exist.";
}
void print_all_courses_all_students(struct student student_list[],int *stu_count)
{


int i;
struct class1 *p1;
for (i=0; i<*stu_count; i++)
if (student_list[i].id != -1) {
cout<<"\nThe course(s) of student with ID %d: \n\n", student_list[i].id;
cout<<"Course Name Section No. No. of Credit(s) \n";
p1 = student_list[i].p_class_from_student;
while (p1 != NULL) {
cout << p1->course_name << " " << p1->section << " " << p1->credit<<"\n";
p1 = p1->p_class;
}
}
}
Last edited on
Topic archived. No new replies allowed.