![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 16
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
What are the errors?
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Apr 2005
Posts: 16
Rep Power: 0
![]() |
Quote:
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?... |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
blueandconfused..
Please Clarify what you want your code to do. Sort.. Search.. ? and what's the exact error you r talking about.. ---- Gigabytee |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2005
Posts: 16
Rep Power: 0
![]() |
that's perfect, except i dont want the tables because i havent learned that yet.
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Apr 2005
Posts: 16
Rep Power: 0
![]() |
once i did more than what i had learned and he got mad. so i dont think he would like it.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|