Hi people
Im taking an undergraduate degree in elctronic engineering. Just done an exam lab (IN C) where we had to produce a program in 2 hours.
The specification was:
Design a computer record system
The program should store the following details:
Student's name
Student registration number
course name e.g. "electronic engineering"
List of course options taken (no more than four options). Each course option can have four characters e.g. "EE1E"
The following operation should be implemented:
- Create a new student, entering all data about the student
- List all the students and associated data
- Search and display all the students with a given student registration number
- search and display all the students taking a particular course name
- search and display all the students taking a particular option
- Delete a student from the system
The way i thought to do it was to use an if else cause as the main structure of the program so the user picks an option 1-6 and then run each of the required operations.
What im asking for is if someone (who knows c programming well) could act as a sort of an examiner and give me a mark out of 10 on my humble effort
C programming happens to be my worst subject. I sat through most of the exam scratching my head. After the two hours this is what i manged to come up with (bearing in mind it was an open textbook exam!):
Code:
#include <stdio.h>
#include <string.h>
int main(void)
//main if else fuction used so the user can pick from 6 options
{
int value;
printf("Enter an option 0f 1-6: ");
scanf("%d", &value);
if (value==1)
//function to add a new student
typedef struct
{
char name[20];
int srn;
char course[30];
char option[4];
}student s;
{
struct student s1;
printf("enter student name:");
scanf("%c",s.name);
printf("enter srn number:");
scanf("%d",s.srn);
printf("enter course name:");
scanf("%c",s.course);
printf("enter course option-EE1E, EE1B, EE1C, EE1D:");
scanf("%c",s.option);
}
//inserts the node into list Function mknode() allocates storage for a node, initializes it, and returns a pointer to it (for the examiner, trying to make it look like i know what im talking about).
//Didnt have a clue about data structures but i thought this may be the code to actually store the data entered.
struct node* mknode(int data)
{
struct node* np;
np = (struct node*) malloc(sizeof(struct node));
if (np)
{
np->data=data;
np->next = NULL;
}
return np;
}
}
//option 2 displays all students using display_list() function
else if (value==2)
struct node* append_node(struct node** head, struct node* np)
{
struct node* n;
if (*head==NULL)
*head=np;
else
{
for (n=*head; n->next!=NULL; n=n->next);
n->next=np;
}
return np;
}
void display_list(struct node* head)
{
struct node* n;
for (n=head; n!=NULL; n=n->next)
printf("list data element = %d \n",n->data);
}
int main(void)
{
int i;
struct node* n;
struct node* head=NULL;
for (i=0; i<10; i++)
{
n=mknode(i);
append_node(&head,n);
}
display_list(head);
return 0;
}
else if (value==3)
// search for student registration number
void find_integer(int key, int array[], int SRN)
{
int j;
for (j=0; j<N; j++)
if (key==array[j])
return j;
return (-1);
}
{
find integer(int key, int array[], int srn)
printf("enter srn:")
scanf("%d",srn)
}
else if (value==4)
//funtion to find students taking a particular course name
int find_string(char* key, char* array[], int N)
{
int j;
for (j=0; j<N; j++)
if (!strcmp(key, array[j]))
return j;
return -1;
}
int main(void)
{
int found;
char course[20];
char* course_table[]={"Electronic Engineering","Businness Studies","Economics","Maths"};
printf("Input the course name : ");
scanf("%s",course);
found=find_string(course,course_table,4);
if (found>=0)
printf("course %d \n",found);
else
printf("Course not found! \n");
return 0;
}
else if (value==5)
//funtion to find students taking a particular course option
int find_string(char* key, char* array[], int option)
{
int j;
for (j=0; j<option; j++)
if (!strcmp(key, array[j]))
return j;
return -1;
}
int main(void)
{
int found;
char option[20];
char* option_table[]={"EE1E","EE1B","EE1C","EE1D"};
printf("Input the course option : ");
scanf("%s",option);
found=find_string(option,option_table,4);
if (found>=0)
printf("The option %d \n",found);
else
printf("option not found! \n");
return 0;
}
else if (value==6)
//function for deleting a student from the registration system
struct node* delete_node(struct node** head, struct node* np)
{
struct node* n;
if (*head==NULL)
return NULL;
else if (np==*head)
*head=(*head)->next;
else
{
for (n=*head; n->next!=np; n=n->next);
n->next=np->next;
}
return np;
}
}
int main (void)
struct node* delete_node;
printf("enter srn number:")
scanf ("%d"s.srn)
{
else
printf ("invalid option entered")
return 0;