Вы находитесь на странице: 1из 8

CS Pre-ILP Assignments[Basics of Programming]

Warning To who ever is going to copy paste and submit the same copy as below, Answers can vary depending on thoughts. There is more than one way for a solution, please do NOT blindly Copy Paste. Thanks to the Community - TCS ILP 2011 and Ayan Jana for providing the solutions. Assignment 1 - Please zip and upload the files. Question 1 a. Search for a name Write a program to accept an array of names and a name and check whether the name is present in the array. Return the count of occurrence. Use the following array as input {Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam, Ayan, Dev, Kity, Meery, Smith, Johnson, Bill, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson} Program : import java.io.*; import java.util.Scanner; class program1 { public static void main(String args[]) { String[] names={Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam, Ayan, Dev, Kity, Meery, Smith, Johnson, Bill, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson}; Scanner sr= new Scanner(System.in); System.out.println("Enter a name to check"); String name=sr.nextLine(); int count=0; for(int i=0;i<names.length;i++) { if(names[i].equals(name)) count++; } if(count!=0) System.out.println("The Number of occurance of the Name is " + count ); else System.out.println("Word not Found"); }

b. Improve the understandability of the below given code:

import java.util.*; class problem3 { int[] numArray = new int[10]; public static void incrementElements (int[] integerArray) { int arraylen = integerArray.length; for (int i = 0; i < arraylen; i ++) { System.out.println(integerArray[i]); } for (int i = 0; i < arraylen; i ++) { integerArray[i] = integerArray[i] + 10; } for (int i=0; i < arraylen; i ++) { System.out.println(integerArray[i]); } }

CODE DESCRIPTION: The code defines that when any element of the integer array is occured then it is printed first and after that it's increamented by 10 .. and then the resultant array is printed, the same code is being defined in the component classes of java.util.*-The Component class is the abstract superclass of the nonmenu-related Abstract Window Toolkit components. Class Component can also be extended directly to create a lightweight component. A lightweight component is a component that is not associated with a native opaque window.the code is predefined in the package and when ever we want to execute any application which wants the code...den without wrting the whole code we can directly import the package and execute thAt application,...\.Here if we explain the code step by step, we can see that an array named numarray is assigned containing 10 elements, which defines the size of the array;

int num[]array=new int[10]; next the arraylength is calculated using integerArray.length, and next using for loop we set a loop from 0 to the obtained length of the array for (int i = 0; i < arraylen; i ++) next the present integer value is incremented, by a value of 10 and then the control passes to the next element in the array. next we have the display the resulted array using the final code: for (int i=0; i < arraylen; i ++) { System.out.println(integerArray[i]); } Question 2 Greatest common divisor Calculate the greatest common divisor of two positive numbers a and b. gcd(a,b) is recursively defined as gcd(a,b) = a if a =b gcd(a,b) = gcd(a-b, b) if a >b gcd(a,b) = gcd(a, b-a) if b > a Program:

import java.io.*; import java.util.Scanner; class program2 { public static void main(String args[]) { Scanner sr= new Scanner(System.in); System.out.println("Enter The number a"); int a=sr.nextInt(); System.out.println("Enter The number b"); int b=sr.nextInt(); int gcd; if(a==b) gcd=a;

else if(a>b) gcd=findgcd(a-b,b); else gcd=findgcd(b,b-a); System.out.println("The greatest common divisor of numbers " + a + " and " + b + " is " + gcd); } public static int findgcd(int c,int d) { if (d == 0) return c; return findgcd(d, c % d); } }

(b) Improve the understandability of the below given code: class Problem1 { int[] a; int nElems; public ArrayBub(int max) { a = new int[max]; } public void insert(int value) { a[nElems] = value; nElems++; } public void Sort() { int out, in; for(out=nElems-1; out>1; out--) for(in=0; in<out; in++) if( a[in] > a[in+1] ) swap(in, in+1); } public void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; } }

The above code explains bubble sorting implementation. Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follow the same steps repeatedly until the values of array is sorted. In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is O(n).

Say we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array. 1.Compare A[0] and A[1] . 2.If A[0]>A[1] then Swap A[0] and A[1]. 3.Take next A[1] and A[2]. 4.Comapre these values. 5.If A[1]>A[2] then swap A[1] and A[2] ............................................................... ................................................................ at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.

So in the above code we obtain: step 1: we initialise number of elements and an array. step 2:we initialise max for a value and then set a varialble say a= maximum. step3: we accept the value for the elements. step4: we set two variables out and in and we compare a[0] with a[1] and if a[0] > a[1] then swap them. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted. Read more: http://vineshbalan.blogspot.com/2011/05/tcs-pre-ilp-assignments-may-31basicsof.html#ixzz1XctIA3Pb

UNIX

Assignment 1 Question 1: Write a command to list all the files inside a folder i.e. if there is a folder inside a folder then it should list all files inside the sub-folder which is inside the folder to be listed. Ans : ls -R Question 2: Search all the files which contains a particular string, say include within a folder. Ans: grep include ./* Question 3: Rename all the files within a folder with suffix Unix_ i.e. suppose a folder has two files a.txt and b.pdf than they both should be renamed from a single command to Unix_a.txt and Unix_b.pdf Ans: for f in *.*; do mv $f Unix_$f; done Question 4: Rename all files within a folder with the first word of their content(remember all the files should be text files. For example if a.txt contains Unix is an OS in its first line then a.txt should be renamed to Unix.txt Ans: for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) ; mv $f $x; done Question 5: Suppose you have a C project in a folder called project, it contains .c and .h files, it also contains some other .txt files and .pdf files. Write a Linux command that will count the number of lines of your text files. That means total line count of every file. (remember you have to count the lines in .txt files only) Ans: wc l *.txt Question 6 : Rename all files which contain the sub-string 'foo', replacing it with 'bar' within a given folder. Ans: for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done Question 7: Show the most commonly used commands from history. [hint: remember the history command, use cut, and sort it] Ans: history|sort|cut d-10 -n Assignment 2

1.Create a tree structure named training in which there are 3 subdirectories level 1, level2 and cep. Each one is again further divided into 3. The level 1 is divided into sdp, re and se. From the subdirectory se how can one reach the home directory in one step and also how to navigate to the subdirectory sdp in one step? Give the commands, which do the above actions? Ans : To navigate from se to home cd ~ To navigate directly to sdp cd training/level1/sdp 2.How will you copy a directory structure dir1 to dir2 ? (with all the subdirectories) Ans : cp R dir1 dir2 3.How can you find out if you have the permission to send a message? Ans : ls -l 4.Find the space occupied ( in Bytes) by the /home directory including all its subdirectories. Ans : du -sb 5.What is the command for printing the current time in 24-hour format? Ans : date +%R 6.What is the command for printing the year, month, and date with a horizontal tab between the fields? Ans : date +%F 7.Create the following files: chapa, chapb, chapc, chapd, chape, chapA, chapB, chapC, chapD, chapE, chap01, chap02, chap03, chap04, chap05, chap11, chap12, chap13, chap14, and chap15. Ans: Cat > chapa Contents of file Ctrl+D Cat > chapb Contents of file Ctrl+D Cat > chapc Contents of file . . . Cat> chap15 Contents of file Ctrl+D 8.With reference to question 7, What is the command for listing all files ending in small letters? Ans : find . name *[a-z].*

9.With reference to question 7, What is the command for listing all files ending in capitals? Ans : find . name *[A-Z].* 10.With reference to question 7, What is the command for listing all files whose last but one character is 0? Ans: find . name *0?.* 11.With reference to question 7, What is the command for listing all files which end in small letters but not a and c? Ans : find . name *[b d-z].* 12.In an organisation one wants to know how many programmers are there. The employee data is stored in a file called personnel with one record per employee. Every record has field for designation. How can grep be used for this purpose? Ans : $ grep -c programmer personnel 13.In the organisation mentioned in question 12 how can sed be used to print only the records of all employees who are programmers. Ans : $ sed -n "/programmer/p" personnel 14.In the organisation mentioned in question 12 how can sed be used to change the designation programmer to software professional every where in the personnel file Ans : $ sed -e "s/programmer/software professional/g" personnel 15.Find out about the sleep command and start five jobs in the background, each one sleeping for 10 minutes. Ans: $ sleep 10 & sleep 10 & sleep 10 & sleep 10 & sleep 10 & 16.How do you get the status of all the processes running on the system? i.e. using what option? Ans : $ ps -e

Read more: http://vineshbalan.blogspot.com/2011/05/tcs-unix-assignment.html#ixzz1XctbZWJL

Вам также может понравиться