Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2005, 12:24 PM   #1
blueandconfused
Newbie
 
Join Date: Apr 2005
Posts: 16
Rep Power: 0 blueandconfused is on a distinguished road
two-dimensional arrays

Can someone please fix this program for me?
import java.io.*;

public class TwoDimensional
{
	static BufferedReader keyboard =
	new BufferedReader(new InputStreamReader(System.in));

	public static void main (String[] args) throws IOException
	{
		int[][] input = {{3, 5, 51, 14, 45},
			        {23, 32, 22, 15, 62},
			        {4, 8, 6, 17, 37},
			        {6, 9, 7, 20, 51},
			        {8, 10, 23, 30, 90}};
		int row;
		int col;
		int largest;

			for (col = 0; col < input[0].length; col++)
		{
		  System.out.print(input[0][col] + " ");
		}


		for (int i = 0; i < 5; i++)
		{
			System.out.print(input[i][0] + ", ");
		}
		System.out.println(input[4][0] + ".");				//end output/input

		int Largest, sort, unsort, i;					//start sorting
		Largest = 0;
		int[][] NewAry = new int[5][5];
		i = 0;
		System.out.println("your sorted numbers are: ");
		for(sort = 0; sort < 5; sort++)
		{
			for(unsort = 0; unsort < 5; unsort ++)
			{
				if(input[unsort][0] > Largest)
				{	Largest = input[unsort][0];
					i = unsort;
				}
			}


			NewAry[0][sort] = Largest;
			input[0][i] = -1;

		}										//end sorting
		for (i = 0; i < 4; i++)			//output final sort
		{
			System.out.print(NewAry[i][0] + ", ");
		}
		System.out.println(NewAry[4][i] + ".");


		int search;														//start search
		System.out.print("Enter a number to search for: ");
		search = Integer.parseInt(keyboard.readLine());
		for(i = 0; i < 5; i++)
			if(NewAry[i][0] == search)
				System.out.println("Found it.");				//output search result

		col = 2;
		for(row = 0; row < input[0].length; row++)
		{
			System.out.print(input[row][0]);
		}
		System.out.println("your sorted numbers are: ");
			for(sort = 0; sort < 5; sort++)
			{
				for(unsort = 0; unsort < 5; unsort ++)
				{
					if(input[0][unsort] > Largest)
					{	Largest = input[0][unsort];
						i = unsort;
					}
				}
				NewAry[0][sort] = Largest;
				input[0][i] = -1;
				Largest = -1;
			}										//end sorting
			for (i = 0; i < 4; i++)			//output final sort
			{
				System.out.print(NewAry[0][i] + ", ");
			}
	System.out.println(NewAry[0][4] + ".");

	int x;
															//start search
	System.out.print("Enter a number to search for: ");
	x = Integer.parseInt(keyboard.readLine());
	for(i = 0; i < 5; i++)
		if(NewAry[0][i] == x)
		System.out.println("Found it.");				//output search result

	}
}

Last edited by blueandconfused; Apr 11th, 2005 at 12:44 PM.
blueandconfused is offline   Reply With Quote
Old Apr 11th, 2005, 1:16 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
What are the errors?
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 11th, 2005, 2:34 PM   #3
blueandconfused
Newbie
 
Join Date: Apr 2005
Posts: 16
Rep Power: 0 blueandconfused is on a distinguished road
Quote:
Originally Posted by Mjordan2nd
What are the errors?
well it compiles fine with no errors but it doesnt run right, this is what i get when i run it....
3 5 51 14 45 3, 23, 4, 6, 8, 8.
your sorted numbers are:
23, 0, 0, 0, 0.
Enter a number to search for: 5
323468your sorted numbers are:
51, 45, 14, 3, -1.
Enter a number to search for: 3
Found it.

there is something wrong with the algorithms maybe?...
blueandconfused is offline   Reply With Quote
Old Apr 12th, 2005, 7:01 AM   #4
Gigabytee
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 Gigabytee is on a distinguished road
blueandconfused..
Please Clarify what you want your code to do. Sort.. Search.. ?
and what's the exact error you r talking about..
----
Gigabytee
Gigabytee is offline   Reply With Quote
Old Apr 12th, 2005, 9:23 AM   #5
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
Even I ran the program....and I cannot understand the motive behind this program.....please clarify....It will make us easier to solve...
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Old Apr 12th, 2005, 9:42 AM   #6
Gigabytee
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 Gigabytee is on a distinguished road
I will write & post code that will search & sort the table based on your code first thing in the morning.... but now I have to sleep it's after midnight here in Australia!

Good luck & keep trying that is the best way to learn, in no time it will be a piece of cake
-----
Gigabytee
Gigabytee is offline   Reply With Quote
Old Apr 13th, 2005, 5:23 AM   #7
Gigabytee
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 Gigabytee is on a distinguished road
Lightbulb

Hay,
I have written a little code based on your code.. is this what you wanted?
This sorts the array then you can search from it
import java.io.*;
import java.lang.Math;

public class TwoDimensional1
{
	static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	public static void main (String[] args) throws IOException
	{
		int search, IndexOfFound;
		int[][] input, newTable;
		
		input=setTable();
		System.out.println("******Input Table******");
		printTable(input);
		newTable=sortTable(input);
		System.out.println("\n\n******Sorted Table******");
		printTable(newTable);
		System.out.print("\n\nPlease Enter Number to Search: ");
		search = Integer.parseInt(keyboard.readLine());
		System.out.println();
		searchTable(search, newTable);
	}
	private static void searchTable(int search, int[][] table)
	{
		int flag=0;
		for(int row=0;row<table.length;row++)
		{
			for(int col=0;col<table[0].length;col++)
				if(table[row][col]==search)
				{
					System.out.println("("+search+") was Found in\n\tRow:"+(row+1)+"\n\tCol:"+(col+1)+"\n");
					flag=9;
				}
		}
		if (flag==0)
		{
			System.out.println("("+search+") is not Found in this Table\n");
		}
	}
	private static int[][] setTable()
	{
		int[][] table1 =
		{
			{3, 5, 51, 14, 45},
			{23, 32, 22, 15, 62},
			{4, 8, 6, 17, 37},
			{6, 9, 7, 20, 51},
			{8, 10, 23, 30, 90}
		};
		return table1;
	}
	
	private static void printTable(int[][] table)
	{
		for(int row=0;row<table.length;row++)
		{
			for(int col=0;col<table[0].length;col++)
				System.out.print(table[row][col] + "\t");
			System.out.println();
		}
	}
	private static int[][] sortTable(int[][] table)
	{
		int[][] newTable;
		int[] tempArray;			
		tempArray=convertFromTwo_to_One(table);		
		tempArray=sortArray(tempArray);			
		newTable=convertFromOne_to_Two(tempArray);
		return newTable;
	}
	
	/*Converts 2 dimentional Array to 1 Dimention Array*/
	private static int[] convertFromTwo_to_One(int[][] table)
	{
		int i=0;
		int arrayLength = table.length*table[0].length;
		int[] tempArray = new int[arrayLength];
		for(int row=0;row<table.length;row++)
		{
			for(int col=0;col<table[0].length;col++)
			{
				tempArray[i] = table[row][col];
				i++;
			}
		}
		return tempArray;
	}
	
	/*Converts 1 dimentional Array to 2 Dimention Array*/
	private static int[][] convertFromOne_to_Two(int[] a)
	{
		int x = (int)(Math.sqrt(a.length));
		int[][] newTable = new int[x][x];
		int i =0;
		for(int row=0;row<newTable.length;row++)
		{
			for(int col=0;col<newTable[0].length;col++)
			{
				newTable[row][col]=a[i];
				i++;
			}
		}	
		return newTable;
	}
	/*Sorts int[] passed to it from smallest to biggest*/
	private static int[] sortArray(int[] a)
    {
        int index, indexOfNextSmallest;
        for (index = 0; index < a.length - 1; index++)
        {
        	//Place the correct value in a[index]:
            indexOfNextSmallest = indexOfSmallest(index, a);
            interchange(index,indexOfNextSmallest, a);
            /*a[0] <= a[1] <=...<= a[index] and these are the smallest
            of the original array elements. The remaining positions
            contain the rest of the original array elements.*/
        }
        return a;
    }

    /**
     Returns the index of the smallest value among
     a[startIndex], a[startIndex+1], ... a[a.length - 1]
    */
    private static int indexOfSmallest(int startIndex, int[] a)
    {
        int min = a[startIndex];
        int indexOfMin = startIndex;
        int index;
        for (index = startIndex + 1; index < a.length; index++)
            if (a[index] < min)
            {
                min = a[index];
                indexOfMin = index;
                /*min is smallest of a[startIndex] through a[index]*/
            }
        return indexOfMin;
    }

    /**
     Precondition: i and j are legal indices for the array a.
     Postcondition: Values of a[i] and a[j] have been interchanged.
    */
    private static void interchange(int i, int j, int[] a)
    {
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp; /*original value of a[i]*/
    }
}

---------
Cheers,
Gigabytee
Gigabytee is offline   Reply With Quote
Old Apr 13th, 2005, 8:05 AM   #8
blueandconfused
Newbie
 
Join Date: Apr 2005
Posts: 16
Rep Power: 0 blueandconfused is on a distinguished road
that's perfect, except i dont want the tables because i havent learned that yet.
blueandconfused is offline   Reply With Quote
Old Apr 13th, 2005, 11:55 AM   #9
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
U don't really have to limit yourself at what u learn at school. Most likely the teacher will apreciate it
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Apr 13th, 2005, 10:32 PM   #10
blueandconfused
Newbie
 
Join Date: Apr 2005
Posts: 16
Rep Power: 0 blueandconfused is on a distinguished road
once i did more than what i had learned and he got mad. so i dont think he would like it.
blueandconfused is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC