954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

matrix array

Hello, I'm confused on how to put the Random.Next() method into my matrix array. First my matrix array is already messed up, I'm trying to create a 6 by 10 matrix of random numbers and then show the numbers times two. here is the code i've already done, ps my code for some reason keeps shutting down my computer as well. this is for an online class so i'm new at learning this. any help would be awesome. thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace week0801
{
   class Program
   {
      static void Main(string[] args)
      {
         int[,] firstArray;

         firstArray = new int[6, 10];

         Console.WriteLine("Load the Matrix");
         for (int i = 0; i < firstArray.Length; i++)
            Console.WriteLine("{0} ", firstArray[i, i]);

         FirstDouble(firstArray);

         Console.WriteLine();
         Console.WriteLine("Matrix times two");
         for (int i = 0; i < firstArray.Length; i++)
            Console.Write("{0} ", firstArray[i, i]);
      }
      public static void FirstDouble(int[,] array)
      {
         for (int i = 0; i < array.Length; i++)
            array[i, i] *= 2;
      }
   }
}
rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
ps my code for some reason keeps shutting down my computer as well.

Wow that's not supposed to happen. I'd look to other causes unless you mean that your VC# crashes and takes down your PC.

To give you a good clue, here is the proper way to address your for loop:

for (int i = 0; i <= firstArray.GetUpperBound(0); i++)
                for (int j = 0; j <= firstArray.GetUpperBound(1); j++)
                    firstArray[i, j] = ??;

I'll let you fill in the stuff for Random yourself (I can't have all the fun!). You were stepping along the array's "diagonal" (really only covering 6 points since it's not a square matrix)

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Just a fix on jonsca's code:

for (int i = 0; i < firstArray.GetUpperBound(0); i++)
                for (int j = 0; j < firstArray.GetUpperBound(1); j++)
                    firstArray[i, j] = ??;

The less than or equal to comparitor should just be less than, else you'll be reading/writing outside of the arrays bounds :)

Medalgod
Light Poster
40 posts since Dec 2009
Reputation Points: 23
Solved Threads: 5
 

Just a fix on jonsca's code:

for (int i = 0; i < firstArray.GetUpperBound(0); i++)
                for (int j = 0; j < firstArray.GetUpperBound(1); j++)
                    firstArray[i, j] = ??;

The less than or equal to comparitor should just be less than, else you'll be reading/writing outside of the arrays bounds :)

Not true, Array.Length returns the number of elements in the array and thus you should use the less than. Array.GetUpperBounds() returns the highest index of the array, so you should use less than or equal. If you make the change you suggest you would skip the last element.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 
Not true, Array.Length returns the number of elements in the array and thus you should use the less than. Array.GetUpperBounds() returns the highest index of the array, so you should use less than or equal. If you make the change you suggest you would skip the last element.


Sorry, I don't know what i must of done. Copied it into C#VS IDE & got an arror when I first tried to run it. This fixed it... with heinsight Im not entirely sure what i did. Sorry for that :)

Medalgod
Light Poster
40 posts since Dec 2009
Reputation Points: 23
Solved Threads: 5
 

At the end of your code you have to use something like

Console.ReadKey(); //keep console on screen in debug mode

firstArray.Length is equal to 60 (6x10) so is not strange you would get an index out of bounds exception.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: