简体   繁体   English

在数组中找到最大值

[英]Finding the max value in an array

As part of school, one of my assignments is to write 10 pieces of code about 10 different search patterns, etc. 作为学校的一部分,我的任务之一是编写关于10种不同搜索模式的10段代码,等等。

For this one, i need to use a linear search to find the highest and lowest value in a defined array, and then display the number of times that value was found. 为此,我需要使用线性搜索在定义的数组中找到最高和最低值,然后显示找到该值的次数。

Heres the code i came up with: 我想出了以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Maxvaluefinder
{
    class Program
    {
        static void Main(string[] args)
        {
            var array = [1, 31, 10, 9, 420, -5, 77, 420, 300, 99]; //Sets up the array
            var maxvalue = 0; //Establishes variables for maximum value and the counter of maximum value.
            var maxvaluecount = 0;

            for (i = 1; i < array.Length; i++)
            {
                if (array[i] > maxvalue)
                {
                    maxvalue = array[i];
                    maxvaluecount = 1;
                }
                if (array[i] == maxvalue)
                {
                     maxvaluecount = maxvaluecount + 1;
                }
            }

            Console.WriteLine("The highest number in this array was" + maxvalue + "which appeared a total of" + maxvaluecount + "times."); // Prints the final outcome.
        }
    }
}

As of now, i am not 100% sure how the "for (i = 1; i < intArray.Length; i++)" part works, and the 'i' bits 'do not exist in current context' 到目前为止,我还不确定100%“ for(i = 1; i <intArray.Length; i ++)”部分的工作方式,并且“ i”位“在当前上下文中不存在”

Please help? 请帮忙?

Also, somewhat unrelated: how do i test run the code in microsoft visual studio? 此外,有些无关:我如何在Microsoft Visual Studio中测试运行代码?

Thanks :) 谢谢 :)

1st question: You need to declare your i inside for loop if you only want to use i inside it. 第一个问题:你需要声明你i里面for循环,如果你只是想用i里面。 Otherwise you need to declare i before the for loop. 否则,您需要在for循环之前声明i

for (var i = 1; i < array.Length; i++)

And this is how you create new int array 这就是您创建新的int数组的方式

var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99}; 

2nd question: Go to VS, create new console project, paste your code under main function and press F5 第二个问题:转到VS,创建新的控制台项目,将代码粘贴到main功能下,然后按F5

You need to declare a variable before you can use it. 您需要先声明一个变量,然后才能使用它。 Try: 尝试:

for (int i = 1; i < array.Length; i++){...}

A for loop works as follows: for循环的工作原理如下:

  1. You declare a control variable, in this case i , you assign it the starting value 1. 声明一个控制变量,在本例中为i ,为它分配起始值1。
  2. The for loop checks at the start of each loop if its end condition is satisfied, in the given case i < array.Length 在给定情况下,for循环在每个循环的开始时检查是否满足其结束条件, i < array.Length
  3. At the end of every loop, the control variable gets updated according to the last part of your syntax i++ , which is equivalent to i = i+1 在每个循环的最后,控制变量将根据语法i++的最后一部分进行更新,这等效于i = i+1

Also, I am not sure if you knew that, but in C# arrays are zero-based indexed, which means, that the first element in an array has actually the index zero. 另外,我不确定您是否知道这一点,但是在C#数组中,索引是从零开始的,这意味着数组中的第一个元素实际上的索引为零。 So you probably want to initialize: int i = 0 所以您可能要初始化: int i = 0

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

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