Need beginner help please! two dimensional array code help

Dec 9 2007 5:30 PM

O.k. so I'm trying to finish up my first semester of programing I C#.  This is the last program I have to write before the final.  I cannot for the life of me figure out what I'm doing wrong.  Here is the deal.....We have a given array and we have to write a method to find the largest number in the array and a method the smallest number in the array.  We then have to write a method to swap the largest and the smallest numbers in the array and then display the array(which should show the numbers swapped).  Our swap method has to have 5 arguments.  My program is finding the correct highest number and smallest number but the swap is giving me trouble.  I don't know if I am passing the arguments incorrectly or not.  If you can help, it would be much appreciate as I am about to pull all my hair out!  Remember, I've only had one semester so I have to work within the parameters that I have learned .  Here is my program below (I"m using visual studio 2005).  My Loadarray is not working right now either so I just commented it out and loaded the array manually.

namespace Program11

{

class Program

{

const string INPUT_FILE_NAME = "C:\\Users\\Owner\\Prog12Dat.Txt";

static StreamReader fileIn;

const int ROWS = 6;

const int COLUMNS = 5;

static void Main()

{

int k, l, m, n;

int[,] numbers = new int[,]{{25,66,95,32,98},

{44,75,42, 115, 98},

{55,33,77,26,89},

{51,2,66,23,82},

{66,41,58,27,32},

{54,62,28,94,3} };

//ConIO.ClrScr();

//OpenFiles();

//LoadArr(numbers);

FindMax(numbers, out m,out n);

FindMin(numbers, out k, out l);

SwapLargeSmall (numbers, ref m,ref n, ref k,ref l);

DisplayArray(numbers);

//CloseFiles();

}

static void OpenFiles()

{

if (File.Exists(INPUT_FILE_NAME))

{

fileIn = File.OpenText(INPUT_FILE_NAME);

Console.WriteLine("{0} was opened", INPUT_FILE_NAME);

}

else

{

Console.WriteLine("Error: {0} does not exist\n", INPUT_FILE_NAME);

//ConIO.Exit();

}

}

static void LoadArr( int [,] nums)

{

string lineIn;

while ((lineIn = fileIn.ReadLine())!= null)

{

for (int i = 0; i < ROWS; i++)

for (int j = 0; j < COLUMNS; j++)

nums[i, j] = Int32.Parse(lineIn);

}

}

static void FindMax(int [,] nums, out int m, out int n)

{

int max,i,j;

i = 0;

j = 0;

max = nums[i, j];

m = 0;

n = 0;

for (i = 0; i < ROWS; i++)

for (j = 1; j < COLUMNS; j++)

{

if (nums[i, j] > max)

max = nums[i, j];

}

nums[m, n] = max;

Console.WriteLine("Max number is {0}",nums[m, n]);

}

static void FindMin(int [,] nums, out int k, out int l)

{

int min;

int i, j;

i = 0;

j = 0;

min = nums[i, j];

k = 0;

l=0;

for(i=0;i<ROWS;i++)

for (j = 1; j < COLUMNS; j++)

{

if (nums[i, j] < min)

min= nums[i, j];

}

nums[k,l] = min;

Console.WriteLine("Min. number is {0}", nums[k,l]);

}

static void SwapLargeSmall(int[,]nums,ref int m, ref int n, ref int k,

ref int l)

{

int tempvalue;

Console.WriteLine("{0} is min number and {1} is max number", nums[k,l], nums[m,n]);

tempvalue = nums[m, n];

nums[m,n] = nums[k, l];

nums[k, l] = tempvalue;

Console.WriteLine("{0} {1} {2}", nums[m,n], nums[k,l], tempvalue);

}

static void DisplayArray(int [,] nums)

{

for (int i = 0; i < ROWS; i++)

{

int j = 0;

Console.WriteLine("\n{0,5} {1,5} {2,5} {3,5} {4,5}", nums[i, j], nums[i, j + 1], +

nums[i, j + 2], nums[i, j + 3], nums[i, j + 4]);

}

}

static void CloseFiles()

{

fileIn.Close();

}

}

}


Answers (2)