Sie sind auf Seite 1von 17

Visit: www.vijayjotani.

tk

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");

Visit: www.vijayjotani.tk
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); 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]); }

Visit: www.vijayjotani.tk

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

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;

Visit: www.vijayjotani.tk
} } }

display() { for (i=0;i<item1+item2;++i) printf("%d\n",c[i]); } 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])

Visit: www.vijayjotani.tk
{ c[k]=b[j]; j++; k++; } else { c[k]=a[i]; i++; j++; k++; } } } while(i<item1) { c[k]=a[i]; i++; k++; } while(j<item2) { c[k]=b[j];

Visit: www.vijayjotani.tk
j++; k++; } } }

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) {

Visit: www.vijayjotani.tk
struct Node * temp, *temp1; if( root == '\0') { *root == node; node->left == '\0'; node->right == '\0'; } else { 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;

Visit: www.vijayjotani.tk
} node->left = node->right = '\0'; } }

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 ]

Visit: www.vijayjotani.tk
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" 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.

Visit: www.vijayjotani.tk
(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,

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:

Visit: www.vijayjotani.tk
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. 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; }

Visit: www.vijayjotani.tk
/* * move to next position */ public void next() { pos = pos.next; } /* * move to previous position */ public void previous() { pos = pos.previous; } } LinkedList.java 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; }

Visit: www.vijayjotani.tk
/* * 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); 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); }

Visit: www.vijayjotani.tk
/* * 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"); } /* * 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 2 3 4 5 6 7 8 9 import java.io.*; import java.util.*; class StudentDatabaseUse { // Read a number of Student records from a file, store them in a // database, and perform operations on them. // The file is given as a command line operator

Visit: www.vijayjotani.tk
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 public static void main(String[] args) throws IOException { Database database=null; Student newStudent,foundStudent; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader inFile; String surname,command; char ch; int i; if(args.length==0) { System.out.println("File name should be given as command-line argument"); System.exit(1); } try { inFile = new BufferedReader(new FileReader(args[0])); 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) {

Visit: www.vijayjotani.tk
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 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"); 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();

Visit: www.vijayjotani.tk
102 103 104 105 106 107 108 109 110 111 112 } while(command.length!=0); i=0; do { ch=command.charAt(i++); } while(ch==' '&&i<command.length()); } while(ch==' '); } }

Das könnte Ihnen auch gefallen