Finding Lowest Value Of Every Column In A 2d Array
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Finding Lowest Value Of Every Column In A 2d Array

1. Junior Member
Join Date
Sep 2013
Posts
5

## Finding Lowest Value Of Every Column In A 2d Array

My array is such: (rows = 3, columns =7)

1 2 3 4 5 6 7
8 9 10 9 8 7 11
5 4 4 2 1 0 6

How can i make a method which displays the lowest value of every column while telling what row it is in?

2. Junior Member
Join Date
Sep 2013
Posts
5

## Re: Finding Lowest Value Of Every Column In A 2d Array

This method will display the lowest of the whole array:

public static void findlowest(int[,] Data)
{
int min = Data[0, 0];
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 7; c++)
{
if (Data[r, c] < min)
{
min = Data[r, c];
}
}
}

Could i use that to get the three lowest values then compare those?

3. Junior Member
Join Date
Oct 2013
Posts
16

## Re: Finding Lowest Value Of Every Column In A 2d Array

Code:
```//.....

int[,] lol = new int[3, 7]{
{1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 9, 8, 7, 11},
{5, 4, 4, 2, 1, 0, 6}};

int[] min = Min2d(lol);

foreach (int x in min)
Console.WriteLine(x);
}

static int[] Min2d(int[,] a)
{
int[] min = new int[a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
int[] c = new int[a.GetLength(1)];
for (int x = 0; x < a.GetLength(1); x++)
c[x] = a[i, x];
min[i] = c.Min();
}
return min;
}```
This works for every column
Last edited by Reroto; November 9th, 2013 at 07:16 AM.

4. Junior Member
Join Date
Sep 2013
Posts
5

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by Reroto
Code:
```//.....

int[,] lol = new int[3, 7]{
{1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 9, 8, 7, 11},
{5, 4, 4, 2, 1, 0, 6}};

int[] min = Min2d(lol);

foreach (int x in min)
Console.WriteLine(x);
}

static int[] Min2d(int[,] a)
{
int[] min = new int[a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
int[] c = new int[a.GetLength(1)];
for (int x = 0; x < a.GetLength(1); x++)
c[x] = a[i, x];
min[i] = c.Min();
}
return min;
}```
This works for every column
How could i do that without using system.linq? We are not allowed to use it.

Also, when i tried linq, all i got was
"1
7
0"

Here is what i did:
Code:
```using System;
using System.Linq;

public class Program
{
static void Main(string[] args)
{
int[,] Data = { { 1, 2, 3, 4, 5, 6, 7, }, { 8, 9, 10, 9, 8, 7, 11 }, { 5, 4, 4, 2, 1, 0, 6 } };

findlowest(Data);

findaverage(Data);

int[] min = Min2d(Data);

foreach (int x in min)
Console.WriteLine(x);

Console.ReadLine();
}

public static void findlowest(int[,] Data)
{
int mineatten = Data[0, 0];
int r;
for (r = 0; r < 3; r++)
{
for (int c = 0; c < 7; c++)
{
if (Data[r, c] < mineatten)
{
mineatten = Data[r, c];
}
}
}

//writeline
}

public static void findaverage(int[,] Data)
{
int sum = 0;

for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 7; c++)
{
sum += Data[r, c];

}
}

//writeline
}

static int[]Min2d(int[,] a)
{
int Min;
int[] min = new int[a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
int[] c = new int[a.GetLength(1)];
for (int x = 0; x < a.GetLength(1); x++)
c[x] = a[i, x];
min[i] = c.Min();
}
return min;
}
}```
Last edited by wildcat1337; November 9th, 2013 at 02:59 PM.

5. Junior Member
Join Date
Oct 2013
Posts
16

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by wildcat1337
How could i do that without using system.linq? We are not allowed to use it.
Why? It's not c++, it's c#, an advanced object-oriented language.

Anyway...
Code:
```static int[] Min2d(int[,] a)
{
int[] min = new int[a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
int[] c = new int[a.GetLength(1)];

for (int x = 0; x < a.GetLength(1); x++)
c[x] = a[i, x];

int mi = c[0];
foreach (int x in c)
mi = x < mi ? x : mi;

min[i] = mi;
}
return min;
}```
These are the minimum numbers of each one
"1
7
0"

as you can see
Code:
```{1, 2, 3, 4, 5, 6, 7}   --> 1
{8, 9, 10, 9, 8, 7, 11} --> 7
{5, 4, 4, 2, 1, 0, 6}   --> 0```
you mean something different?
Last edited by Reroto; November 9th, 2013 at 03:54 PM.

6. Junior Member
Join Date
Sep 2013
Posts
5

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by Reroto
Why? It's not c++, it's c#, an advanced object-oriented language.

Anyway...
Code:
```static int[] Min2d(int[,] a)
{
int[] min = new int[a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
int[] c = new int[a.GetLength(1)];

for (int x = 0; x < a.GetLength(1); x++)
c[x] = a[i, x];

int mi = c[0];
foreach (int x in c)
mi = x < mi ? x : mi;

min[i] = mi;
}
return min;
}```
These are the minimum numbers of each one
"1
7
0"

as you can see
Code:
```{1, 2, 3, 4, 5, 6, 7}   --> 1
{8, 9, 10, 9, 8, 7, 11} --> 7
{5, 4, 4, 2, 1, 0, 6}   --> 0```
you mean something different?
our professor will not allow us to use linq. Part of the reason I have a hard time figuring it out as Im fairly new to programming.

Your code also displays the lowest value of each row, I need to find each lowest of each column along with what row has it, should display this:

1 from row 1
2 from row 1
3 from row 1
2 from row 3
1 from row 3
0 from row 3
6 from row 3

7. Junior Member
Join Date
Oct 2013
Posts
16

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by wildcat1337
our professor will not allow us to use linq. Part of the reason I have a hard time figuring it out as Im fairly new to programming.

Your code also displays the lowest value of each row, I need to find each lowest of each column along with what row has it, should display this:

1 from row 1
2 from row 1
3 from row 1
2 from row 3
1 from row 3
0 from row 3
6 from row 3
That's it
Code:
```static int[] Min2d(int[,] a)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
int[] min = new int[columns];

for (int i = 0; i < columns; i++)
{
int[] nums = new int[rows];

for (int x = 0; x < rows; x++)
nums[x] = a[x, i];

int mi = nums[0];
foreach (int x in nums)
mi = x < mi ? x : mi;

min[i] = mi;
}

return min;
}```

8. Junior Member
Join Date
Sep 2013
Posts
5

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by Reroto
That's it
Code:
```static int[] Min2d(int[,] a)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
int[] min = new int[columns];

for (int i = 0; i < columns; i++)
{
int[] nums = new int[rows];

for (int x = 0; x < rows; x++)
nums[x] = a[x, i];

int mi = nums[0];
foreach (int x in nums)
mi = x < mi ? x : mi;

min[i] = mi;
}

return min;
}```
I can display the min value, but how to display the row it is in?

9. Junior Member
Join Date
Oct 2013
Posts
16

## Re: Finding Lowest Value Of Every Column In A 2d Array

Originally Posted by wildcat1337
I can display the min value, but how to display the row it is in?
Ultimate edition xD
Code:
```       public static void Main()
{
int[,] lol = new int[3, 7]{
{1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 9, 8, 7, 11},
{5, 4, 4, 2, 1, 0, 6}};

int[,] min = Min2d(lol);

for (int i = 0; i < min.GetLength(1); i++)
Console.WriteLine("Row: {0}, Column: {1}, Value: {2}", min[0, i], i + 1, min[1, i]);

Console.ReadKey();
}

static int[,] Min2d(int[,] a)
{
int rows = a.GetLength(0);
int columns = a.GetLength(1);
int[,] min = new int[2,columns];

for (int i = 0; i < columns; i++)
{
int[] nums = new int[rows];

for (int x = 0; x < rows; x++)
nums[x] = a[x, i];

int row = 0;
for (int s = 0, mi=nums[0]; s < rows; s++)
{
if (nums[s] < mi)
{
mi = nums[s];
row = s;
}
}

min[0, i] = row+1;
min[1, i] = nums[row];
}

return min;
}```
Last edited by Reroto; November 9th, 2013 at 10:09 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

This a Codeguru.com survey!