C++ 算法扫描一个数字数组,并计算范围(0-24)(25-49)等之间的数字

C++ 算法扫描一个数字数组,并计算范围(0-24)(25-49)等之间的数字,c++,arrays,function,file-io,C++,Arrays,Function,File Io,该网站的新成员,但该网站的积极搜索者寻找答案 这件事让我有点困惑。。。希望在阅读了源文件后,你能帮助我解决这个难题 程序:存储数字数组(手动/从文件);扫描数组并计算特定范围内的数字。(0-24、24-49、50-74等,直到200);然后在屏幕上显示计数或保存到.txt文件中 // Program for teachers to input test grades. // Test scores range from 0-200. // Test scores can

该网站的新成员,但该网站的积极搜索者寻找答案

这件事让我有点困惑。。。希望在阅读了源文件后,你能帮助我解决这个难题

程序:存储数字数组(手动/从文件);扫描数组并计算特定范围内的数字。(0-24、24-49、50-74等,直到200);然后在屏幕上显示计数或保存到.txt文件中

    // Program for teachers to input test grades.
    // Test scores range from 0-200.
    // Test scores can either be input by file, or manually.
    // Program will then display the number of test scores that falls
    //  into quarter ranges.
    // Display options are either on screen or in file.


    #include "stdafx.h"
    #include <iostream>
    #include <fstream>

    using namespace std;

    // Due to being new with functions, I was not sure if these variables
    //  need to be constant or not...
    char scores;
    int students;
    int i,n, list[100];
    void openinfile(ifstream &infile);
    void openoutfile(ofstream &outfile);
    int count(int x, int y);
    ifstream inputfile;
    ofstream outputfile;

    // Using VS2013
    // Main program for options menu's and algorithms.
    int main(int argc, char *argv[])
    {


int total;
int qMenu1;
int qMenu2;




cout << "How many students are in your class?" << endl;
cin >> students;
cout << "How would you like to enter your test scores?" << endl;
cout << "1 - From File." << endl;
cout << "2 – Manually." << endl;
cin >> qMenu1;

if (qMenu1 = 1)
    openinfile(inputfile);

else if (qMenu1 = 2)
{   
    cout << "Please enter total number of students." << endl;
    cin >> students;            // Store array size in students.


    // Sets up the array for the user to enter all grades, 1 by 1
    // until the the entries = max number of array slots.
    for (i=0; i<students-1; i++)    
    {
        cout << "Enter test score #" << i << ":" << endl;
        cin >> list[i];
    }
}
else 
{
    cout << "Please enter a valid Number." << endl;
    cout << "(1 or 2)" << endl;
}



// Menu2: Display options. (On Screen or Save to File)
cout << "How would you like your results displayed?" << endl;
cout << "1 – On Screen." << endl;
cout << "2 – To File." << endl;
cin >> qMenu2;

if (qMenu2 = 1)
{
    cout << "TOTAL NUMBER OF STUDENTS WITHIN RANGE:" << endl;
    cout << endl;
    total = count(0, 24);           // Quarter ranges that the input 
    cout << "000 – 024\t" << total << endl; // needs to be checked against.
    total = count(25, 49);
    cout << "025 – 049\t" << total << endl;
    total = count(50, 74);
    cout << "050 – 074\t" << total << endl;
    total = count(75, 99);
    cout << "075 – 099\t" << total << endl;
    total = count(100, 124);
        cout << "100 – 124\t" << total << endl;
    total = count(125, 149);
        cout << "125 – 149\t" << total << endl;
    total = count(150, 174);
        cout << "150 – 174\t" << total << endl;
    total = count(174, 200);
        cout << "174 – 200\t" << total << endl;
}   
else
{
    openoutfile(outputfile);    

    outputfile << "TOTAL NUMBER OF STUDENTS WITHIN RANGE:" << endl;
    outputfile << endl;
    total = count(0, 24);
    outputfile << "000 – 024\t" << total << endl;
    total = count(25, 49);
    outputfile << "025 – 049\t" << total << endl;
    total = count(50, 74);
    outputfile << "050 – 074\t" << total << endl;
    total = count(75, 99);
    outputfile << "075 – 099\t" << total << endl;
    total = count(100, 124);
        outputfile << "100 – 124\t" << total << endl;
    total = count(125, 149);
        outputfile << "125 – 149\t" << total << endl;
    total = count(150, 174);
        outputfile << "150 – 174\t" << total << endl;
    total = count(174, 200);
        outputfile << "174 – 200\t" << total << endl;

    outputfile.close();     

    return 0;
}

    }

// Function to open file for reading and input in array.
void openinfile(ifstream &infile)
{
    char filename[100];

    cout << "Enter the file name: ";
    cin >> filename;
    infile.open(filename);

    if (infile.fail())
        cout << filename << "\tDoes not exist" << endl;

    else
    {
        cout << filename << "Successfully open" << endl;
    // read character until the end of file.
        for (int i = 0; i < 100; i++) 
        {
            infile >> list[i];
        }
    }
}

// function to open new file for writing result to.
void openoutfile(ofstream &outfile)
{
    char filename[100];

    cout << "Enter the file name to save.";
        cin >> filename;

    outfile.open(filename);
    if (outfile.fail())
    {
        cout << filename << " could not be saved." << endl;
    }
    else
    {
        cout << filename << "Successfully saved!" << endl;
    }
}

// function to count numbers within a certain range.
int count(int x, int y)         // ((x >= 0)&&(x <= 24))
{
    int numOf = 0;              // variable counter
    int target;



    target = (( target >= x)&&(target <= y));

    for(int cntr = 0; cntr < students; cntr++)  
    {
        if(list[students] == target)
        {
            numOf += 1; // adds 1 for every number found between
        }                       // range target.
        return numOf;           // returns number of occurances found.
    }
}
//供教师输入考试成绩的程序。
//考试分数在0-200分之间。
//考试分数可以通过文件输入,也可以手动输入。
//然后,程序将显示下降的测试分数数
//进入四分之一范围。
//显示选项在屏幕上或文件中。
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
//由于是新函数,我不确定这些变量
//是否需要保持不变。。。
字符分数;
国际学生;
int i,n,list[100];
空旷的露天填充物(ifstream&infile);
空腔开口排水管(流管和排水管);
整数计数(整数x,整数y);
ifstream输入文件;
流输出文件;
//使用VS2013
//选项菜单和算法的主程序。
int main(int argc,char*argv[])
{
整数合计;
int qMenu1;
int qMenu2;
cout学生;

cout您看到的不是修复代码本身,而是一个计数器。您需要一组计数器,而不仅仅是一个,并且您需要使用输入数字来确定要递增的计数器

如果我没看错你的介绍的话,你在这里有一个巨大的优势。你的“桶”代表一个连续的数字范围,每个范围大小相等

因此,如果您还没有从中找到答案,那么您需要一个计数器数组,您可以使用整数除以25来查找每个bucket的索引


这也应该比直接比较更简单。

您对比较感到困惑:

target = (( target >= x)&&(target <= y));
现在,有更好的方法可以做到这一点。例如,您可以使用整数除法对一个循环中的所有范围进行计数。如果您确定所有值都在有效范围内,则可以这样简单:

int counts[8] = {0};
for( int i = 0; i < students; i++ ) counts[list[i]/25]++;

如果测试分数在0到200之间,您应该创建“四分之一”范围,那么它应该是4个范围(不是8个):(0-49},{50-100},{101-150},{151-200}。对于这个程序来说,更好的术语是直方图,因为某个范围内的值没有被排序。输出是直方图,是的。虽然这个过程是桶排序,但它不会生成集合的总排序。它是“非比较排序”之一这通常只能给出大致的大小。不,这个过程也是一个直方图。这个代码与桶排序无关。对不起,我不清楚。我的意思是桶排序计算一组区间的代表。如果你需要做的部分是这样的计算,那么使用桶排序直接消除re-I换方向盘。因为我是新来的,我从来没有听说过柱状图或桶排序……但我以后一定会研究它们!谢谢你花时间帮忙!效果非常好!还有你关于咖啡和深夜的权利……哈哈,现在来修正我的文件输入,我会准备好的!非常感谢你!@Podis不用担心。我已经准备好了经过多年的学习,认识到咖啡和睡眠剥夺对代码的影响。玩得开心!
int counts[8] = {0};
for( int i = 0; i < students; i++ ) counts[list[i]/25]++;
void output( ostream & s ) {
    s << "TOTAL NUMBER OF STUDENTS WITHIN RANGE:" << endl;
    // etc...
}
if (qMenu2 = 1)
{
    output(cout);
} else {
    openoutfile(outputfile);
    output(outputfile);
    outputfile.close();
}