C# exercises and solutions-C# array: series of numbers


C# array exercise: series of numbers

Exercise: A two-dimensional array stores values in rows and columns. By using two-dimensional array, write C# program to display a table of numbers as shown below:


12 34 5 
67 89 10
1112 1314 15
1617 1819 20
2122 2324 25
 

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)

        {

 

            printSeries();

            Console.ReadLine();

 

        }

        public static void printSeries()

        {

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

            int i, j;

                           

                for (i = 0; i < 5; i++) //assign values to the two-dimensional array

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

                    {

                        if (i == 0) tArr[i, j] = j + 1; //fill the first row

                        else if (i > 0 && j == 0)

                            tArr[i, j] = tArr[i - 1, 4] + 1; //fetching the value of the last cell in the previous row

                        else

                         tArr[i, j] = tArr[i, j - 1] + 1; //fill subsequent cells

                    }

         

     for(i=0;i<5;i++){ //print the array

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

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

        Console.WriteLine();  

       }

 

  }

 }

}


Comments

shrill comment

 shrill

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace twoDimarray
{
public class Program
{
public void Main(string[] args)
{
int[,] twoDarray = new int[4, 4];
int length = twoDarray.Length;

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i == 0)
twoDarray[i, j] = j + 1;
else if (i > 0 && j == 0)
{

twoDarray[i, j] = (twoDarray[i - 1, 3]) + 1;
}
else
twoDarray[i, j] = (twoDarray[i, j - 1]) + 1;

}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0}\t", twoDarray[i, j]);

}
Console.WriteLine();
}
}
}
}


2017-02-08
Bob comment

 Bob

This teacher is not very easy to understand. Luckily the comments show solutions that are MUCH better and easier to understand.


2016-12-10
Hai comment

 Hai

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

namespace Array_SerieN
{
class Program
{
static void Main(string[] args)
{
int count=1;
int[,] arr = new int[5, 5];
for (int r=0; r<5; r++)
{
for(int c=0; c<5; c++)
{
Console.Write("{0} \t", count++);
}
Console.Write("\n\n");
}
Console.ReadLine();
}
}
}


2016-12-04
Deepika comment

 Deepika

int[,] arr = new int[5, 5]
{
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}
};

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(Convert.ToString(arr[i, j]));
Console.Write(" ");
}


2016-08-10
HK comment

 HK

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

namespace Tutorial_3
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[5, 5];

int i, j;

for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
arr[i, j] = i * 5 + j + 1;
}
}

for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}


2016-04-24
Antonia comment

 Antonia

Array assignment value methods too complicated. Use a simple math expression like this:

private static void printTwoDimArray(int[,] array)
{
int dim=5;

/*populating array*/
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
array[i, j] = i*dim + j + 1;
}

/*printing array*/
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
Console.Write("{0}\t",array[i, j]);
Console.Write("\n");
}
}


2015-07-28
Akram comment

 Akram

using System;

namespace trinnings
{



class Program
{


static void Main(string[] args)
{
int[,] a = new int[5, 5];

int c = 1;

for(int i=0;i<a.GetLength(0);i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
a[i,j] =c;
c++;
}
}




for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write(a[i, j] +"\t");
}
Console.WriteLine();
}


}
}
}


2015-07-10
Optimus Rhyme comment

 Optimus Rhyme

Some people above seem to have forgotten that the values need to be in an array, THEN printed. The answer above seems complicated and VERY unfriendly to human reading.
Try this:

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

// This is my incrementing variable
int numbHolder = 1;

for(int i = 0; i < myTwoDimArray.GetLength(0); i++)
{
for(int j = 0; j < myTwoDimArray.GetLength(1); j++)
{
if(i == 1 && j == 2)
{
// This makes the 8 number stored as 0 in the Array
numbHolder++;
continue;
}
myTwoDimArray[i, j] = numbHolder;
numbHolder++;
}
}


2014-08-12
Baba comment

 Baba

int[,] A = new int[5, 5];
int a = 1;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++,a++)
{
if(a<10)
Console.Write(a + " ");
else
Console.Write(a + " ");
}
Console.WriteLine();
}


2014-07-21
davyka comment

 davyka

//oups!
using System;
class Program
{
static void Main(string[] args)
{
int [,] array = new int [5,5];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i,j] = (j+1) + (i * array.GetLength(0));
Console.Write(array[i,j] + "\t");
}
Console.WriteLine("\r");
}
}
}


2014-05-11
Lâm Minh Nhân (VietNam) comment

 Lâm Minh Nhân (VietNam)

int[,] mang = new int[5,5];
int n = 0;
for(int i=1;i<=5;i++)
{
for (int j = 1; j <= 5; j++)
{
if (i < 3)
{
Console.Write(j + n + " ");
}
else
{
Console.Write(j + n + " ");
}
}
n += 5;
Console.WriteLine();


2013-11-18



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.