Resolved How to find the smallest element in a matrix and the row in which that element is located?

Sajo

Member
Joined
Jul 22, 2020
Messages
17
Programming Experience
Beginner
Hello everybody. I have problem with the task. The task is:"
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?

Code for this task:
 static void Main(string[] args)
        {
            Console.WriteLine(" Enter the number of months for which the value of the products is calculated ");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine(" Enter the number of manufacturing sectors ");
            int m = int.Parse(Console.ReadLine());
            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);
                    c[i, j] = int.Parse(Console.ReadLine());
                                                                                    
                }

                

            }
            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);
 
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.
 
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.
 
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[0];

            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);
 
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.
 
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?
 
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);
 
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".
 
Back
Top Bottom