Pages

Friday, 16 September 2011

TCS 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.


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;ib
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 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.


Tuesday, 28 June 2011

Truth vs Lie

This post is inspired by a very interesting status message that a dear friend updated on Facebook. It read "Truth and lie might be poles apart but they have one thing in common. They both prove a person's worth."
And well it made me think about the politicians who have lied about their black money, made unfulfilled promises, assured lower strata of the society of a better life and etc etc... It made me think of my mobile service provider's promise of connecting me to my family and friends, which it failed to fulfill a thousand times. It made me think of a thousand other companies who had broken my trust and yet done nothing about it. Surprisingly in all these cases I changed my vote, telephone number or the company. I just went to a more trustworthy person (I rest my case with politicians). The switch took place since a head existed for the tail. And when it did not, like politicians for instance I had to stick to the tail. The reason these heads exists is because there are tails. The trust we get from the trustworthy company acts as a solace to the inconvenience caused by the unfulfilled promise (lie). You can't have heads without tails or truth without lie. An honest person's worth is still determined by a liar. So if there are many liars in the business, politics or your Eco-system, an honest person would be worth much more. The honest firm or person can act out of benevolence and can get many fans or in some cases command a premium too.
Truth is again a subjective matter. Is it the truth to self, firm (where you are working) or peers. Being true to self automatically puts you in the right circle of friends and also lands you the right kind of job. Further more , truth to self results in you sharing the same vision as your organization and you slowly climbing up the corporate ladder. The reason we have great companies who keep up the promises is due to the simple fact that they are run by people who are honest to self. Although money can't buy happiness, these firms typically have greater market share and loyal consumers. That means more moolah and better quality of life.