如何在计算最高和最低间隔后在控制台上读取数组编号的下标

How do I get the subscript of an array number to read on the console after calculating highest and lowest intervals?

本文关键字:读取 控制台 数组 编号 下标 计算      更新时间:2023-10-16

我正在尝试显示哪个"跟踪"数字被计算为最长和最短。我不确定对这样一个整数的调用是什么。目前我的程序只调用 0,这是我两者的起始值。任何帮助将不胜感激。

这是我正在谈论的代码部分。数组中有 12 个项目。

longest = albumlength[0];
for (count = 1; count < Num_Tracks; count++)
    if (albumlength[count] > longest)
        longest = albumlength[count];
shortest = albumlength[0];
for (count = 1; count < Num_Tracks; count++)
    if (albumlength[count] < shortest)
        shortest = albumlength[count];
int total = 0;
int average;
for (int count = 0; count < Num_Tracks; count++)
    total += albumlength[count];
average = total / Num_Tracks;
cout << endl;
cout << "The longest track is #" << albumlength[longest] << " at ";
displayTime(longest);
cout << "The shortest track is #" << albumlength[shortest] << " at ";
displayTime(shortest);
cout << "The total length of the album is: ";
displayTime(total);
cout << "The average length of a track is: ";
displayTime(average);

代码正在计算实际的最长和最短值,然后您将该值用作曲目编号的索引。这是没有道理的。

如果您想弄清楚"计算了哪个'轨道'编号",请准确操作:跟踪轨道编号本身,而不是实际的最短或最长值:

longest = 0;
for (count = 1; count < Num_Tracks; count++)
    if (albumlength[count] > albumlength[longest])
        longest = count;

现在longest是最长轨道的数量,而不是最长轨道的实际值。对最短的轨道执行相同的操作。