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?
Printable View
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?
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?
This works for every columnCode://.....
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;
}
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;
}
}
Why? It's not c++, it's c#, an advanced object-oriented language.
Anyway...
These are the minimum numbers of each oneCode: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;
}
"1
7
0"
as you can see
you mean something different?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
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;
}
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;
}