
Patrick B. answered 04/08/21
Math and computer tutor/teacher
I mostly agree with Jim's idea. But you MUST know the
dimensions of the 2-D array ahead of time !
There could be, for example 16 values in the array...
Is the array 4 by 4 or 8 by 2 or 2 by 8.... you can't tell!!!
There could be, for example, 24 values in the array...
Is the array 6 by 4, 4 by 6, 3 by 8 or 8 by 3, 12 by 2,
or 2 by 12; you can't tell...
So no, you CANNOT figure out the dimensions of the array
based only on the number of values input
For the FUN of it and just of kicks and entertainment, here is a 2-D selection sort.
class Sort2D
{
int numRows;
int numColumns;
double A[][];
boolean error_flag;
public void SetError() { error_flag=true; }
public void ClearError() { error_flag=false; }
public boolean GetError() { return(error_flag); }
public Sort2D(int r, int c)
{
numRows=r; numColumns=c;
A = new double [numRows][numColumns];
error_flag=false;
}
public boolean IsValidIndex(int row, int column)
{
return (
((row>=0) && (row<numRows)) &&
((column>=0) && (column<numColumns))
);
}
public void IndexerSetAtIndex(int row, int column, double val)
{
ClearError();
if (IsValidIndex(row,column))
{
A[row][column]=val;
}
else
{
SetError();
}
}
public double IndexerGetAtIndex(int row,int column)
{
ClearError();
double dblAmtReturn=0;
if (IsValidIndex(row,column))
{
dblAmtReturn = A[row][column];
}
else
{
SetError();
}
return(dblAmtReturn);
}
//selection sort
public void Sort()
{
int rowIndexOfMin=-1;
int columnIndexOfMin=-1;
double minValue = Double.MAX_VALUE;
int rowLoop,columnLoop;
int r,c;
for (rowLoop=0; rowLoop<numRows; rowLoop++)
{
for (columnLoop=0; columnLoop<numColumns; columnLoop++)
{
minValue = A[rowLoop][columnLoop];
rowIndexOfMin=rowLoop;
columnIndexOfMin=columnLoop;
for (r=rowLoop; r<numRows;r++)
{
for (
c= ((r != rowLoop) ? 0 : columnLoop); c<numColumns; c++)
{
if (A[r][c]<minValue)
{
minValue = A[r][c];
rowIndexOfMin = r;
columnIndexOfMin=c;
}
}
}
System.out.println("min value is "+minValue);
double dblTempVal = A[rowLoop][columnLoop];
A[rowLoop][columnLoop] = A[rowIndexOfMin][columnIndexOfMin];
A[rowIndexOfMin][columnIndexOfMin] = dblTempVal;
} //columnLoop
} //rowLoop
}
public void Dump(String strMsg)
{
if (strMsg!=null)
{
System.out.println("********************************************");
System.out.println(strMsg);
}
double dblAmt=-1;
System.out.println("********************************************");
for (int rowLoop=0; rowLoop<numRows; rowLoop++)
{
for (int columnLoop=0; columnLoop<numColumns; columnLoop++)
{
dblAmt = IndexerGetAtIndex(rowLoop,columnLoop);
System.out.print(dblAmt +" ");
}
System.out.println(" ");
}
}
}
//------------- Sort2DMain.java
import java.util.Scanner;
class Sort2DMain
{
void Go()
{
Scanner scanner = new Scanner(System.in);
int numRows=-1;
int numColumns=-1;
while (numRows<0)
{
System.out.print(" How many rows :>");
numRows = scanner.nextInt();
}
while (numColumns<0)
{
System.out.print(" How many columns :>");
numColumns = scanner.nextInt();
}
Sort2D sort = new Sort2D(numRows,numColumns);
double dblAmt =-1;
int rowLoop,columnLoop;
for (rowLoop=0; rowLoop<numRows; rowLoop++)
{
for (columnLoop=0; columnLoop<numColumns; columnLoop++)
{
System.out.print(" Input row " + (rowLoop+1) + ", column " + (columnLoop+1) + " :> ");
dblAmt = scanner.nextDouble(); scanner.nextLine();
// System.out.println(dblAmt);
sort.IndexerSetAtIndex(rowLoop,columnLoop,dblAmt);
}
}
sort.Dump("--------- unsorted --------------");
sort.Sort();
sort.Dump("----- sorted ------------");
}
public static void main(String args[])
{
Sort2DMain x = new Sort2DMain();
x.Go();
}
}