title: "Index was outside the bounds of the array" error in multiply 2 matrices
body: <p>When I run the script, I get the "Index was outside the bounds of the array" error at line <code>float[] i_f = b[itemid];</code>. </p>
  
  <p>BTW, I changed </p>
  
  <p><code>for (int y = 0; y &lt;= 114360000; y++)</code> to <code>for (int y = 0; y &lt; 114360000; y++)</code> and also</p>
  
  <p><code>for (int x = 0; x &lt;= 8939500; x++)</code> to <code>for (int x = 0; x &lt; 8939500; x++)</code> </p>
  
  <p>but it shows the same error again.</p>
  
  <p>FYI: This code has two parts:</p>
  
  <p>First part that get 100 values from 2 matrices and multiply them (matrix multiply) which works correctly.</p>
  
  <p>Second part that repeats the first part for 1143600 times (The error happens when I run this part).</p>
  
  <pre><code>namespace function
  {
      public partial class Form1 : Form
      {
          //Hashtable hashtable = new Hashtable();
          float userscore, itemscore,result;
          string lineitem, lineuser;
          //float[][] a = new float[89395][100];
          //float[][] b = new float[1143600][100];
          float[][] a = Enumerable.Range(0, 89395).Select(i =&gt; new float[100]).ToArray();
          float[][] b = Enumerable.Range(0, 1143600).Select(j =&gt; new float[100]).ToArray();
          //float[,] c = new float[89395, 100];
          StreamReader fileitem = new StreamReader("c:\\p.txt");
          StreamReader fileuser = new StreamReader("c:\\L.txt");
          public Form1()
          {
              InitializeComponent();
              for (int x = 0; x &lt;= 8939500; x++)
              {
                  lineuser = fileuser.ReadLine();
                  if (!string.IsNullOrEmpty(lineuser))
                  {
                      string[] values = lineuser.Split(' ');
                      int userid, factoriduser;
                      foreach (string value in values)
                      {
                          userid = Convert.ToInt32(values[0]);
                          factoriduser = Convert.ToInt32(values[1]);
                          userscore = Convert.ToSingle(values[2]);
                          a[userid][factoriduser] = userscore;
                      }
                  }
              }
  
              for (int y = 0; y &lt;= 114360000; y++)
              {
                  lineitem = fileitem.ReadLine();
                  if (!string.IsNullOrEmpty(lineitem))
                  {
                      string[] valuesi = lineitem.Split(' ');
                      int itemid, factoriditem;
                      foreach (string value in valuesi)
                      {
                          itemid = Convert.ToInt32(valuesi[0]);
                          factoriditem = Convert.ToInt32(valuesi[1]);
                          itemscore = Convert.ToSingle(valuesi[2]);
                          b[itemid][factoriditem] = itemscore;
                      }
                  }
  
              }
  
          }
          public float dotproduct(int userid, int itemid)
          {
  
              //get the score of 100 from user and item to dotproduct
              //get userid and itemid element from array a and b
              float[] u_f = a[userid];
              float[] i_f = b[itemid];   //&lt;-----error happens
  
              for (int i = 0; i &lt; u_f.Length; i++)
              {
                  //  result += u_f[userid] * i_f[itemid];
                  result += u_f[i] * i_f[i];
  
              }
              return result;
  
          }
  
          private void btn_recomm_Click(object sender, EventArgs e)
          {
             // if (txtbx_id.Text ==null)
              if (String.IsNullOrEmpty(txtbx_id.Text))
              {
                  MessageBox.Show("please insert user id");
              }
              //if (txtbx_id.Text != null &amp;&amp; txtbx_itemid == null)
                  if (!String.IsNullOrEmpty(txtbx_id.Text) &amp;&amp; String.IsNullOrEmpty(txtbx_itemid.Text))
              {
                  int sc = Convert.ToInt32(txtbx_id.Text);
  
  
                  if (sc &gt;= 0 &amp;&amp; sc &lt;= 89395)
                  {
                      //for (int z = 0; z &lt;= 1143600; z++)
                      //{
                        //  dotproduct(sc, z);
                         // hashtable.Add(z, result);
                      //}
                      //SortedDictionary&lt;int, float&gt; dict = new SortedDictionary&lt;int, float&gt;(hashtable);
                      //foreach (int key in dict)
                      //{
                        //  System.Console.WriteLine(String.Format("{0}: {1}", key, dict[key]));
                      //}
                      var results = new List&lt;float&gt;(1143600);
                      for (int z = 0; z &lt;= 1143600; z++)
                      {
                          results.Add(dotproduct(sc, z));
                      }
                      foreach (var resultwithindex in results.Select((r, index) =&gt; new { result = r, Index = index }).OrderByDescending(r =&gt; r.result).Take(10))
                      {
                          System.Console.WriteLine(string.Format("{0}: {1}", resultwithindex.Index, resultwithindex.result));
                      }
                      }
  
  
  
              }
              //if (txtbx_id!=null &amp;&amp; txtbx_itemid!=null)
              if (!String.IsNullOrEmpty(txtbx_id.Text) &amp;&amp; !String.IsNullOrEmpty(txtbx_itemid.Text))
              {
                  int uid = Convert.ToInt32(txtbx_id.Text);
                  // int uid;
                  // if (!Int32.TryParse(txtbx_id.Text, out uid))
                  // {
                  // MessageBox.Show("Try again");
                  // }
  
                  int iid = Convert.ToInt32(txtbx_itemid.Text);
                  // int iid;
                  //if (!Int32.TryParse(txtbx_id.Text, out iid))
                  //{
                  //MessageBox.Show("Try again");
                  //}
                  //{
                  if (uid &gt;= 0 &amp;&amp; uid &lt;= 89395 &amp;&amp; iid &gt;= 0 &amp;&amp; iid &lt;= 1143600)
                  {
                      dotproduct(uid, iid);
                      MessageBox.Show("The Score of item id " + iid + " is " + result);
                  }
              }
          }
  </code></pre>