Write a program in C-Sharp to demostrate Arrays.

An array represents a fixed number of elements of a particular type. The elements in an array are always stored in a contiguous block of memory, providing highly efficient access.An array is denoted with square brackets after the element type. Like,

char[] vowels = new char[5]; // It declare an array of 5

Square brackets also index the array, accessing a particular element by position:

vowels [0] = 'a';
vowels [1] = 'e';
vowels [2] = 'i';
vowels [3] = 'o';
vowels [4] = 'u';
Console.WriteLine (vowels [1]); //To print e.

This prints "e" because array indexes start at zero. We can use a for loop statement to iterate through each element in the array.The Length property of an array returns the number of elements in the array. Once an array has been created, its length cannot be changed. The System.Collection namespace and subnamespaces provide higher-level data structures, such as dynamically sized arrays and dictionaries. All arrays inherit from the System.Array class, which defines common methods and properties for all arrays. This includes instance properties such as Length and Rank, and static methods to:
  • Dynamically create an array (CreateInstance)
  • Get and set elements regardless of the array type (GetValue/SetValue)
Creating an array always preinitializes the elements with default values. The default value for a type is the result of a bitwise-zeroing of memory.

int[] a = new int[1000];
Console.Write (a[123]); // It will print 0(zero).

Multidimensional arrays come in two varieties: rectangular and jagged. Rectangular arrays represent an n-dimensional block of memory, and jagged arrays are arrays of arrays.Rectangular arrays are declared using commas to separate each dimension,

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

A rectangular array can be initialized as follows,

int[,] matrix = new int[,]
{
{0,1,2},
{3,4,5},
{6,7,8}
};

Jagged arrays are declared using successive square brackets to represent each dimension,

int [][] matrix = new int [3][];

A jagged array can be initialized as,

int[][] matrix = new int[][]
{
new int[] {0,1,2},
new int[] {3,4,5},
new int[] {6,7,8}
};

Example: Write a program in C-Sharp to store Student Name, Roll No and Marks of three subjects in an Array. Find the Student record with highest total Marks. Download Code

using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment_3
{
struct StudentInfo
{
public string stdName ;
public long stdRollNo ;
public int stdMarks1 ;
public int stdMarks2 ;
public int stdMarks3 ;
public int total;

}

class Program
{
static void Main(string[] args)
{
StudentInfo[] std = new StudentInfo[2];
int[] total =new int[2];
for (int i = 0; i < std.Length; i++)
{
Console.Write("\nEnter Student Name: ");
std[i].stdName = Console.ReadLine();
Console.Write("Enter Student RollNo: ");
std[i].stdRollNo = long.Parse(Console.ReadLine());
Console.Write("Enter Student Marks1: ");
std[i].stdMarks1 = int.Parse(Console.ReadLine());
Console.Write("Enter Student Marks2: ");
std[i].stdMarks2 = int.Parse(Console.ReadLine());
Console.Write("Enter Student Marks3: ");
std[i].stdMarks3= int.Parse(Console.ReadLine());
std[i].total = std[i].stdMarks1 + std[i].stdMarks2 + std[i].stdMarks3;
Console.WriteLine("The Student Totoal Marks is {0}",std[i].total);
}

double max = std[0].total;
int maxstd = 0;
for (int j = 0; j < std.Length; j++)
{
if (max < std[j].total)
{
max = std[j].total;
maxstd = j;
}

}
Console.WriteLine("\n\nThe Student with Heightest total is : \n\nName: {0}\nRoll No: {1}\nTotal Marks: {2}",std[maxstd].stdName,std[maxstd].stdRollNo,std[maxstd].total);
Console.ReadLine();
}


}
}

0 comments:

Post a Comment

 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.