Loading...

Two Dimensional Arrays in C# - 2D Arrays

Understanding 2 D Arrays in C# :

Understanding 2 D Arrays has always been a very difficult task for students of Computer Programming. Most of the Students understood the basic Concept of 2 D Arrays but have no idea how to implement that. Today we will learn the basic concept and implementation both.

Basic Concept:

We have learnt the concept and implementation of Arrays in post Arrays in C# . 2 D Arrays have not a big difference but only one difference which is elaborated from its name also that it has two Dimensions. It means that 2 D array do not stored in a line path like 1 D or simple Arrays, and it grows in both x and y dimension.

Names
Physics Marks
Computer Programming Marks
Faisal Jabbar
78
81
Aqib Ahmed
89
75
Junaid Jabbar
75
83

In above table, the data is in two Dimension, means that data is stored in rows and columns, not in single line format like single dimensional arrays.
This Table consists of 4 rows and 3 columns.

In a 2D array, we generally consider the first index to be the row, and the second to be the column: a[row, col]
In above table, if the arrays name is Student, then we will get data of first column and first row by Student[0,0] and so on.
int[,] student = new int[5,8]; // it is used to initialize an array on data type int, name = student, and size: rows = 5, columns = 8;

 Example:


For example we want to store marks of three student in three subjects and get the sum of them. To start implementing 2 D arrays, REMEMBER one thing that you can not implement a 2 D array only in making array design in your mind unless you are an expert. So first make an array design on paper like this.

Names
Physics Marks 
Computer Programming Marks
Mathematics Marks
Total Marks
User will insert
User will insert
User will insert
User will insert
We will calculate
User will insert
User will insert
User will insert
User will insert
We will calculate
User will insert
User will insert
User will insert
User will insert
We will calculate

In above array design Names is written in student[0,0], Faisal Jabbar is written in student[1,0], "Total Marks" is written in student[0,4] and so on. 
Now come on the Coding Phase:

class Program
    {
       public static string[,] student = new string[4, 5]; // 4 rows and 5 columns, taking in string as number can go in string but name cannot go in int, and we need both
           
        static void Main(string[] args)
        {
            insertion();
            display();
        }
        public static void insertion()
        {
            student[0, 0] = "Names"; // storing dumb values in array.. values which are fixed
            student[0, 1] = "Physics Marks";
            student[0, 2] = "Computer Programming Marks";
            student[0, 3] = "Mathematics Marks";
            student[0, 4] = "Total Marks";
            for (int i = 1; i <= 3; i++) // row no. 0 is filled already and we have 4 rows till index number 3
            {
                Console.WriteLine("Enter the name of student");
                student[i, 0] = Console.ReadLine();  // i will iterate through 1 to 3 storing 3 student names and column name is fixed for name which is 0
                for (int j = 1; j <= 3; j++) // column 0 in already filled and we have 5 cloumns, 0 to 4, we will calculate index 4
                {
                    Console.WriteLine("Enter the marks of {0} in {1}",student[i,0],student[0,j]);  // name is saved in i,0 and subject name is saved in 0,j index number
                    student[i, j] = Console.ReadLine();
                }
                student[i, 4] = sum(student[i, 1], student[i, 2], student[i, 3]).ToString(); // get the sum in string format
            }
        }
        public static double sum(string first_marks, string second_marks, string third_marks) // it will calculate the sum of marks
        {
            double a, b, c, sum;
            a = double.Parse(first_marks); // to convert first marks in double format so that we can make calculations
            b = double.Parse(second_marks);
            c = double.Parse(third_marks);
            sum = a + b + c;
            return sum;
        }
        public static void display() // it will display the whole array using for loop
        {
            for (int i = 0; i <= 3; i++)
            {
                for (int j = 0; j <= 4; j++)
                {
                    Console.Write("{0} ",student[i,j]);
                }
                Console.WriteLine();
            }
        }
    }

Output:

The output has alignment and designing issues, logic is crystal clear.



Thanks for Reading, Hit Like if you Understood. It helps a lot to keep us motivated

0 comments:

Post a Comment

Google+

 
TOP