Need someone to check my code. I dont get why I got an indexoutofrange error in my code. | SoloLearn: Learn to code for FREE!

+1

# Need someone to check my code. I dont get why I got an indexoutofrange error in my code.

I am trying to write a program that finds the minimum of the 2nd element of a two dimensional array, but I get an indexoutofrange error. Code below: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double[,] cs = { { 1, 10 }, { 2, 9 }, { 3, 8 }, { 4, 7 }, { 5, 6 }, { 6, 5 }, { 7, 6 }, { 8, 7 }, { 9, 8 }, { 10, 9 }, { 11, 10 }, { 12, 11 } }; // Determine min first double minimums = 9999; for (int i = 0; i <= cs.Length; i++) { if (cs[0, i] < minimums) { minimums = cs[0, i]; } else { } } Console.WriteLine(minimums); Console.ReadLine(); } } }

7/15/2020 2:01:36 AM

Francis

+1

Francis https://code.sololearn.com/c8JBXf42qP9u/?ref=app

+1

Francis Proলয় मिश्ra for (int i = 0; i <cs.GetLength(0); i++) { if (cs[i, 0] < minimums) { minimums = cs[i, 0]; } }

+1

I made such a stupid mistake. Thank you very much for the patience.

+1

It seems that the location of the variable location_of_min matters. The computer said it didn't exist in the current context because it was declare inside the first for loop. @Rithea Thanks again.

+1

Francis not only that. You have to take care of comparison operator either. If you use '<' in 1st loop, you will get wrong result in 2nd loop. so, in 1st loop you have to use '<=' .

0

i<cs.Length cs[i,0] but however, idk what is your real purpose to use this loop

0

Tried that but didn't work. Visual studio is pointing to this line if (cs[0, i] < minimums) Im not sure whats wrong with it.

0

In order to loop through 2D array in C#. you cannot use array.Length but instead use array.GetLength() array.GetLength(0) // is for row array.GetLength(1) // is for column

0

Francis change the line to "if(cs[0][i] < minimums" and see if it works

0

I created a two dimensional array (2 rows and 2 columns). I want to find the lowest number among the 2nd column ( referring to the 10,9,8,7,6,5, and so on). The answer is suppose to be 5. I realized cs.Length is wrong because it counts all the elements so it returns 24.

0

Francis but in your code, it is 2d array with 12 rows and 2 columns

0

Here I changed my code just so we can keep our mind of length of the index. There are 5 rows, but I set the condition to i<3 (i=0) which means it will not perform the 5th statement on the row. However, I still get the same error. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double[,] cs = { { 1, 8 }, { 2, 5 }, { 3, 2 }, { 4, 4 }, { 5, 9 }}; // Determine min first double minimums = 9999; for (int i = 0; i <3; i++) { if (cs[0, i] < minimums) { minimums = cs[0, i]; } else { } } Console.WriteLine(minimums); Console.ReadLine(); } } }

0

Francis Second column is at index 1 not 0. cs[i,1] Or cs[i,cs.GetLength(1)-1]

0

After finding the minimum, I want to find the maximum, but not the maximum on the second column(not the minimum between 3,5,6,2,5,3), just the maximum between the first number until the location of the minimum(minimum between 3,5,6,2) on the second column. The maximum is suppose to be 6. It works if I use this. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double[,]list = { {0,3}, {1,5}, {2,6}, {3,2}, {4,5}, {5,3}}; double current_min= 9999; for (int i = 0; i < (list.Length/2); i++) { if (list[i, 1] < current_min) { current_min = list[i, 1]; int location_of_min = i; } } double max_before_current_min = -1; for (int j = 0; j < 3; j++) { if (list[j, 1] > max_before_current_min) { max_before_current_min = list[j, 1]; } } Console.WriteLine(current_min); Console.WriteLine(max_before_current_min); } } } However, lets say I don't know where the location of the minimum is. Instead of using for (int j = 0; j < 3; j++), I will use this (int j = 0; j < location_of_min; j++). When I use the latter, it says location_of_min doesn't exist in the current context. How do I do it?

0

Francis you don't need to use many loops like that. Just put one more condition for maximum value inside the condition of minimum value. You can check my code again. I have updated it according to your next issue.

0

I changed {1,8} to {1,2}. Answer should be minimum is 2 and maximum is 5, but it returned 2 and 2.

0

Francis I got your point now, let me check.

0

Francis check it again

0

Pls wat editor 4 html