Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组上迭代时某些数据未被解释 我正在从我的C++书籍中进行随机练习,因为我正在“重新学习”C++,但是我从我编写的程序中得到了一些奇怪的输出。我相当肯定程序的逻辑中没有错误,但是如果“scoreCount”数组中的元素之和应该是26,与scores数组的长度相同,那么它只有20。我不知道其他6个元素发生了什么。下面的代码中描述了该练习。谁能看出我可能做错了什么 /* Exercise 09 - 04 Write a program that reads a file consisting of students' test scores in the range 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #include <cstdio> int main(int argc, char ** argv) { int scores[] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size = sizeof(scores) / sizeof(scores[0]); int scoreCount[] = {0, 0, 0, 0, 0, 0, 0, 0}; printf("Number of Scores: %d\n\n", size); for(int i = 0; i < size; i++) { scoreCount[((int)(scores[i]/25))] += 1; printf("%d - scoreCount Index: %d\n", i, ((int)(scores[i]/25))); } printf("\n"); int low = 0; int high = 24; size = sizeof(scoreCount) / sizeof(scoreCount[0]); for(int i = 0; i < size; i++) { printf("Range %d-%d: %d\n", low, high, scoreCount[i]); low += 25; high += 25; if(high == 199) high = 200; } int sum = 0; for(int i = 0; i < size; i++) { sum += scoreCount[i]; } if(sum < 26) printf("\n%d: Wrong number of scores counted.\n", sum); else printf("\nAll students accounted for.\n"); return 0; } /*练习09-04 编写一个程序,读取包含学生考试成绩的文件 在0-200范围内。然后,它应该确定有多少学生 以下各范围的分数:0-24、25-49、50-74、75-99、, 100-124、125-149、150-174和175-200。输出分数范围和 学生人数。(使用以下输入数据运行程序: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #包括 int main(int argc,字符**argv){ 整数分数[]={76,89,150,135,200,76,12,100,150,28,178,189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size=sizeof(分数)/sizeof(分数[0]); int scoreCount[]={0,0,0,0,0,0,0}; printf(“分数数:%d\n\n”,大小); 对于(int i=0;i_C++_Arrays - Fatal编程技术网

在数组上迭代时某些数据未被解释 我正在从我的C++书籍中进行随机练习,因为我正在“重新学习”C++,但是我从我编写的程序中得到了一些奇怪的输出。我相当肯定程序的逻辑中没有错误,但是如果“scoreCount”数组中的元素之和应该是26,与scores数组的长度相同,那么它只有20。我不知道其他6个元素发生了什么。下面的代码中描述了该练习。谁能看出我可能做错了什么 /* Exercise 09 - 04 Write a program that reads a file consisting of students' test scores in the range 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #include <cstdio> int main(int argc, char ** argv) { int scores[] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size = sizeof(scores) / sizeof(scores[0]); int scoreCount[] = {0, 0, 0, 0, 0, 0, 0, 0}; printf("Number of Scores: %d\n\n", size); for(int i = 0; i < size; i++) { scoreCount[((int)(scores[i]/25))] += 1; printf("%d - scoreCount Index: %d\n", i, ((int)(scores[i]/25))); } printf("\n"); int low = 0; int high = 24; size = sizeof(scoreCount) / sizeof(scoreCount[0]); for(int i = 0; i < size; i++) { printf("Range %d-%d: %d\n", low, high, scoreCount[i]); low += 25; high += 25; if(high == 199) high = 200; } int sum = 0; for(int i = 0; i < size; i++) { sum += scoreCount[i]; } if(sum < 26) printf("\n%d: Wrong number of scores counted.\n", sum); else printf("\nAll students accounted for.\n"); return 0; } /*练习09-04 编写一个程序,读取包含学生考试成绩的文件 在0-200范围内。然后,它应该确定有多少学生 以下各范围的分数:0-24、25-49、50-74、75-99、, 100-124、125-149、150-174和175-200。输出分数范围和 学生人数。(使用以下输入数据运行程序: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #包括 int main(int argc,字符**argv){ 整数分数[]={76,89,150,135,200,76,12,100,150,28,178,189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size=sizeof(分数)/sizeof(分数[0]); int scoreCount[]={0,0,0,0,0,0,0}; printf(“分数数:%d\n\n”,大小); 对于(int i=0;i

在数组上迭代时某些数据未被解释 我正在从我的C++书籍中进行随机练习,因为我正在“重新学习”C++,但是我从我编写的程序中得到了一些奇怪的输出。我相当肯定程序的逻辑中没有错误,但是如果“scoreCount”数组中的元素之和应该是26,与scores数组的长度相同,那么它只有20。我不知道其他6个元素发生了什么。下面的代码中描述了该练习。谁能看出我可能做错了什么 /* Exercise 09 - 04 Write a program that reads a file consisting of students' test scores in the range 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #include <cstdio> int main(int argc, char ** argv) { int scores[] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size = sizeof(scores) / sizeof(scores[0]); int scoreCount[] = {0, 0, 0, 0, 0, 0, 0, 0}; printf("Number of Scores: %d\n\n", size); for(int i = 0; i < size; i++) { scoreCount[((int)(scores[i]/25))] += 1; printf("%d - scoreCount Index: %d\n", i, ((int)(scores[i]/25))); } printf("\n"); int low = 0; int high = 24; size = sizeof(scoreCount) / sizeof(scoreCount[0]); for(int i = 0; i < size; i++) { printf("Range %d-%d: %d\n", low, high, scoreCount[i]); low += 25; high += 25; if(high == 199) high = 200; } int sum = 0; for(int i = 0; i < size; i++) { sum += scoreCount[i]; } if(sum < 26) printf("\n%d: Wrong number of scores counted.\n", sum); else printf("\nAll students accounted for.\n"); return 0; } /*练习09-04 编写一个程序,读取包含学生考试成绩的文件 在0-200范围内。然后,它应该确定有多少学生 以下各范围的分数:0-24、25-49、50-74、75-99、, 100-124、125-149、150-174和175-200。输出分数范围和 学生人数。(使用以下输入数据运行程序: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */ #包括 int main(int argc,字符**argv){ 整数分数[]={76,89,150,135,200,76,12,100,150,28,178,189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int size=sizeof(分数)/sizeof(分数[0]); int scoreCount[]={0,0,0,0,0,0,0}; printf(“分数数:%d\n\n”,大小); 对于(int i=0;i,c++,arrays,C++,Arrays,谢谢你的帮助 scoreCount数组中的元素数应该是9而不是8 As 200/25将被评估为8,这将导致索引超出范围 编辑: 正如Andrew_CS所建议的,值为200的元素应该添加到最后一个组本身 scoreCount[(scores[i]/25)==8?7:(scores[i]/25)] += 1; scoreCount数组中的元素数应该是9而不是8 As 200/25将被评估为8,这将导致索引超出范围 编辑: 正如Andrew_CS所建议的,值为200的元素应该添加到最后一个组本身 s

谢谢你的帮助

scoreCount数组中的元素数应该是9而不是8

As 200/25将被评估为8,这将导致索引超出范围

编辑:

正如Andrew_CS所建议的,值为200的元素应该添加到最后一个组本身

scoreCount[(scores[i]/25)==8?7:(scores[i]/25)] += 1;

scoreCount数组中的元素数应该是9而不是8

As 200/25将被评估为8,这将导致索引超出范围

编辑:

正如Andrew_CS所建议的,值为200的元素应该添加到最后一个组本身

scoreCount[(scores[i]/25)==8?7:(scores[i]/25)] += 1;
如果分数[i]为200,则产生8。目前,您的代码无法处理此问题

如果分数[i]为200,则产生8。目前您的代码无法处理此问题。

200/25=8和 记分计数[8]不存在。

200/25=8和
scoreCount[8]不存在。

您正在为
scoreCount
生成一个超出范围的索引,此处:

scoreCount[((int)(scores[i]/25))] += 1;

scoreCount
的有效索引范围为
0到7
,但使用当前的
scores
可以生成多达
8
的索引。解决方法是将
scoreCount
扩展一个元素,或者检查索引是否为
8
,并将其映射到索引
7
,因为问题似乎限制了您的范围。

您正在为
scoreCount
生成一个超出范围的索引,这里:

scoreCount[((int)(scores[i]/25))] += 1;

scoreCount
的有效索引范围为
0到7
,但使用当前的
scores
可以生成多达
8
的索引。解决方法是将
scoreCount
扩展一个元素,或者检查索引是否为
8
,并将其映射到索引
7
,因为问题似乎限制了您的范围。

我在我的机器上运行了代码。 26分中我得了23分

原因是有3个值为200。
当值为200时,它将小于8。

我在我的机器上运行了代码。 26分中我得了23分

原因是有3个值为200。
当值为200时,它将在8以下。

< P>这里是正确的C++风格。我知道这个问题已经得到了回答,但这里有一个好处:

#include <vector>
#include <map>
#include <iostream>

int main(int argc, char ** argv)
{
    const std::vector<int> scores = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189,
                                     167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
                                     35, 157, 189
                                    };
    std::map<int, int> scoreCount;
    std::cout << "Number of Scores: " << scores.size() << "\n";

    for(auto score : scores)
    {
        scoreCount[score/25] ++;
        std::cout <<  score << " - scoreCount Index: " << score/25 << "\n";
    }

    for(auto const& slot : scoreCount)
    {
        auto low  = slot.first*25;
        auto high = low+24;
        std::cout << "Range " << low << "-" << high << ": " << slot.second << "\n";
    }

    int sum = 0;
    for(auto const& slot : scoreCount)
        sum += slot.second;

    if(sum < 26)
        printf("\n%d: Wrong number of scores counted.\n", sum);
    else
        printf("\nAll students accounted for.\n");
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
向量得分={76,89,150,135,200,76,12,100,150,28,178,189,
167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
35, 157, 189
};
地图计分计数;

STD:CUT< P>这里是正确的C++风格。我知道问题已经得到了答案,但这里有一个额外的好处:

#include <vector>
#include <map>
#include <iostream>

int main(int argc, char ** argv)
{
    const std::vector<int> scores = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189,
                                     167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
                                     35, 157, 189
                                    };
    std::map<int, int> scoreCount;
    std::cout << "Number of Scores: " << scores.size() << "\n";

    for(auto score : scores)
    {
        scoreCount[score/25] ++;
        std::cout <<  score << " - scoreCount Index: " << score/25 << "\n";
    }

    for(auto const& slot : scoreCount)
    {
        auto low  = slot.first*25;
        auto high = low+24;
        std::cout << "Range " << low << "-" << high << ": " << slot.second << "\n";
    }

    int sum = 0;
    for(auto const& slot : scoreCount)
        sum += slot.second;

    if(sum < 26)
        printf("\n%d: Wrong number of scores counted.\n", sum);
    else
        printf("\nAll students accounted for.\n");
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
向量得分={76,89,150,135,200,76,12,100,150,28,178,189,
167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
35, 157, 189
};
地图计分计数;

std::cout您需要增加
scoreCount
数组中的元素数,因为
200/25
给出
8
scoreCount[8]
不存在。

您需要增加
scoreCount
数组中的元素数,因为
200/25
给出
8
scoreCount[8] 
不存在。

200/25=8超出scoreCount的范围-只有索引0-7

我个人不会增加scoreCount中的元素数量,因为只有8组有效分数。我会检查这个案例,它生成8作为索引

for(int i = 0; i < size; i++){
    int theIndex = (scores[i]/25);
    if(theIndex == 8)
        scoreCount[7] += 1;
    else
        scoreCount[theIndex] += 1;
    printf("%d - scoreCount Index: %d\n", i, theIndex;
}
for(int i=0;i