简体   繁体   English

如何找到数组的最小值和最大值以及数组的平方和反转的数组?

[英]How do I find the lowest and highest value of an array as well as the array squared and the array reversed?

I'm trying to write a code that will give me the highest and lowest number in an array as well as the array squared and the reversed array.我正在尝试编写一个代码,该代码将为我提供数组中的最高和最低数字以及数组平方和反转数组。 I keep getting 0's when I run the program but no other problem, so something is set equal to wrong but I can't figure out what's wrong because there's no error messages.当我运行程序时,我一直得到 0,但没有其他问题,所以设置为错误,但我无法弄清楚是什么问题,因为没有错误消息。 Here's the code:这是代码:

using System;

namespace ArrayMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an array size: ");
            int someArray = Convert.ToInt32(Console.ReadLine());


            int[] Array = new int[someArray];

            String input = Console.ReadLine();

            int[] arrayPart3 = new int[10];

            ReversedArray(arrayPart3);
            for (int index = 0; index < arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }
            Console.WriteLine();

            Console.WriteLine("Largest is:" + ArrayMax(arrayPart3));
            Console.WriteLine("Smallest is:" + ArrayMin(arrayPart3));

            SquaredArray(arrayPart3);
            for (int index = 0; index<arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }
            Console.WriteLine();

            ReversedArray(arrayPart3);
            for (int index = 0; index < arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }

        }

        public static int ArrayMax(int[] someArray)
        {
            int highest = someArray[0];
            for (int index = 0; index < someArray.Length; index++)
            {
                if (someArray[index] > highest)
                    highest = someArray[index];
            }
            return highest;
        }

        public static int ArrayMin(int[] someArray)
        {
            int lowest = someArray[0];
            for (int index = 0; index < someArray.Length; index++)
            {
                if (someArray[index] < lowest)
                    lowest = someArray[index];
            }
            return lowest;
        }

        public static void SquaredArray(int[]someArray)
        {

            for (int index = 0; index < someArray.Length; index++)
            {
                 someArray[index] = someArray[index] * someArray[index];
            }

        }


        public static void ReversedArray(int[] someArray)
        {
            for (int index = 0; index<someArray.Length; index++)
            {
                int temp = someArray[index];
                someArray[index] = someArray[someArray.Length - 1 - index];
                someArray[someArray.Length - 1 -index] = temp;
            }

        }
    }
}

the code of parsing the input is missing缺少解析输入的代码

here is a compact linq based code:这是一个基于 linq 的紧凑代码:

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var input = Console.ReadLine();
        // or use a fixed input: var input = "1 2 3 4";
        int[] arrayPart3 = input.Split(' ',',', ';').Select(s => int.Parse(s)).ToArray();

        Console.WriteLine("Reversed: " + string.Join("| ", arrayPart3.Reverse()));
        Console.WriteLine();

        Console.WriteLine("Largest is:" + arrayPart3.Max());
        Console.WriteLine("Smallest is:" + arrayPart3.Min());
        Console.WriteLine("Squared: " + string.Join("| ", arrayPart3.Select(i => i*i)));
        Console.WriteLine("Reversed Squared: " + string.Join("| ", arrayPart3.Reverse().Select(i => i*i)));

    }
}

As mentioned by @user287107 System.Linq provides most of these functions out of the box, for the squaring you can use a simple Select extension method Func.正如@user287107 System.Linq 所提到的,这些功能中的大部分都是开箱即用的,对于平方,您可以使用简单的 Select 扩展方法 Func。

Here is an ArrayUtils class I quickly wrote to provide the functionality you are looking for:这是我快速编写的 ArrayUtils class 以提供您正在寻找的功能:

using System.Linq;

namespace StackOverflow
{
    public class ArrayUtils
    {
        public int GetMax(int[] arr)
        {
            return arr.Max();
        }

        public int GetMin(int[] arr)
        {
            return arr.Min();
        }

        public int[] GetReverse(int[] arr)
        {
            return arr.Reverse().ToArray();
        }

        public int[] SquareValues(int[] arr)
        {
            return arr.Select(x => x * x).ToArray();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM