Java作业MaxSumTest

发布于 2025-01-06 12:00:45 字数 2251 浏览 0 评论 0原文

我目前正在尝试修改我的数据结构类的 Java 家庭作业程序,该程序在表中显示 MaxSumTest 程序的输出。

我创建了四个数组,并将它们放置在计算计时信息的循环之一内。我试图仅使用一种算法的计时信息填充每个数组。所以每个数组有 4 个元素。然而,每次我运行程序的修订版时,我都会收到越界错误。

这是原文

我修改的唯一方法是 getTimingInfo。这是我的修订:

public static void getTimingInfo( int n, int alg )
{
    int [] test = new int[ n ];

    long startTime = System.currentTimeMillis( );;
    long totalTime = 0;



   //create an array for each Algorithm
    long[] alg4;
    long[] alg3;
    long[] alg2;
    long[] alg1;

    //allocate memory for 5 long ints 

    alg4 = new long[5];
    alg3 = new long[5];
    alg2 = new long[5];
    alg1 = new long[5];

   int i;
   int j;
   int index = 0;

   for( i = 0; totalTime < 4000; i++ )
   {
       for( j = 0; j < test.length; j++ )
           test[ j ] = rand.nextInt( 100 ) - 50;
    index = j;
       switch( alg )
       {
         case 1:
           maxSubSum1( test );
           break;
         case 2:
           maxSubSum2( test );
           break;
         case 3:
           maxSubSum3( test );
           break;
         case 4:
           maxSubSum4( test );
           break;
       }

       totalTime = System.currentTimeMillis( ) - startTime;

   }

    alg1[index] = totalTime * 1000 / i;
    alg2[index] = totalTime * 1000 / i;
    alg3[index] = totalTime * 1000 / i;
    alg4[index] = totalTime * 1000 / i;

    //Build first column of table
    System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");

    System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
    System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
    System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
    System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);


    /*

    System.out.println( "Algorithm #" + alg + "\t"
         + "N = " + test.length
         + "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
    */


 }

任何正确方向的建议或指示将不胜感激。

I am currently attempting to revise a Java Homework program for my Data Structures class that displays the output of the MaxSumTest program in a table.

I've created four arrays, and placed them inside one of the loops that calculates the timing info. I am trying to populate each array with only the timing info for one algorithm. So each array would have 4 elements. However, every time I run my revision of the program, I get an out of bounds error.

Here is the Original

The only method I revised is getTimingInfo. Here is my revision:

public static void getTimingInfo( int n, int alg )
{
    int [] test = new int[ n ];

    long startTime = System.currentTimeMillis( );;
    long totalTime = 0;



   //create an array for each Algorithm
    long[] alg4;
    long[] alg3;
    long[] alg2;
    long[] alg1;

    //allocate memory for 5 long ints 

    alg4 = new long[5];
    alg3 = new long[5];
    alg2 = new long[5];
    alg1 = new long[5];

   int i;
   int j;
   int index = 0;

   for( i = 0; totalTime < 4000; i++ )
   {
       for( j = 0; j < test.length; j++ )
           test[ j ] = rand.nextInt( 100 ) - 50;
    index = j;
       switch( alg )
       {
         case 1:
           maxSubSum1( test );
           break;
         case 2:
           maxSubSum2( test );
           break;
         case 3:
           maxSubSum3( test );
           break;
         case 4:
           maxSubSum4( test );
           break;
       }

       totalTime = System.currentTimeMillis( ) - startTime;

   }

    alg1[index] = totalTime * 1000 / i;
    alg2[index] = totalTime * 1000 / i;
    alg3[index] = totalTime * 1000 / i;
    alg4[index] = totalTime * 1000 / i;

    //Build first column of table
    System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");

    System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
    System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
    System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
    System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);


    /*

    System.out.println( "Algorithm #" + alg + "\t"
         + "N = " + test.length
         + "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
    */


 }

Any advice or pointers in the right direction would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转瞬即逝 2025-01-13 12:00:45

更新数组元素的行不应该位于内部循环内部吗?

alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;

您在循环外部分配它们,其中 index 的值为 5,因此超出范围。

Shouldn't the lines that update the array elements be inside the inner loop?

alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;

You are assigning them outside the loop, where the value of index is 5, so it's out of bounds.

时光瘦了 2025-01-13 12:00:45

我不确定您到底想做什么,但我认为您真正想做的是多次调用 getTimingInfo() ,然后将结果分配给表,而不是更改getTimingInfo() 的内部工作原理。

这要求您定义将在 getTimingInfo() 外部存储计时数据的数组,然后 getTimingInfo() 内部的唯一更改将存储到该数组(已经定义的)数组,而不是打印。

I'm not sure exactly what you are trying to do, but I think that what you really want to do is call getTimingInfo() a number of times and then assign the results to a table, not change the internal workings of getTimingInfo().

This requires that you define the arrays in which you will store the timing data OUTSIDE of getTimingInfo(), and then the only change inside of getTimingInfo() would be storing to that (already defined) array, rather than printing.

山色无中 2025-01-13 12:00:45
long[] alg1;
alg4 = new long[5];

可以更好地表达为

long[] alg1 = new long[5];

maxSubSum3( test );

“写得更好”“

maxSubSum3(test);

int j;
int index = 0;

实际上做同样的事情”。

只是挑几点。

long[] alg1;
alg4 = new long[5];

Can be better expressed as

long[] alg1 = new long[5];

maxSubSum3( test );

Is written nicer as

maxSubSum3(test);

int j;
int index = 0;

Actually do the same thing.

Just to pick a few points.

浅浅 2025-01-13 12:00:45

您将 alg4、alg3、alg2 和 alg1 设置为大小 5,而不是您所说的需要的大小 4。

You are setting the alg4, alg3, alg2, and alg1 to size 5, not size 4 as you said you needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文