Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that reads two arrays from the console and compares them element by element.
- using System;
- class ComparingArrays
- {
- private static uint InputData(string message)
- {
- uint n;
- Console.Write(message);
- while (!uint.TryParse(Console.ReadLine(), out n) || n < 0)
- {
- Console.Write("Invalid input. Enter a positive integer number: ");
- }
- return n;
- }
- private static void Initialisation(uint arrayLength, int[] array)
- {
- for (int i = 0; i < arrayLength; i++)
- {
- Console.Write("Enter the {0} element of the array: ", i);
- while (!int.TryParse(Console.ReadLine(), out array[i]))
- {
- Console.Write("Invalid input. Enter an integer number: ");
- }
- }
- }
- static void Main()
- {
- uint firstArrayLength = InputData("Enter the first arrays`s length: ");
- int[] firstArray = new int[firstArrayLength];
- Initialisation(firstArrayLength, firstArray);
- uint secondArrayLength = InputData("Enter the second arrays`s length: ");
- int[] secondArray = new int[secondArrayLength];
- Initialisation(secondArrayLength, secondArray);
- bool areEqual = true;
- //Compare two arrays till the length of the shorter
- for (int i = 0; i < Math.Min(firstArrayLength, secondArrayLength); i++)
- {
- if (firstArray[i] != secondArray[i])
- {
- Console.WriteLine("The two arrays are NOT equal.");
- areEqual = false;
- break;
- }
- }
- if (areEqual && firstArrayLength == secondArrayLength)
- {
- Console.WriteLine("The two arrays are equal.");
- }
- else if (areEqual && firstArrayLength != secondArrayLength)
- {
- Console.WriteLine("The two arrays are equal till the length of the shorter.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement