Friday 12 August 2011

Magic Square

I designed an interesting program

I wrote code for the creation and initialization
of a magic square of the given odd size. I implemented the
following algoritm:

- put 1 in the middle cell of the first row;
- every next value must be placed in a cell
one row up and one column right relative to the previous one;
- if the destinaton of the next cell appears outside of the
array, enter the later from the opposite end;
- if the destinaton of the next cell is already occupied,
then move just one row down relative to the previous cell.

For example,

for size = 3,

8    1    6
3    5    7
4    9    2

the sum of all rows and columns is 15


Here is the code
class Grid
{
int num;
public void fillGrid(int n)
{
int ar[][]=new int[n][n];
num=n;
fill(ar);
int r=0,c=n/2,i;
for(i=1;i<=(n*n);i++)
{
if(ar[r][c]==0)
ar[r][c]=i;
else
ar[++r][c]=i;

/*printArray(ar);
System.out.println("\n\n\n\n");*/

if((r-1)<0 && (c+1)>=n)
r++;
else
{
if((r-1)<0)
{
r=n-1;
c++;
}
else if((c+1)>=n)
{
r--;
c=(c+1)-n;
}
else if(ar[r-1][c+1]==0)
{
r--;
c++;
}
}
}
printArray(ar);
}
void fill(int Ar[][])
{
int i,j;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
Ar[i][j]=0;
}
}
void printArray(int Ar[][])
{
int i,j;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
System.out.print("\t "+Ar[i][j]);
System.out.println("");
}
}
}


Saturday 6 August 2011

Binary Search in an array

import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch
{
// precondition: array a[] is sorted
public int search(int[] a,int key)
{
int lo = 0;
int hi = a.length - 1;
while (lo <= hi)
{
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid])
hi = mid - 1;
else if (key > a[mid])
lo = mid + 1;
else return mid;
}
return -1;
}
public static void main(String[] args)
{
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++)
{
arr[i] = input.nextInt();
}
BinarySearch search = new BinarySearch();
System.out.print("Enter the element to search: ");
int num=input.nextInt();
int n = search.find(arr, num);
if (n!=-1 )
{
System.out.println("Found at index: " + n);
}
else
{
System.out.println("Not Found");
}
}

}

Linear Search in an array in java

Linear Search in Java

In this section, we are going to find an element from an array using Linear Searching. Linear searching is a good way to find an element from the array. The array can be of any order, it checks whether a certain element (number , string , etc. ) is in a specified array or not. Basically it is used for small arrays. In the given code, we have allowed the user to enter the numbers to be stored into the arrays. If the element is found we can return the index where the element is located in the array. If the element is not found we can return -1.
Here is the code:

import java.util.Scanner;

public class LinearSearch
{
public int find(final int[] data, final int key)
{
for (int i = 0; i < data.length; ++i) {
if (data[i] > key)
return -1;
else if (data[i] == key)
return i;
}
return -1;
}

public static void main(String[] args)
{
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
}
LinearSearch search = new LinearSearch();
System.out.print("Enter the element to search: ");
int num=input.nextInt();
int n = search.find(arr, num);
if (n!=-1 )
{
System.out.println("Found at index: " + n);
} else
{
System.out.println("Not Found");
}
}

}

Output:

Enter 10 numbers:
1
2
3
4
5
6
7
8
9
10
Enter the element to search:5
Found at index: 4