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];

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;
}
}
}``````
5
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by ddanbe

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)

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 :)

Edited by Medalgod: n/a

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.

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 :)

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.