Need someone to check my code. I dont get why I got an indexoutofrange error in my code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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(); } } }

15th Jul 2020, 2:01 AM
Francis
9 Answers
+ 1
I made such a stupid mistake. Thank you very much for the patience.
15th Jul 2020, 5:04 AM
Francis
+ 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.
15th Jul 2020, 9:40 PM
Francis
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.
15th Jul 2020, 3:06 AM
Francis
0
Francis change the line to "if(cs[0][i] < minimums" and see if it works
15th Jul 2020, 3:29 AM
Bot
Bot - avatar
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.
15th Jul 2020, 3:59 AM
Francis
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(); } } }
15th Jul 2020, 4:53 AM
Francis
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?
15th Jul 2020, 7:18 AM
Francis
0
I changed {1,8} to {1,2}. Answer should be minimum is 2 and maximum is 5, but it returned 2 and 2.
15th Jul 2020, 9:26 AM
Francis
0
Pls wat editor 4 html
16th Jul 2020, 9:14 PM
Damilola sulaiman