首页 \ 问答 \ 数组索引超出范围异常C#(Array Index out of range exception C#)

数组索引超出范围异常C#(Array Index out of range exception C#)

我试图在数组的平均值上找到数组中最接近的数字。 阵列将在主程序中确定。 这是当前的代码:

public static void Main()
{
    double[] numbers = { 1, 2, 3, 2, 5 };    
    double m1 = Miidi(numbers);         
    double m2 = Miidi(new double[] { 1 });  
    double m3 = Miidi(new double[] { 3, 3 });
    double m4 = Miidi(new double[] { });
    Console.WriteLine(m1);
    Console.WriteLine(m2);
    Console.WriteLine(m3);
    Console.WriteLine(m4);
}

public static double Miidi(double[] numbers)
{
    double average = 0;
    average = numbers.Average();

    double nearest = FindNearest(numbers, average);
    return nearest;
}

public static double FindNearest(double[] t, double avg)
{
    double searchValue = avg;
    double currentNearest = t[0];
    double currentDiff = currentNearest - searchValue;

    for (int i = 0; i < t.Length; i++)
    {
        double diff = t[i] - avg;
        if (diff < currentDiff)
        {
            currentDiff = diff;
            currentNearest = t[i];
        }
    }
    return currentNearest;
}

}

我得到一个超出范围异常的数组索引。 我尝试了你们提供的方法,并在循环中改变了<= to <,但我仍然得到异常。 我提供了主要程序以便澄清。


I'm trying to find the closest number in an array when compared to the array's average. The array will be determined in the main program. Here's the current code:

public static void Main()
{
    double[] numbers = { 1, 2, 3, 2, 5 };    
    double m1 = Miidi(numbers);         
    double m2 = Miidi(new double[] { 1 });  
    double m3 = Miidi(new double[] { 3, 3 });
    double m4 = Miidi(new double[] { });
    Console.WriteLine(m1);
    Console.WriteLine(m2);
    Console.WriteLine(m3);
    Console.WriteLine(m4);
}

public static double Miidi(double[] numbers)
{
    double average = 0;
    average = numbers.Average();

    double nearest = FindNearest(numbers, average);
    return nearest;
}

public static double FindNearest(double[] t, double avg)
{
    double searchValue = avg;
    double currentNearest = t[0];
    double currentDiff = currentNearest - searchValue;

    for (int i = 0; i < t.Length; i++)
    {
        double diff = t[i] - avg;
        if (diff < currentDiff)
        {
            currentDiff = diff;
            currentNearest = t[i];
        }
    }
    return currentNearest;
}

}

I'm getting an array index out of range exception. I tried the methods you guys provided and changed the <= to < in the loop, but I'm still getting the exception. I provided the main program for clarification.


原文:
更新时间:2022-06-06 21:06

最满意答案

您需要从a.out命令中删除-c

a.out: test.o extra.o
    gcc $(T) test.o extra.o

或更好:

a.out: test.o extra.o
    gcc $(T) -o $@ test.o extra.o

或者,还是更好:

extra: test.o extra.o
    gcc $(T) -o $@ test.o extra.o

错误消息通常是因为您在命令行上使用-c指定了-lm内容,但是您没有在此处执行此操作。 OTOH,你用-c选项列出目标文件 - 它将生成警告:

gcc -ansi -pedantic -Wall -Werror -c test.o extra.o
                                   ^ this one, here

那些.o文件是链接器输入,但您没有链接。 删除-c ,您将生成a.out 。 那应该没问题。

我想这是我第一次看到用于构建a.out的makefile。 极端不寻常 - 并非完全错误,但绝对不是你通常使用的方式。 它具有内置规则,可以从单个源文件构建程序,例如example.c example 。 通常,您根据其中一个源文件为程序提供有意义的名称。 请注意,创建程序test通常是一个坏主意; 在典型的shell中有一个标准的test命令,混乱很猖獗。


You need to remove the -c from the a.out command:

a.out: test.o extra.o
    gcc $(T) test.o extra.o

or, better:

a.out: test.o extra.o
    gcc $(T) -o $@ test.o extra.o

or, still better:

extra: test.o extra.o
    gcc $(T) -o $@ test.o extra.o

The error message is normally because you specify something like -lm on a command line with -c, but you're not doing that here. OTOH, you are listing object files with the -c option — that'll generate the warning:

gcc -ansi -pedantic -Wall -Werror -c test.o extra.o
                                   ^ this one, here

Those .o files are the linker inputs, but you're not linking. Drop the -c and you will generate a.out. That should proceed OK.

I think this is the first time I've seen a makefile used to build a.out. It is unusual in the extreme — not precisely wrong, but definitely not the way you normally use make. It has built-in rules to build programs from single source files, such as example from example.c. Normally, you give a program a meaningful name based on one of the source files. Note that creating a program test is usually a bad idea; there is a standard test command built into the typical shell and confusion is rampant.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。