lundi 20 avril 2015

create 2-D Array, Populating it with random numbers and then adding and printing Rows and Columns

This program is supposed to create a 2-D filled with Random numbers then it prompts the user to choose a number after that it searches the array for the chosen number. I have not completed the program as you can see by the code by I have questions about the methods that I do have done.

FillArray() - This method I have a nested for loop and I am wondering If I have taken the right approach to coding this method am I supposed to make the random numbers equal to the rows and the columns and then return it in the parameter like I did or is there a more efficient way?

SumRows() - This method is supposed to have a two-dimensional array within the parameter and the method will go through the array row by row and determine the sum of each column one at a time. I again used a nested for loop for this method. Within the first for loop I used the rows for the condition and then I used the Columns in the nested for loop. Am I supposed to still type Console.Write("{0}", randomPrintArray[r, c]); like so? or am I supposed to put the columns before the rows.

SumCols() - This method is supposed to aslo have a two-dimensional array within the parameter and add the columns row by row one at a time. I have the same question for this method as I do in the sumRows() method and am I right by resetting the rowSum variable everytime the loop runs into the nested for loop?

SumRows() - This is my biggest question which brought me here initially. I can't figure out how to add the Arrays together and return that value. As you can see I have a sumOfAll variable that is supposed to represent the sum of the arrays each iteration of the loop and add it to itself. I do not know how to code this correctly.

Please Help!!! Any help on any of these questions would be MUCH appreciated!!!

Here is my code:

class Program
{
    static void Main(string[] args)
    {
        string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        string[,] randomNumArray = new string[3, 5];


    }

    public static void FillArray(int[,] randomNumbersArray)
    {
        Random num = new Random();
        for (int r = 0; r < randomNumbersArray.GetLength(0); r++)
        {
            for (int c = 0; c < randomNumbersArray.GetLength(1); c++)
            {
                randomNumbersArray[r, c] = num.Next(15, 97);
            }
        }
    }

    public static void PrintArray(int[,] randomPrintArray)
    {
        for (int r = 0; r < randomPrintArray.GetLength(0); r++)
        {
            for (int c = 0; c < randomPrintArray.GetLength(1); r++)
            {
                Console.Write("{0}", randomPrintArray[r, c]);
            }
            Console.WriteLine();
        }
    }

    public static void SumRows(int[,] sumOfRowsArray)
    {
        int rowSum;
        for (int r = 0; r < sumOfRowsArray.GetLength(0); r++)
        {
            rowSum = 0;
            for (int c = 0; c < sumOfRowsArray.GetLength(1); c++)
            {
                rowSum += sumOfRowsArray[r, c];
            }
        }
    }

    public static void SumCols(int[,] sumOfColsArray)
    {
        int colsSum;
        for (int r = 0; r < sumOfColsArray.GetLength(1); r++)
        {
            colsSum = 0;

            for (int c = 0; c < sumOfColsArray.GetLength(0); c++)
            {
                colsSum += sumOfColsArray[r, c];
            }
        }
    }

    public static void SumArray(int[,] sumOfAllArray)
    {
        int sumOfAll;
        for (int r = 0; r < sumOfAllArray.GetLength(0); r++)
        {
            sumOfAll += sumOfAll;
            for (int c = 0; c < sumOfAllArray.GetLength(1); c++)
            {
                sumOfAll += sumOfAllArray[r, c];
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire