Questions no 1

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 ”}

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

}

}

Ans :Program in basic code

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

}

Programs in basic terms

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 .

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

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;

}

lass 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;

}

}

Ans :Program in basic code

First initialise number of elements and an array.

den initialise max for a value and then set a varialble say a= maximum.

Later accept the value for the elements.

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

Programs in basic terms

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

IF you have More Question Share in Comment

## No comments:

## Post a Comment