# ResolvedHow to find the smallest element in a matrix and the row in which that element is located?

#### Sajo

##### Member
Write a C # program that loads the value of manufactured goods in m manufacturing sectors from users over n months and then determines and prints the month in which productivity was lowest."
I realized that productivity is lowest in the month where the value of the goods produced is lowest. My problem is how to write the month where the lowest value is. First I have to find the smallest value of all the elements in the matrix. As you can see, that's what I did. To find the month in which that value is located, I need to find the row in which that value is located. Month=row(Example January is first month and it is first row). Unfortunately I don’t know how to write code for that part. Can you help me?

`````` static void Main(string[] args)
{
Console.WriteLine(" Enter the number of months for which the value of the products is calculated ");
Console.WriteLine(" Enter the number of manufacturing sectors ");
int[,] c = new int[m, p];

Console.WriteLine(" Enter the price for each month in each manufacturing sectors");

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

{

Console.WriteLine("Month {0}:", i + 1);
for (int j = 0; j < m; j++)
{

Console.WriteLine("Manufacturing sector {0}: ", j + 1);

}

}
int small = c[0 ,0];
for (int i = 0; i < n; i++)
{ for(int j = 0; j < m; j++)
{
if (small > c[i,j])
{
small = c[i, j];
}

}

}
Console.WriteLine(small);``````

#### jmcilhinney

##### C# Forum Moderator
Staff member
You simply need to declare a second variable where you declare `small` and set it where you set `small`. You simply assign the current row index (which is presumably `i`) to that variable.

• Skydiver

#### Skydiver

Staff member
Yes. Have `small` and `smallRow`. When you update `small`, also update `smallRow`.

As a quick aside, you don't even need an array unless you later have to display all the data the user originally entered.

#### Sajo

##### Member
I assign that current row index but it is always zero
Here is the code
Find the number of row where is the smallest element:
``````int small = c[0 ,0];
//n is number of months
int[] b = new int[n];
int smallrow = b;

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{

if (small > c[i, j])
{
small = c[i, j];
smallrow = b[i];
}

}

}
Console.WriteLine(smallrow);
Console.WriteLine(small);``````

#### Skydiver

Staff member
The number `i` represents the row number. Why are you taking the value of `b[i]` ?

As a quick aside, in C#, when you allocate a new array like you are doing with `int[] b = new int[n]`, all the elements of the array are initialized to zero. This explains why your `smallrow` is always zero.

#### Sajo

##### Member
But which variable I should use then? And i is for number of months.

#### Skydiver

Staff member
Stop and think about it. If you are storing data from from the first month in `c[0, ...]`, and the second month in `c[1, ...]`, etc. and you are using a variable to go to a particular row, wouldn't that variable tell you which month you are on?

#### Sajo

##### Member
Hahahhahaha I did it. Here is the code. I solved the problem.
Solution of the problem:
`````` int small = c[0 ,0];

int smallmonth = 0;

for (int i = 0; i < n; i++)
{

for (int j = 0; j < m; j++)
{

if (small > c[i, j])
{
small = c[i, j];

smallmonth = i;

}

}

}
Console.WriteLine("The least productive month: {0}. month",(smallmonth+1));
Console.WriteLine("The lowest value produced is {0} \$",small);``````

• Skydiver

#### Skydiver

Staff member
Good job! Great feeling figuring it out on your own, instead of us just giving you the answer right? I call that feeling "hacker's high".

• • Sajo and Sheepings

Replies
4
Views
113
Replies
1
Views
77
Replies
7
Views
104
Replies
2
Views
98
Question Tic-tac toe game
Replies
3
Views
91