Zim's Life in TCS Trivandrum! | Yet another ILP story for ...
QUESTION 1: Improve the understandability of the below given code:
import java.util.*; //importing the required packages import java.io.*;
class problem3 //creating a class
{
int[] numArray = new int[10]; //declaring an integer array of size 10 called numArray
public static void incrementElements (int[] integerArray) //defining the 'incrementElementst()'method with one integer parameter
{
int arraylen = integerArray.length; // initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array
for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to print the current values of array
{
System.out.println(integerArray[i]); //printing the element at position or index ‘i’
}
for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to increment the current values of array
{
integerArray[i] = integerArray[i] + 10; //add 10 to each value of array
}
for (int i=0; i < arraylen; i ++)
{
System.out.println(integerArray[i]);
}
}
EXPLANATION:
When any element of the integer array occurs, then it is printed first and after that it is incremented by 10 and then the resultant array is printed. The code is predefined in the package and we want to execute any application which requires the code. We don’t have to write the whole code as we can directly import the package and execute that application.
QUESTION 2: Improve the understandability of the below given code:
class Problem1 //creating a new class
{
int[] a; //declaring a new integer variable 'a' of array type to store the numbers
int nElems; //declaring an integer variable to hold the value of no. of elements in the array
public ArrayBub(int max)
{
a = new int[max];
}
public void insert(int value) //defining the insert method to insert values in the array
{
a[nElems] = value; //assigning the value to array at current position
nElems++; //incrementing the position counter
}
public void Sort() //defining the method to sort the array
{
int out, in; // declaring two integer variables 'out' & 'in'
for(out=nElems-1; out>1; out--) //outer loop
for(in=0; in a[in+1] ) //conditional statement to compare the two values
swap(in, in+1); //swapping the two values in specified order
}
public void swap(int one, int two) //defining 'swap' function to perform swapping of elements
{
long temp = a[one]; //interchanging the values
a[one] = a[two];
a[two] = temp;
}
EXPLANATION:
This is a program for Bubble Sort. It is the simplest sorting algorithm. In bubble sort algorithm, the 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 follows the same steps and performs multiple passes until the values of the array are sorted. Bubble sort has worst case if the elements are all in reverse order. In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is O(n).
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- what s new in fashion for 2019
- what s important in life quotes
- that s life song lyrics
- stage 5 parkinson s life expectancy
- life in prison for marijuana
- hitler s life before he became a dictator
- moving on with one s life synonym
- athena s life story
- alzheimer s life expectancy table
- alzheimer s life expectancy formula
- what s life like in prison
- life in a women s prison