Found 34494 Articles for Programming

How do I sort a two-dimensional array in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:41:25

2K+ Views

To sort a two-dimensional array in C#, in a nested for loop, add another for loop to check the following condition.Examplefor (int k = 0; k < j; k++) {    if (arr[i, k] > arr[i, k + 1]) {       int myTemp = arr[i, k];       arr[i, k] = arr[i, k + 1];       arr[i, k + 1] = myTemp;    } }Till the outer loop loops through, use the GetLength() method as shown below. This is done to sort the array.Examplefor (int i = 0; i < arr.GetLength(0); i++) {    for ... Read More

How do we use multi-dimensional arrays in C#?

karthikeya Boyini
Updated on 23-Jun-2020 14:45:08

131 Views

C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular array. Declare a 2-dimensional array of strings as.string [, ] names;A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 4 rows and each row has 4 columns.int [, ] a = new int [4, 4] {    {0, 1, 2, 3} , /* initializers for row indexed by 0 */    {4, 5, 6, 7} , /* initializers for row indexed by ... Read More

How do we pass an array in a method in C#?

Samual Sam
Updated on 23-Jun-2020 14:46:03

78 Views

Pass array in a method as a method argument.Let’s say the following is our array declaration and initialization.MyArray app = new MyArray(); /* an int array with 5 elements */ int [] balance = new int[]{1000, 2, 3, 17, 50};Now call the method getAverage() and pass the array as method argument.double getAverage(int[] arr, int size) {    // code }The following is the example showing how to pass an array in a method in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       double getAverage(int[] arr, int size) {          int i;   ... Read More

How do you sort an array in C# in descending order?

karthikeya Boyini
Updated on 23-Jun-2020 14:46:40

4K+ Views

The following is the unsorted array.int[] list = {98, 23, 97, 36, 77};Now first use the Sort() method to sort the array.Array.Reverse(list);Use the Reverse() method that would eventually give you a sorted array in descending order.Array.Reverse(list);You can try to run the following code to to sort an array in descending order.Example Live Demousing System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          int[] list = {98, 23, 97, 36, 77};          Console.WriteLine("Original Unsorted List");          foreach (int i in list) {   ... Read More

How do we use foreach statement to loop through the elements of an array in C#?

Samual Sam
Updated on 23-Jun-2020 14:28:39

255 Views

A foreach loop is used to execute a statement or a group of statements for each element in an array or collection.It is similar to for Loop; however, the loop is executed for each element in an array or group. Therefoe, the index does not exist in it.Let us see an example of Bubble Sort, wherein after sorting the elements, we will display the elements using the foreach loop.Example Live Demousing System; namespace BubbleSort {    class MySort {       static void Main(string[] args) {          int[] arr = { 78, 55, 45, 98, 13 };          int temp;          for (int j = 0; j

How do we use enum keyword to define a variable type in C#?

karthikeya Boyini
Updated on 23-Jun-2020 14:29:11

68 Views

C# enumerations are value data type. An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.The following is the syntax of enum.enum {    enumeration list };Let us see an example.enum Vehicle { Car, Bus, Truck };The following is an example showing how to use enum keyword to define variable type.Example Live Demousing System; namespace Demo {    class Program {       enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };       static void Main(string[] args) {          int WeekdayStart = (int)Days.Mon;   ... Read More

How do we use continue statement in a while loop in C#?

Samual Sam
Updated on 23-Jun-2020 14:31:50

147 Views

The continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.The continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.For the while loop, continue statement causes the program control passes to the conditional tests.The following is the complete code to use continue statement in a while loop.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {         ... Read More

How do we pass parameters by value in a C# method?

karthikeya Boyini
Updated on 23-Jun-2020 14:32:38

104 Views

This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter.The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument. The following is the code to pass parameters by value.Example Live Demousing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(int x, int y) {          int temp;          temp = x; /* save the value ... Read More

How do you use a ‘for loop’ for accessing array elements in C#?

Samual Sam
Updated on 23-Jun-2020 14:35:45

182 Views

The ‘for loop’ executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.The following is our for loop.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          int i, j;          /* initialize elements of array n */          for ( i = 0; i < 10; i++ ) {             n[ ... Read More

How do we call a C# method recursively?

Samual Sam
Updated on 23-Jun-2020 14:21:46

235 Views

To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().If the value is 1, it returns 1 since Factorial is 1.if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if 1you want the value of 5!Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);The following is the complete code to call a C# method recursively.Example Live Demousing System; ... Read More

Advertisements