none
2D Arrays RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using TestLibary;

    namespace ICA25
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sTitle;
                bool bExit = false;

                sTitle = ("ICA 25 - Times table");
                Console.WriteLine("{0,60}\n", sTitle);


                do
                {
                    {
                        int[,] iFinal;
                        int iNum = GetValue("Please enter the size (4-20) of the times table: ", 4, 20);

                        Console.Write("Here is the times table for that size: \n");
                        iFinal = GenerateTT(iNum);
                        DisplayTT(iFinal);
                    }
                }
                while ((YesNo("Would you like to do another? Y or N:") == "Y") && !bExit);
                bExit = true;
            }


            static public int GetValue(string s, int iMin, int iMax)
            {
                int iNum;
                Utilities.GetValue(out iNum, s, 4, 20);
                return iNum;
            }


            static public int[,] GenerateTT(int iValue)
            {
                int[,] iNum = new int[iValue, iValue];

                for (int iRow = 0; iRow < iNum.GetLength(0); iRow++)
                {
                    for (int iCol = 0; iCol < iNum.GetLength(1); iCol++)
                    {
                        iNum[iRow, iCol] = (iCol) * (iRow);
                    }
                }
                return iNum;
            }

            static public void DisplayTT(int[,] iFinal)
                
            {
                for (int iRow = 0; iRow < iFinal.GetLength(0); iRow++)
                {
                    {
                        for (int iCol = 0; iCol < iFinal.GetLength(1); iCol++)
                        {
                            Console.Write( iFinal[iRow, iCol]);
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                }
            }

            static public string YesNo(string sPrompt)
            {
                string stemp;

                do
                {
                    Console.Write("{0}", sPrompt);
                    stemp = Console.ReadLine().ToString().ToUpper();
                    if (stemp == "N")
                    {
                        Console.Write("Bye");
                        Console.ReadLine();

                    }
                    if (stemp != "Y" && stemp != "N")
                    {
                        Console.Write("\nPlease answer Y or N.");
                    }
                }
                while ((stemp != "Y") && (stemp != "N"));
                return stemp;
            }

        }
    }
    Thursday, October 10, 2019 8:38 PM

All replies

  • Cant get it to be in a times table format (ex. 1   2   3  4 

                                                                     4   5   6  7 

                                                                     9  10 11 12 )

    Thursday, October 10, 2019 8:43 PM
  • Cant get it to be in a times table format (ex. 1   2   3  4 

                                                                     4   5   6  7 

                                                                     9  10 11 12 )

    Maybe if you move the WriteLine outside of the inner loop:

    static public void DisplayTT(int[,] iFinal)
    {
        for (int iRow = 0; iRow < iFinal.GetLength(0); iRow++)
        {
            {
                for (int iCol = 0; iCol < iFinal.GetLength(1); iCol++)
                {
                    Console.Write(iFinal[iRow, iCol]);
                    //{
                        //Console.WriteLine();
                    //}
                }
                Console.WriteLine(); // <<<
            }
        }
    }
    

    - Wayne

    Friday, October 11, 2019 1:17 AM
  • Better formatting of the output might also require some additional spacing.
    For example:

    static public void DisplayTT(int[,] iFinal)
    {
        for (int iRow = 0; iRow < iFinal.GetLength(0); iRow++)
        {
                for (int iCol = 0; iCol < iFinal.GetLength(1); iCol++)
                {
                    Console.Write(iFinal[iRow, iCol]);
                    Console.Write('\t');
                }
                Console.WriteLine();
            }
        }
    }
    

    - Wayne

    Friday, October 11, 2019 1:56 AM
  • Cant get it to be in a times table format (ex. 1   2   3  4 

                                                                     4   5   6  7 

                                                                     9  10 11 12 )

    Further formatting suggestion: To get up to 20 values adequately spaced on an
    80-character display - which is the default for a console window - try setting
    the string widths.

    static public void DisplayTT(int[,] iFinal)
    {
        for (int iRow = 0; iRow < iFinal.GetLength(0); iRow++)
        {
                for (int iCol = 0; iCol < iFinal.GetLength(1); iCol++)
                {
                    Console.Write("{0,4}", iFinal[iRow, iCol]);                
                }
                Console.WriteLine();
            }
        }
    }
    

    - Wayne

    Friday, October 11, 2019 2:15 AM
  • Yet another refinement. If you want to avoid an automatic newline at the end
    causing blank lines between each line of numbers, reduce the width by one of 
    the first value (string) only:

    static public void DisplayTT(int[,] iFinal)
    {
        for (int iRow = 0; iRow < iFinal.GetLength(0); iRow++)
        {
                for (int iCol = 0; iCol < iFinal.GetLength(1); iCol++)
                {
                    if(iCol == 0) Console.Write("{0,3}", iFinal[iRow, iCol]);
                    else Console.Write("{0,4}", iFinal[iRow, iCol]);
                }
                Console.WriteLine();
            }
        }
    }
    

    - Wayne

    Friday, October 11, 2019 2:34 AM
  • Hi Lil_Jokez,

    Thank you for posting here.

    First, I think your problem is not clearly.

    Second, the code your provided is not completed. 

    Therefore, I hope that you could tell me what you want to do clearly and provide a completed code.

    We are waiting for your update.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 11, 2019 5:24 AM
    Moderator