0

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;

    }
}
4
  • 1
    I believe this is tagged incorrectly as this is not valid c++ syntax. Did you mean c# or Java? Commented Mar 17, 2014 at 4:12
  • It's suppose to be tagged as C# Commented Mar 17, 2014 at 4:14
  • So I suppose the last homework assignment you posted was done? stackoverflow.com/questions/22446522/c-sharp-bank-assignment Commented Mar 17, 2014 at 4:17
  • No i'm still working on that one too. There's three questions to the assignment and these two are giving me problems Commented Mar 17, 2014 at 4:23

2 Answers 2

1

You are not setting the last variable.

public static bool IsSorted(int[] array, int n)
{
    int last = array[0];

    foreach (int val in array)
    {
        if (array[val] < last)
            return false;

        last = array[val+1];
    }

    return true;
}

This should work assuming that the first check is always valid. i.e. array[0] >= 0

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately that didnt fix it
0
public class Array_Sort
{
    public static int n = 0;
    public static int SIZE = 20;
    public static int[] array = new int[SIZE];

    public static void Main()
    {
        InputArray();
        if (IsSorted())
        {
            Console.WriteLine("\n  The values in the array are in Ascending order");
        }
        else
        {
            Console.WriteLine("\n  The values in the array are NOT in Ascending order");
        }
    }

    public static void InputArray()
    {
        int i = 0;
        Console.Write("\n Enter the number of elements  : ");
        SIZE = Convert.ToInt32(Console.ReadLine());
        if (SIZE > 20)
        {
            Console.WriteLine("\n Invalid Selection, try again \n\n ");
            InputArray();
        }
        else
        {
            for (i = 0; i < SIZE; ++i)
            {
                Console.WriteLine("\n Enter the element- {0}  : ", i + 1);
                array[i] = Convert.ToInt32(Console.ReadLine());
            }
        }
    }
    public static bool IsSorted()
    {
        int i;
        int count = 1;
        for (i = 0; i < n; i++)
        {
            if (i >= 1)
            {
                if (array[i] > array[i - 1])
                {
                    count++;
                }
            }
        }

        if (count == n)
            return true;
        else
            return false;
    }
}

i hope this will help you for most part of what the assignment asks for.

7 Comments

let me know if you need any modifications i can do it
the code is not tested... if you get any error, let me know... i don't think there is anything wrong in the code, as of now.
comment me if you like it... i did it with simplicity so that you can better understand the logic
Is there a way to make it only allow 20 elements to be entered. If more than 20 are selected is there a way for an error message to show up and ask for the number of elements again?
Or is there a way that the program loops around to ask the person if they want to look at another array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.