我正在尝试显示哪个“轨道”编号被计算为最长和最短。我不确定对这样一个整数的调用是什么。目前我的程序只调用 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
是最长轨道的编号,而不是最长轨道的实际值。对最短轨道执行相同操作。
关于c++ - 在计算出最高和最低间隔后,如何获取要在控制台上读取的数组编号的下标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341319/