Okay.. now i need help to sort this kind of array.. as u can see.. i wan all element in the same row followed the 1st element in the array.. but the problem is.. the source code is too long.. is there any way to cut down the code to be more simple?
here is mine..
int temp = 0, p = 0, l = 0, k = 0;
int[,] arr = new int[2, 3] { { 100, 3, 2 }, { 10, 11, 12 } };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
if (arr[p, l] > arr[p + 1, l])
{
temp = arr[p + 1, l];
arr[p + 1, l] = arr[l, l];
arr[l, l] = temp;
temp = arr[p + 1, p + 1];
arr[p + 1, p + 1] = arr[l, p + 1];
arr[l, p + 1] = temp;
temp = arr[p + 1, p + 2];
arr[p + 1, p + 2] = arr[l, p + 2];
arr[l, p + 2] = temp;
}
Console.Write("{0} ", arr[i, j]);
k++;
if (k >= 3)
{
Console.WriteLine();
k = 0;
}
}
please help me..i already tired thinking about it..
Loading
AlanPosted Oct 14, 2007, 6:16 AM
Well, I don't know about making the code simpler, but you can bring all those strands together by asking the user to input the number of rows, the number of columns, the column to sort on (1-based) and the data itself:
using System;
class TwoDArraySorter
{
static void Main()
{
while (true)
{
Console.Write("\nNumber of rows : ");
int rows = int.Parse(Console.ReadLine());
Console.Write("Number of columns : ");
int cols = int.Parse(Console.ReadLine());
Console.Write("Column to sort on : ");
int sortCol = int.Parse(Console.ReadLine());
int[][] arr = new int [rows][];
for (int i = 0; i < rows; i++)
{
arr[i] = new int[cols];
Console.WriteLine("\nInput columns for row {0} :\n", i + 1);
for (int j = 0; j < cols; j++)
{
Console.Write("Column {0} : ", j + 1);
arr[i][j] = int.Parse(Console.ReadLine());
}
}
int[] temp = null;
sortCol--;
for(int i = 0; i < rows; i++)
for (int j = i + 1; j < rows; j++)
if (arr[i][sortCol] > arr[j][sortCol])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Console.WriteLine("\nThe sorted array is :\n");
foreach (int[] ia in arr)
{
for (int j = 0; j < cols; j++ )
{
Console.Write("{0,-10}", ia[j]);
if (j == cols - 1) Console.WriteLine();
}
}
Console.Write("\nSort another y/n : ");
if (Char.ToLower(Console.ReadLine()[0]) != 'y') break;
}
}
}
Hideki SnkPosted Oct 13, 2007, 9:53 PM
int[][] arr = new int[4][];
arr[0] = new int[] { 100, 3, 2 };
arr[1] = new int[] { 10, 11, 12 };
arr[2] = new int[] { 2, 5, 2 };
arr[3] = new int[] { 1, 10, 5 };
int[] temp = null;
for(int polo=1;polo<4;polo++)
for (int j = 0; j < 4; j++)
if (arr[j][0] > arr[polo][0])
{
temp = arr[polo];
arr[polo] = arr[j];
arr[j] = temp;
}
foreach (int[] ia in arr)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0} ", ia[j]);
if (j == 2) Console.WriteLine();
}
}
here my own code that i make after i take a shower...
int a=0,b=1,c=2,z=0;
int[][] arr = new int[2][];
arr[0] = new int[3];
int x = int.Parse(Console.ReadLine());
int d = int.Parse(Console.ReadLine());
int pl = int.Parse(Console.ReadLine());
arr[a][z] = x;
arr[a][b] = d;
arr[a][c] = pl;
a++;
foreach (int[] ia in arr)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0} ", ia[j]);
if (j == 2) Console.WriteLine();
}
}
ok now, is there any way so that i can cut off the code to be more simple?
AlanPosted Oct 13, 2007, 7:21 PM
When sorting a two-dimensional array, it's generally better to use a jagged array rather than an 'ordinary' 2D array, so that you can swap whole rows in one operation:
int[][] arr = new int [2][];
arr[0] = new int[]{ 100, 3, 2 };
arr[1] = new int[]{ 10, 11, 12 };
int[] temp = null;
if (arr[0][0] > arr[1][0])
{
temp = arr[1];
arr[1] = arr[0];
arr[0] = temp;
}
foreach (int[] ia in arr)
{
for (int j = 0; j < 3; j++ )
{
Console.Write("{0} ", ia[j]);
if (j == 2) Console.WriteLine();
}
}