In C#, on a console application on Microsoft visual studios-Build a method which
ID: 3743291 • Letter: I
Question
In C#, on a console application on Microsoft visual studios-Build a method which reads 3 integers from the console and returns them as an int array (type a number, then hit enter, 3 times).-Call this method twice to store 2 local int array variables. -Once both arrays are built, write a method which accepts the two arrays and compares the contents to see if they match. If the method returns true (the arrays match), display "Arrays match" in the console. Otherwise, display "Arrays don't match" in the console.-Finally, ensure at the end, that the user must hit the enter key in order to exit the program.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static int[] getArray()
{
// create integer array
int[] arr = new int[3];
Console.Write("Enter first number");
// read int from console
arr[0] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number");
// read int from console
arr[1] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter third number");
// read int from console
arr[2] = Convert.ToInt32(Console.ReadLine());
return arr;
}
public static bool compareArrays(int[] arr1 , int[] arr2)
{
int i;
// traverse through the arrays
for( i = 0 ; i < arr1.Length ; i++ )
// if the current element of both are not equal
if( arr1[i] != arr2[i] )
return false;
return true;
}
public static void Main(string[] args)
{
Console.WriteLine("First Array ...");
int[] arr1 = getArray();
Console.WriteLine(" Second Array ...");
int[] arr2 = getArray();
Console.WriteLine("");
if( compareArrays(arr1 , arr2) )
Console.Write("Arrays match");
else
Console.Write("Arrays don't match");
Console.ReadLine();
}
}
}
Sample Output