Please have the code run as shown in the sample run. Use as many functions as possible and make the main function as clean as possible. Use C for language
Code Screenshots:
Sample Output:
Code To Copy:
//Include the
//required header files.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//Define the structure course.
struct course
{
int id;
int credit;
char name[7];
};
//Define the structure student.
struct student
{
int id;
char name[100];
int no_course;
struct course crs[4];
};
//Define the required functions.
struct course create_course(int crn, char* name, int credit)
{
struct course new_course;
new_course.id = crn;
strcpy(new_course.name, name);
new_course.credit = credit;
return new_course;
}
int print_menu()
{
int choice = 0;
printf("Choose from the following options:\n");
printf("\t1- Add a new student\n");
printf("\t2- Add/Delete a course\n");
printf("\t3- Search for a student\n");
printf("\t4- Print fee invoice\n");
printf("\t0- Exit program\n");
printf("\nEnter your selection: ");
scanf("%d", &choice);
return choice;
}
void disp_course(struct course c)
{
printf("%d\t %s\t %d\n", c.id, c.name, c.credit);
}
void print_student(struct student x)
{
printf("The details of the students are as follows:\n");
printf("Name: %s\n", x.name);
printf("\nCourses: ");
int i=0;
for(i=0;i<x.no_course;i++)
{
disp_course(x.crs[i]);
}
}
void print_invoice(struct student x)
{
float curr = 0;
float total = 0;
printf("\tVALENCE COMMUNITY COLLEGE\n");
printf("\tORLANDO FL 10101\n");
printf("\t-------------------\n\n");
printf("\tFee Invoice prepared for: \n");
printf("\t%d-%s\n\n",x.id,x.name);
printf("\t1 Credit hour = $ 120.25\n\n");
printf("\tCRN\tCR_PREFIX\tCR_HOURS\n");
int i=0;
for(i=0;i<x.no_course;i++)
{
curr = x.crs[i].credit*(120.25);
printf("\t%d\t %s\t %d\t $ %.2f\n", x.crs[i].id, x.crs[i].name, x.crs[i].credit, curr);
total = total + curr;
}
printf("\t\tHealth & id fees\t $ 35.00\n");
total = total + 35;
printf("\t---------------------------------------\n");
printf("\t\tTotal Payments\t $ %.2f\n", total);
}
void search_student(struct student x[], int size)
{
int id;
int flag =0;
printf("Enter the id of the student: ");
scanf("%d", &id);
int i;
for(i=0;i<size;i++)
{
if(x[i].id == id)
{
print_student(x[i]);
flag =1;
break;
}
}
if(flag == 0)
{
printf("No Student Found!\n");
}
}
void check_invoice(struct student x)
{
char ch = 'n';
printf("\n Do you wish to print the new invoice(Y/N): ");
scanf("%c", &ch);
if(ch == 'Y' || ch == 'y')
{
print_invoice(x);
}
}
void add_del_course(struct student all_students[], int size, struct course all_courses[], int course_size)
{
printf("\n%d\n", size);
int sid;
printf("\nEnter the student's id: ");
scanf("%d", &sid);
int i=0;
for(i=0;i<size;i++)
{
if(all_students[i].id == sid)
{
break;
}
}
int k;
for( k = 0; k<all_students[i].no_course;k++)
{
disp_course(all_students[i].crs[k]);
}
int n_c = all_students[i].no_course;
char choice = 'C';
scanf("%c",&choice);
printf("\nChoose from:\n");
printf("A. Add a new course for [%s] \n",all_students[i].name);
printf("D. Delete a course from [%s]'s schedule\n",all_students[i].name);
printf("C. Cancel operation\n");
choice = getchar();
int c_id;
int l =0;
int j=0;
char ac;
switch(choice)
{
case 'A':
case 'a':
if(all_students[i].no_course < 4)
{
printf("\nEnter the course id: ");
scanf("%d", &c_id);
for(l=0; l<course_size; l++)
{
if(all_courses[l].id == c_id)
{
all_students[i].crs[n_c] = all_courses[l];
all_students[i].no_course++;
break;
}
}
scanf("%c", &ac);
check_invoice(all_students[i]);
}
else
{
printf("\nNo more courses can be added.\n");
}
break;
case 'D':
case 'd':
printf("Enter the course id: ");
scanf("%d", &c_id);
getchar();
for(l=0;l<n_c;l++)
{
if(all_students[i].crs[l].id == c_id)
{
for(j = l;j<n_c-1;j++)
{
all_students[i].crs[j] = all_students[i].crs[j+1];
}
break;
}
}
all_students[i].no_course--;
printf("\nDeleted Sucessfully.\n\n");
scanf("%c", &ac);
check_invoice(all_students[i]);
break;
case 'C':
case 'c':
break;
default:
printf("\nInvalid choice.\n");
}
}
struct student add_student(struct student all_students[], int size, struct course all_courses[], int course_size)
{
struct student* new_student = (struct student*)malloc(sizeof(struct student));
char c;
int i ;
int flag = 1;
int no_courses = 0;
printf("Enter the student's id: ");
scanf("%d", &new_student->id);
while(flag == 1 && size != 0)
{
int j =0;
for(j =0; j< size; j++)
{
if(all_students[j].id == new_student->id)
{
printf("The id already exists.");
printf("Enter the student's id: ");
scanf("%d", &new_student->id);
flag = 1;
break;
}
flag = 0;
}
}
scanf("%c",&c);
printf("Enter student's name: ");
c = getchar();
i=0;
while(c != '\n')
{
new_student->name[i] = c;
c = getchar();
i++;
}
new_student->name[i] = '\0';
printf("Enter how many courses (%s) is taking (upto 4 courses)\n\t?", new_student->name);
scanf("%d", &no_courses);
while(no_courses > 4 || no_courses < 0)
{
printf("\nPlease enter a valid number (0-4)\n\t");
scanf("%d", &no_courses);
}
new_student->no_course = no_courses;
int c_flag = 0;
int c_ids[4];
while(c_flag == 0)
{
i=0;
printf("\nEnter the %d course numbers\n", no_courses);
while(i < no_courses)
{
scanf("%d", &(c_ids[i]));
i++;
}
int p;
for(p=0;p<no_courses;p++)
{
c_flag = 0;
int q;
for(q=0;q<course_size;q++)
{
if(c_ids[p] == all_courses[q].id)
{
new_student->crs[p] = all_courses[q];
c_flag = 1;
break;
}
}
if(c_flag == 0)
{
printf("Wrong course CRN(s) entered.\n");
break;
}
}
}
return *new_student;
}
//Define the main() function.
int main()
{
struct course all_courses[8];
struct student students[100];
int student_count = 0;
int id;
all_courses[0] = create_course(4587, "MAT 236", 4);
all_courses[1] = create_course(4599, "COP 220", 3);
all_courses[2] = create_course(8997, "GOL 124", 1);
all_courses[3] = create_course(9696, "COP 100", 3);
all_courses[4] = create_course(1232, "MAC 531", 5);all_courses[5] = create_course(9856, "STA 100", 2);
all_courses[6] = create_course(8520, "TNV 400", 5);
all_courses[7] = create_course(8977, "CMP 100", 1);
int i=0;
int choice = print_menu();
while(choice != 0)
{
switch(choice)
{
case 0:
break;
case 1:
students[student_count] = add_student(students, student_count, all_courses,7);
student_count++;
printf("\nStudent added successfully!\n");
break;
case 2:
add_del_course(students, student_count, all_courses,7);
break;
case 3:
search_student(students, student_count);
break;
case 4:
printf("Enter the id of the student: ");
scanf("%d", &id);
for(i=0;i<student_count;i++)
{
if(id == students[i].id)
{
print_invoice(students[i]);
}
}
break;
default:
printf("\nPlease enter a valid choice.\n");
}
printf("\n--------------------\n");
choice = print_menu();
}
return 0;
}
Please have the code run as shown in the sample run. Use as many functions as...
Written in C, if you are able to provide comments so I can learn as I practice that would be amazing! Thank you! Learning Outcomes: Selection structures and Loops Read carefully before you start coding! At Valence community college, a student can’t take more than 3 courses under the constraint of having no more than 7 credit hours. The purpose of this assignment is to construct a fee invoice for a student. This requires the input of Student’s id as...
C Programming
Please read the sample runs carefully as this is quite different from the previous projects. In this project, the student can take as many courses as permitted. The list of courses to take are listed below. This time there is no restriction on the total credit hours Your code should allow the user, after you enter the student' s id, to do the following options: 1- Add a course for the student 2- Drop a course for the...
The homework assignment is to write a fee invoice using C. It
must follow the guidelines posted in the question and run as the
sample run given. Here is the question and sample run below
Please read the sample runs carefully as this is quite different
from the previous projects. In this project, the student can take
as many courses as permitted. The list of courses to take are
listed below. This time there is no restriction on the total...