I have an assignment for school where the user has to input numbers and the program has to determine whether they are sorted or not. If anyone can help with my code that would be awesome. I am having trouble with the IsSorted(int[] array, int n), the true and false are not working properly.
Here is the question: Q7: Write a program to input an int array, and then determines if the array is sorted. The program should have two user-defined methods to help you with the task.
public static void InputArray(int[] array, ref int n)
public static bool IsSorted(int[] array, int n)
InputArray() should be similar to Fill from Lab 4. IsSorted() should simply return true is the
array is sorted in ascending order and false otherwise. Please note that you are NOT being
asked to sort an array, just determine if the array is sorted. The Main method should give the
user the option of examining more than one array (i.e. loop). You can assume that the
maximum number of values will be 20.
** Note: On this assignment you can assume the correct datatype: that is, if the program
requests a double, you can assume that the user will input a double, etc. You need to validate
that the data entered is in the correct range.
Here is my code so far:
using System;
public class Array_Sort
{
public static void Main()
{
int n = 0;
const int SIZE = 20;
int[] array = new int[SIZE];
InputArray(array, ref n);
IsSorted(array, n);
}
public static void InputArray(int[] array, ref int SIZE)
{
int i = 0;
Console.Write("Enter the number of elements: ");
SIZE = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the {0} elements:", SIZE);
for (i = 0; i < SIZE; ++i)
array[i] = Convert.ToInt32(Console.ReadLine());
}
public static bool IsSorted(int[] array, int n)
{
int last = 0;
foreach (int val in array)
{
if (val < last)
return false;
}
return true;
}
}