-
November 8th, 2013, 05:31 PM
#1
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?
-
November 8th, 2013, 05:50 PM
#2
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?
-
November 9th, 2013, 07:13 AM
#3
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.
-
November 9th, 2013, 02:49 PM
#4
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.
-
November 9th, 2013, 03:48 PM
#5
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.
-
November 9th, 2013, 04:59 PM
#6
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
-
November 9th, 2013, 05:19 PM
#7
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|