I'm reading a C# book for beginners, and in every end of the chapter, there are exercises to be answered based on the lessons tackled.
One of those exercises goes this way: (not the exact wordings)
Write a program that will accept an int as the array length, and the values for the array.
Then will print:
"0" if the array is not sorted in ascending way.
"1" if it is sorted. And,
"2" if it is sorted, but there are duplicates.
Example:
// Sorted
Input: 1, 2, 3, 5
Print: 1
// Not sorted
Input: 2, 1, 3, 6
Print: 0
// Sorted, but with duplicates
Input: 2, 2, 3, 7
Print: 2
I don't know if my logic here is absolute, but somehow it is working,
and I done it in my way using this code:
int arrayLength = 0;
int prev, next;
int sortStatus = 1;
Console.Write("Input array Length: ");
arrayLength = Convert.ToInt32(Console.ReadLine());
int[] ar = new int[arrayLength];
for (int x = 0; x < arrayLength; x++)
{
Console.Write("Input {0} value: ", (x+1).ToString());
ar[x] = Convert.ToInt32(Console.ReadLine());
}
for (int x = 0; x < ar.Length-1; x++)
{
prev = (int)ar[x];
next = (int)ar[x + 1];
if (next < prev)
sortStatus = 0;
if (next == prev)
sortStatus = 2;
}
Console.Write(sortStatus.ToString());
Console.Read();
Is it possible to express this in LINQ? How?