C# exercises and solutions-C# array: matrix


C# array exercise: matrix

In this C# exercise, you are about to write C# program to display a matrix as shown below. The diagonal of the matrix fills with 0s. The lower side fills will -1s and the upper side fills with 1s.


01 11 1 
-10 11 1
-1-1 01 1
-1-1 -10 1
-1-1 -1-1 0
 

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace ConsoleApplication1

{

   

    class Program

    {

        static void Main(string[] args)

        {

 

            printMatrix();

            Console.ReadLine();

 

        }

 

 

        public static void printMatrix()

        {

 

            int[,] matrix = new int[5, 5];

            int i, j;

            for (i = 0; i < 5; i++) //assign values to the matrix

                for (j = 0; j < 5; j++)

                { //if row=column=> fill the matrix with 0

                    if (i == j) matrix[i, j] = 0;//if row>columns=> fill matrix with -1

                    else if (i > j) matrix[i, j] = -1;//if row<columns=> fill matrix with 1

                    else matrix[i, j] = 1;

                }

 

            for (i = 0; i < 5; i++)

            { //print the matrix

                for (j = 0; j < 5; j++)

                    Console.Write("{0}\t", matrix[i, j]);

                Console.WriteLine();

            }

 

        }

  }

}


Comments

Ned comment

 Ned

This quote from Optimus Rhyme pretty much sums up this site: "generally not good training for newbies."


2016-12-10
Optimus Rhyme comment

 Optimus Rhyme

// When choosing among 3 choices, you rarely need more than 2 evaluative statements. Which is why mine differs from above.
// The answers tend to have hard-coded answers or array searches, which are generally not good training for newbies. What if the design changes, or you don't know the array's size?? Then, see below!
// Also, my Printer function actually spaces out the print out as per the requested question, not jumbled together, even allowing an extra space for the negative numbers! :P


static void TwoDimMatrixEnumerator()
{
int[,] myTwoDimMatrix = new int[5, 5];

for (int i = 0; i < myTwoDimMatrix.GetLength(0); i++)
{
for (int j = 0; j < myTwoDimMatrix.GetLength(1); j++)
{
if (j > i)
{
myTwoDimMatrix[i, j] = 1;
}
else if (j < i)
{
myTwoDimMatrix[i, j] = -1;
}
}
}

Printer(myTwoDimMatrix);
}

static void Printer(int[,] passedInArray)
{
for (int i = 0; i < passedInArray.GetLength(0); i++)
{
for (int j = 0; j < passedInArray.GetLength(1); j++)
{
if (passedInArray[i, j] > -1)
{
// This puts a space in front of any 0's or 1's, to look nice with the -1's
Console.Write(" ");
}
Console.Write(passedInArray[i, j] + "\t ");
}
Console.WriteLine();
}
}


2014-08-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.