Hello

Help is appreciated in the following code. I am trying to multiply the first array ,collected from random numbers, by two.

When i run the code below:I get only the last number of the first array multiplied by two

using System;

public class Matrix
{
    Random randomNumbers = new Random(); // random number generator

    int matrix = 0;
    int matrixTimesTwo;
    int[] Array;

    public void loadArray()
    {

        Array = new int[60];
        Console.WriteLine("Load the Matrix");

        for (int counter = 1; counter <= Array.Length; counter++)
        {
            matrix = randomNumbers.Next(1, 100);
            Console.Write("{0}  ", matrix); // display generated value
            if (counter % 10 == 0)
                Console.WriteLine();
        }
    }

    public void doubleArray()
    {

        Array = new int[60];

        Console.WriteLine("Load the MatrixTimes Two");

        for (int counter = 1; counter <= Array.Length; counter++)
        {
            matrixTimesTwo = matrix * 2;
            Console.Write("{0}  ", matrixTimesTwo); // display generated value
            if (counter % 10 == 0)
                Console.WriteLine();
        }
    }
}







public class TheMatrix
{
    public static void Main(string[] args)
    {
        Matrix myMatrix = new Matrix();
        myMatrix.loadArray();
        myMatrix.doubleArray();
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi Ceerno,

Sorry to say but there are a number of problems with your code.

The first thing I noticed was that your for-loops run from index 1 up to and including the Array length. Arrays are indexed from 0, so it should run from 0 to (but not including) the Array length (use strictly < rather than <= in your for-loop).

Also, you never actually set or read any values in your array, you simply read and set the int matrix variable. Even if you were setting the values in the array in your first method you completely wipe the array clear at the start of your second method.

In your second loop you constantly read the matrix variable without updating it, so you are reading the same value over and over again.

Hope this gives you some idea of how to continue.

Thanks for the input. I will work on it and see what i can come up with

This article has been dead for over six months. Start a new discussion instead.