Lab exam

This is a discussion on Lab exam within the C Programming forums, part of the General Programming Boards category; Hi people Im taking an undergraduate degree in elctronic engineering. Just done an exam lab (IN C) where we had ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8

    Smile Lab exam

    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;

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    does it work. i think would be the first questioin. i have never seen int main(void) so many times in one prog. personaly i would have used while(value != 0){} with switchcase(). if else works for you good. and missing } at end. just a few comments

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8

    Question

    it didnt work had 36 errors, dindn't hav enough time to figure out why

    was i along the right tracks?

    does anyone else have an opinion? would you say it was worth 40/100 marks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well you have many main() functions, and the top-level if/else constructs appear outside of functions (or so it seems).

    The general approach is to press the compile button often (like every 5 lines)
    Code:
    #include <stdio.h>
    int main ( ) {
      return 0;
    }
    This would compile

    Code:
    #include <stdio.h>
    int main ( ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
      }
      return 0;
    }
    ]
    This too would compile.

    Code:
    #include <stdio.h>
    int main ( ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf( "hello world %d" i );
      }
      return 0;
    }
    ]
    This wouldn't compile, but you're not going to get a flood of errors, and you'd be pretty sure it had something to do with your last edit. Finding one error in 5 lines is a lot better than finding dozens of errors in hundreds of lines.

    Basically, only write as much code as you're prepared to deal with the consequences of lots of error messages from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    I don't want to be rude (and I'm not an instructor) but I'd consider a 40/100 generous for that code.

    I can understand the very basic logic of what you are trying to do, but that part is pretty much given in the description of the problem.

    Otherwise it looks like you just ripped functions out of the book. The two basic ways to solve the problem would be with an array of students, or a linked list of students. You mixed and matched generic array and linked list implementations of the add, search, del... functions.

    The troubling part is the overall structure.

    Like already said you have multiple main()'s, you define functions within main, you never call the functions...... It's just a long way from anything that would work

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    >scanf("%s",option);


    scanf("%s", &option);

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by kryptkat
    >scanf("%s",option);

    scanf("%s", &option);
    Why? So it's even less correct?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    scanf
    This function is defined as follows:


    int scanf(char *format, args....) -- reads from stdin and puts input in address of variables specified in args list. Returns number of chars read.


    Format control string similar to printf


    Note: The ADDRESS of variable or a pointer to one is required by scanf.


    scanf(``%d'',&i);


    We can just give the name of an array or string to scanf since this corresponds to the start address of the array/string.




    char string[80];
    scanf(``%s'',string);
    http://www.cs.cf.ac.uk/Dave/C/node18...00000000000000

    edit
    char option[20];
    ...
    scanf("%s",option);

    was not a pointer and i thought it was an int.

    after looking at it again seen it was array.
    Last edited by kryptkat; 05-13-2005 at 02:21 PM.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    2
    great job!!!! but a long way to go... Hope that u pass. i am also a engineering tudent but i never saw a 'c programme' so 'brutally raped'. please mug up all the progs but don't commit such mistakes.

    cons
    1. Using linked list when no list is defined or declared.
    2. declaring and defining varibles and function through out the prog.(declaring can be done only in the begining of the prog)
    3. mismatching brackets (atleast count them and put them correctly)
    4. scanf("%d",s.srn); is not allowed. (use a '&' before s.srn)
    5. where is the options menu.
    6.and many more stated above

    pro's
    1. beautifully indented. (works well only when the code works)
    2. use of comments. (helps in debugging one or two errors or understating a prog)
    3. tried really well to fake ur knowledge.

    if i was your examer i would have very politely asked to try next time.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kryptkat
    http://www.cs.cf.ac.uk/Dave/C/node18...00000000000000

    edit
    char option[20];
    ...
    scanf("%s",option);

    was not a pointer and i thought it was an int.

    after looking at it again seen it was array.
    Even if it was an int, it would have been wrong. You don't use %s for integers.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8

    Thumbs up Thanks for taking a look

    Thanks everyone for your much valued opinions

    Sorry I took so long to respond, up to my eyeballs in revision and exams (paying the price for slacking off all year)

    Just as i thought i think it will be a resit exam in C for me in August. Oh well! It was worth a try.

    I think this time I'll actually try learning how to write in c before entering the exam!

  12. #12
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8
    I'll let u all know how I got on with this exam when I get my results in mid August

    Wish me good luck (even though I think I'll need more than luck, maybe a miracle)!

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 07:39 AM
  2. Exam of programming
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-15-2005, 02:40 PM
  3. Datastructure lab
    By El_Maco in forum C++ Programming
    Replies: 14
    Last Post: 10-10-2004, 07:35 AM
  4. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 08:46 PM
  5. Please help debug this lab! Please!
    By mack_ol in forum C Programming
    Replies: 0
    Last Post: 02-17-2002, 11:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21