You are on page 1of 16

www.gullybaba.

com
Course Code Course Title Assignment Number : MCSL-025 : Lab Course : MCA (2)/025L/Assign/12

***************************************************************** PART-1: MCS-021

Question 1: Write a program in C language to implement 2-way merge sort. Input the following data to the program. Show all intermediate steps: 104,50,20,39,206,29,409,26,298,3,10,8 Ans:
#include<stdio.h> int *a[100],*b[100],*c[100],i,j,k,item1,item2; void main() { clrscr(); printf("\n Enter the number of elements in the first array\n"); scanf("%d",&item1); printf("\n Enter %d numbers\n",item1); for(i=0;i<item1;++i) scanf("%d",&a[i]); printf("\n Enter the number of elements in the second array\n"); scanf("%d",&item2); printf("\n Enter %d numbers\n",item2);

www.gullybaba.com
for(i=0;i<item2;++i) scanf("%d",&b[i]); input1(); input2(); sort(); printf("Sorted merged array is:\n"); display(); }

input1() { bsort(a,item1); printf("\n Sorted first array\n"); for(i=0;i<item1;++i) printf("%d\n",a[i]); }

input2() { bsort(b,item2); printf("\n Sorted second array\n"); for(i=0;i<item2;++i)

www.gullybaba.com
printf("%d\n",b[i]); }

bsort(int *m[],int n) { int swap=1,*temp; for(i=0;i<n && swap==1;++i) { swap=0; for(j=0;j<n-(i+1);++j) if (m[j]>m[j+1]) { temp=m[j]; m[j]=m[j+1]; m[j+1]=temp; swap=1; } } }

display() { for (i=0;i<item1+item2;++i) printf("%d\n",c[i]);

www.gullybaba.com
} sort() { int i,j,k; i=j=k=0; while ((i<item1)&& (j<item2)) { if (a[i]<b[j]) { c[k]=a[i]; i++; k++; } else { if (a[i]>b[j]) { c[k]=b[j]; j++; k++; } else {

www.gullybaba.com
c[k]=a[i]; i++; j++; k++; } } } while(i<item1) { c[k]=a[i]; i++; k++; } while(j<item2) { c[k]=b[j]; j++; k++; } } }

www.gullybaba.com
Question 2: Write a program in C language to convert a Tree into a Binary Tree. Ans:
struct Node * newroot = '\0'; struct Node* PostOrder(Struct Node* root) { if(root != '\0') { PostOrder(root->left); PostOrder(root->right); insertBST(root, &newroot); } }

insertBST(struct Node* node, struct Node** root) { struct Node * temp, *temp1; if( root == '\0') { *root == node; node->left == '\0'; node->right == '\0'; } else {
6

www.gullybaba.com
temp = *root; while( temp != '\0') { temp1= temp; if( temp->data > node->data) temp = temp->left; else temp = temp->right; } if(temp1->data > node->data) { temp1->left = node; } else { temp1->right = node; } node->left = node->right = '\0'; } }

www.gullybaba.com
PART-2: MCS-022 Question 1: Write a shell script in Linux/Unix that accepts a text file as input and prints the number of words which consist of at least one vowel. Ans:
Clear echo " Enter the String" read str echo $str > str.txt len = 'echo $str | wc -c' len = 'expr $len -1' c= 0 , w = 0 , s = 0 , i = 1 echo " Length of string = $len" w = 'echo $str | wc - w' c = 'echo $str | wc -c' while [ i -le $ len ] do st = 'cut - c$I str.txt' if ["str" = " "] then s = 'expr $s + 1' fi i = 'expr $i + 1 done echo "Character = $c" echo "Words = $w"
8

www.gullybaba.com
echo "Spaces = $s"

Question 2: Your PC is on a network. Now, take a print from a printer which is not the default printer for your PC. Ans:
To print to a different (secondary) printer, click on the File menu and select Print. The print dialog box displays all installed printers. Select the printer you wish to use and click OK to close the dialog box. After that our data is printed by the respective printer.

PART-3: MCS-023 Question 1: Create a database consisting of students Enroll. No. , Programme of study, and marks secured in each of the courses in MCA. After creating the database, perform the following tasks: (i) Find the no. of students who failed only in 1 course in all the 6 semesters. (ii) Generate a report indicating the no. of students who secured marks between 60-70%, >70 80% and >80% Ans:
Create table student ( enrl_no Name Course varchar2(11) primary key, varchar2(20), varchar2(6));

Create table marks ( enrl_no varchar2(11) foreign key,


9

www.gullybaba.com
Course_code varchar2(7), Obtained_marks Remark varchar2(2), varchar2(10));

Select enrl_no, name, remarks from student s where s.enrl_no in(select enrl_no from marks where marks.obtained_marks<50)); Select enrl_no, name, remarks from student s where s.enrl_no in(select enrl_no from marks where marks.obtained_marks between 60 to 70)); Select enrl_no, name, remarks from student s where s.enrl_no in(select enrl_no from marks where marks.obtained_marks between 70 to 80)); Select enrl_no, name, remarks from student s where s.enrl_no in(select enrl_no from marks where marks.obtained_marks>80));

Part-4: MCS-024

Question 1: Write a program in Java for the implementation of a doubly linked list. Ans:
Implementing the Doubly-Linked List The objects of type ListItem are placed in a doubly-linked list. For this we have to define a class LinkedList. It contains a special ListItem, called the head, to give the user a principal handle to the list items. The insertion methods (insertBefore, insertAfter) and the removal method (remove) in the LinkedList class need a pointer to the position in the list at which action should take place. For this purpose we introduce a ListIterator class. A ListIterator contains the LinkedList to which it belongs and a pointer to the ListItem that is currently selected. The ListIterator class contains methods to move the cursor position. The LinkedList class contains the methods Head, which returns an iterator that points to the head of the linked list; find, which searches in the linked list for a requested object and, if found, returns an iterator pointing to the object To step through all current list items, we added a procedure elements in the LinkedList class; it returns an Enumeration object.
10

www.gullybaba.com
ListIterator.java final class ListIterator { LinkedList owner; ListItem pos; ListIterator(LinkedList owner, ListItem pos){ this.owner = owner; this.pos = pos; } /* * check whether object owns the iterator */ public boolean belongsTo(Object owner) { return this.owner == owner; } /* * move to head position */ public void head() { pos = owner.head; } /* * move to next position */ public void next() { pos = pos.next; } /* * move to previous position */ public void previous() { pos = pos.previous; } } LinkedList.java
11

www.gullybaba.com
import java.util.*; public class LinkedList { ListItem head; /* * creates an empty list */ public LinkedList() { head = new ListItem(null); head.next = head.previous = head; } /* * remove all elements in the list */ public final synchronized void clear() { head.next = head.previous = head; } /* * returns true if this container is empty. */ public final boolean isEmpty() { return head.next == head; } /* * insert element after current position */ public final synchronized void insertAfter(Object obj, ListIterator cursor) { ListItem newItem = new ListItem(cursor.pos, obj, cursor.pos.next); newItem.next.previous = newItem; cursor.pos.next = newItem; } /* * insert element before current position */ public final synchronized void insertBefore(Object obj, ListIterator cursor) { ListItem newItem = new ListItem(cursor.pos.previous, obj, cursor.pos);
12

www.gullybaba.com
newItem.previous.next = newItem; cursor.pos.previous = newItem; } /* * remove the element at current position */ public final synchronized void remove(ListIterator cursor) { if (isEmpty()) { throw new IndexOutOfBoundsException("empty list."); } if (cursor.pos == head) { throw new NoSuchElementException("cannot remove the head"); } cursor.pos.previous.next = cursor.pos.next; cursor.pos.next.previous = cursor.pos.previous; } /* * Return an iterator positioned at the head. */ public final ListIterator head() { return new ListIterator(this, head); } /* * find the first occurrence of the object in a list */ public final synchronized ListIterator find(Object obj) { if (isEmpty()) { throw new IndexOutOfBoundsException("empty list."); } ListItem pos = head; while (pos.next != head) { // There are still elements to be inspected pos = pos.next; if (pos.obj == obj) { return new ListIterator(this, pos); } } throw new NoSuchElementException("no such object found");
13

www.gullybaba.com
} /* * Returns an enumeration of the elements. Use the Enumeration methods on * the returned object to fetch the elements sequentially. */ public final synchronized Enumeration elements() { return new ListEnumerator(this); } }

Question 2: Write a program in Java that connects to a database and generates grad cards of the students. Make necessary assumptions. Ans:
1 import java.io.*; 2 import java.util.*; 3 4 class StudentDatabaseUse 5 { 6 // Read a number of Student records from a file, store them in a 7 // database, and perform operations on them. 8 // The file is given as a command line operator 9 10 public static void main(String[] args) throws IOException 11 { 12 Database database=null; 13 Student newStudent,foundStudent; 14 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 15 BufferedReader inFile; 16 String surname,command; 17 char ch; 18 int i; 19 if(args.length==0) 20 { 21 System.out.println("File name should be given as command-line argument"); 22 System.exit(1); 23 } 24 try 25 { 26 inFile = new BufferedReader(new FileReader(args[0]));
14

www.gullybaba.com
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 database = new StudentDatabase1(); for(;;) try { newStudent=Student.read(inFile); database.insert(newStudent); } catch(PersonException e) { System.out.println(e.getMessage()+"\n"); } } catch(FullUpException e) { System.out.println("No room for any more records"); } catch(FileNotFoundException e) { System.out.println("No file '"+args[0]+"' found"); System.exit(2); } catch(IOException e) { } ch='l'; while(ch!='q') { switch(ch) { case 'l': database.list(); break; case 'd': System.out.print("Enter surname of student to be deleted: "); surname=in.readLine(); try { database.delete(surname); System.out.println("Record deleted\n"); } catch(NotFoundException e) { System.out.println("No record found for "+surname+"\n"); } break; case 'r': System.out.print("Enter surname of student: "); surname=in.readLine(); try { foundStudent=(Student)database.retrieve(surname); System.out.println("\nFull record is:\n");
15

www.gullybaba.com
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 System.out.println(foundStudent); } catch(NotFoundException e) { System.out.println("No record found for "+surname+"\n"); } break; case 'i': try { newStudent=Student.promptedRead(in); database.insert(newStudent); System.out.println("Record inserted\n"); } catch(PersonException e) { System.out.println(e.getMessage()+"\n"); } catch(FullUpException e) { System.out.println("No room for any more records\n"); } break; case 'q': break; default: System.out.println("Not a valid command"); } do { do { System.out.print("Enter command: "); command=in.readLine(); } while(command.length!=0); i=0; do { ch=command.charAt(i++); } while(ch==' '&&i<command.length()); } while(ch==' '); } }

16

You might also like