【问题标题】:Determining if an array is sorted判断一个数组是否排序
【发布时间】:2014-03-17 04:04:53
【问题描述】:

我有一个学校作业,用户必须输入数字,程序必须确定它们是否已排序。如果有人可以帮助我的代码,那就太棒了。我在使用 IsSorted(int[] array, int n) 时遇到问题,truefalse 无法正常工作。

这是一个问题: Q7:编写程序输入一个int数组,然后判断该数组是否排序。这 程序应该有两个用户定义的方法来帮助你完成任务。

public static void InputArray(int[] array, ref int n)
public static bool IsSorted(int[] array, int n)

InputArray() 应该类似于 Lab 4 中的 Fill。IsSorted() 应该简单地返回 true 是 数组按升序排序,否则为假。请注意,您不是 要求对数组进行排序,只需确定数组是否已排序。 Main 方法应该给出 用户可以选择检查多个数组(即循环)。你可以假设 值的最大数量为 20。

** 注意:在这个赋值中,你可以假设正确的数据类型:也就是说,如果程序 请求一个double,你可以假设用户会输入一个double,等等。你需要验证 输入的数据在正确的范围内。

到目前为止,这是我的代码:

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;

    }
}

【问题讨论】:

  • 我认为这是错误的标记,因为这不是有效的 c++ 语法。你的意思是 c# 还是 Java?
  • 应该标记为C#
  • 所以我想你发布的最后一个家庭作业已经完成了? stackoverflow.com/questions/22446522/c-sharp-bank-assignment
  • 不,我还在研究那个。作业有三个问题,这两个问题给我带来了问题

标签: c# arrays sorting int


【解决方案1】:

您没有设置last 变量。

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;
}

假设第一次检查始终有效,这应该可以工作。即array[0] &gt;= 0

【讨论】:

  • 很遗憾没有修复它
【解决方案2】:
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;
    }
}

我希望这对你完成任务要求的大部分内容有所帮助。

【讨论】:

  • 如果您需要任何修改,请告诉我,我可以做到
  • 代码未经过测试...如果您遇到任何错误,请告诉我...到目前为止,我认为代码没有任何问题。
  • 喜欢的话给我点个赞吧……我做的很简单,这样你就可以更好地理解逻辑
  • 有没有办法让它只允许输入20个元素。如果选择了超过 20 个,是否有办法显示错误消息并再次询问元素的数量?
  • 或者有没有一种方法可以让程序循环询问人们是否想查看另一个数组?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2016-02-10
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
相关资源
最近更新 更多