Hello Techies,
Today, we are going to learn about Arrays in C# with some simple examples. Here, we have a simple example to print the array in the reverse order with spaces separated.Here, we are going to cover most of the basic things about the Arrays in C#.
So, let's check out what things (variables, methods, etc) are required. Our task is basic so we require a simple looping variable and an array variable, and a loop to print the Array in reverse order.
Let's not delve into the theory part and start with the example by declaring the variables.
Here, firstly we require the two variables for the array, one is to store the array elements and one is to get the number of elements of the array as here we are taking the elements dynamically from the user.
So, firstly we are going to check by declaring the array and then initializing it with the static value. Please check the following code snippet,
//Declaring different type of the array...
string[] cars; //Array of string contains car name..
int[] numberPlate; //Array of integer contains number plat for the cars..
//Initializing the Arrays...
string[] cars = {
"Volvo",
"BMW",
"Ford",
"Mazda"
};
int[] numberPlate = {
1020,
3040,
5070,
8080
};
Here, we have defined the array with its value. Before initializing it with the value we have declared it with the proper datatype.
We all know array is a data structure to store the value with the index value which is a string from 0 to n-1. So, now we are going to print the value of the above arrays. Please check the following code snippet,
Console.WriteLine('Element of 0th Index as car :{0} with numberplate:{1}', cars[0], numberPlate[0]);
Console.WriteLine('Element of 1st Index as car :{0} with numberplate:{1}', cars[1], numberPlate[1]);
Console.WriteLine('Element of 2nd Index as car :{0} with numberplate:{2}', cars[2], numberPlate[2]);
Console.WriteLine('Element of 3rd Index as car :{0} with numberplate:{3}', cars[3], numberPlate[3]);
// OutPut
//Element of 0th Index as car :Volvo with numberplate:1020
//Element of 1st Index as car :BMW with numberplate:3040
//Element of 2nd Index as car :Ford with numberplate:5070
//Element of 3rd Index as car :Mazda with numberplate:8080
Here, you can see as per the index value we can get the output from the array with its element value from the index. Now check out our following source code which contains the solution for the challenge or the statement which is defined previously at the starting of the article. Please check the following code snippet,
public static void array(string[] tmp_arr,int n)
{
int[] arr = Array.ConvertAll(tmp_arr,Int32.Parse);
for (var i = n - 1; i > -1; i--)
{
Console.Write(arr[i] + " ");
}
}
Here, in the above code snippet, you can see we have defined the function for our small and simple statement. Why? Confused? Ok. So, functions are reusable and it will reduce the code complexity. Still confused about how it is reusable? Here, we require a small change to perform the same operations on the N number of elements, then what happens if we have used the linear approach? Then we have to make a completely separate program for that or we have to make many changes.
We just have to change the main method; no changes in the functions are required, will see furthermore about this later in this article.
So, let's check what we have done in this function,
- We have taken the arguments as the array of string and the integer number.
- The integer number gives the number of elements of the array and the array of the string contains space-separated elements.
- Inside the function, we have done the conversion of the space-separated string elements to the integer array.
- Then inside the loop, we have printed the array in reverse format.
Now, let's check how we are going to call this function. Please check the following code snippet,
int n = Conver.ToInt32(Console.ReadLine().Trim()); //Taking the number of elements of Array...
string[] arr_temp = Console.ReadLine().Split(' '); //Taking the Space Separated elements....
array(arr_temp,n); //Calling Functions with arguments....
Here, by following the above code snippet we can call the function with our problem statement.
Now, let's check out the following code snippet for the situation. I have defined for performing the same operations for the multiple arrays,
int numOfArray = Conver.ToInt32(Console.ReadLine().Trim());
for(int i = 0;i<=numOfArray;i++)
{
int n = Convert.ToInt32(Console.ReadLine().Tim());
string[] arr_temp[] = Console.ReadLine().Split(' ');
array(arr_temp,n);
}
Here, the above code firstly takes the total number of arrays and then is going to take the number of elements for the array and elements for the array and then going to call the function one by one in this given order for each and every array and then it will print the output.
NOTE
Here, check out the following full source code for the given statement which is also the solution for the HackerRank Coding challenge 30daysOfCode Day 7.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
public static void Main(string[] args)
{
//commented code is for performing the operations for the dynamic number of arrays.
/*int numOfArray = Conver.ToInt32(Console.ReadLine().Trim());
for(int i = 0;i<=numOfArray;i++)
{
int n = Convert.ToInt32(Console.ReadLine().Tim());
string[] arr_temp[] = Console.ReadLine().Split(' ');
array(arr_temp,n);
} */
int n = Convert.ToInt32(Console.ReadLine().Trim());
string[] arr_temp = Console.ReadLine().Split(' ');
array(arr_temp,n);
}
public static void array(string[] tmp_arr,int n)
{
int[] arr = Array.ConvertAll(tmp_arr,Int32.Parse);
for (var i = n - 1; i > -1; i--)
{
Console.Write(arr[i] + " ");
}
}
}