A rectangular array is a two-dimensional array where each row has an equal number of columns.
Syntax:
type [,]<variableName>;
variableName = new type[value1 , value2];
where,
type: Specifies the data type of the arrays element.
[,]: Specifies that the array is a two-dimensional array.
variableName: Specifies the name of the two-dimensional array.
new: Is the operator used to instantiate the arrays.
value1: Specifies the number of rows in the two-dimensional array.
value2: Specifies the number of columns in the two-dimensional array.
variableName = new type[value1 , value2];
where,
type: Specifies the data type of the arrays element.
[,]: Specifies that the array is a two-dimensional array.
variableName: Specifies the name of the two-dimensional array.
new: Is the operator used to instantiate the arrays.
value1: Specifies the number of rows in the two-dimensional array.
value2: Specifies the number of columns in the two-dimensional array.
Example 3:
using System;
class StudentScore
{
void StudentDetails()
{
Console.Write("Enter the number of Students: ");
int noOfStds = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of Exams: ");
int exams = Convert.ToInt32(Console.ReadLine());
string[] stdName = new string[noOfStds];
string[,] details = new string[noOfStds, exams];
for (int i = 0; i < noOfStds; i++)
{
Console.WriteLine();
Console.Write("Enter the Student Name: ");
stdName[i] = Convert.ToString(Console.ReadLine());
for (int y = 0; y < exams; y++)
{
Console.Write("Enter Score in Exams " + (y + 1) + ": ");
details[i, y] = Convert.ToString(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Student Exam Details");
Console.WriteLine("--------------------");
Console.WriteLine();
Console.WriteLine("Student\t\tMarks");
Console.WriteLine("-----\t\t------");
for (int i = 0; i < stdName.Length; i++)
{
Console.WriteLine(stdName[i]);
for (int j = 0; j< exams; j++)
{
Console.WriteLine("\t\t" + details[i, j]);
}
Console.WriteLine();
}
}
static void Main()
{
StudentsScore objStudentsScore = new StudentsScore();
objStudentsScore.StudentDetails();
}
}
class StudentScore
{
void StudentDetails()
{
Console.Write("Enter the number of Students: ");
int noOfStds = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of Exams: ");
int exams = Convert.ToInt32(Console.ReadLine());
string[] stdName = new string[noOfStds];
string[,] details = new string[noOfStds, exams];
for (int i = 0; i < noOfStds; i++)
{
Console.WriteLine();
Console.Write("Enter the Student Name: ");
stdName[i] = Convert.ToString(Console.ReadLine());
for (int y = 0; y < exams; y++)
{
Console.Write("Enter Score in Exams " + (y + 1) + ": ");
details[i, y] = Convert.ToString(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Student Exam Details");
Console.WriteLine("--------------------");
Console.WriteLine();
Console.WriteLine("Student\t\tMarks");
Console.WriteLine("-----\t\t------");
for (int i = 0; i < stdName.Length; i++)
{
Console.WriteLine(stdName[i]);
for (int j = 0; j< exams; j++)
{
Console.WriteLine("\t\t" + details[i, j]);
}
Console.WriteLine();
}
}
static void Main()
{
StudentsScore objStudentsScore = new StudentsScore();
objStudentsScore.StudentDetails();
}
}
0 comments:
Post a Comment