简体   繁体   English

用于扫描数字数组的算法,并计算在范围(0 - 24)(25 - 49)​​等之间的数量

[英]Algorithm to scan an array of numbers, and count how many are between a range (0 - 24)(25 - 49) etc

New Member to the site, but an active searcher of the site to find answers. 该网站的新成员,但该网站的活跃搜索者,以找到答案。

This one is a bit confusing for me to figure out... Hopefully after reading the source file you can help with my dilemma. 这个对我来说有点令人困惑......希望在阅读源文件后,你可以帮助解决我的困境。

Program: Store an array of numbers (manually/from file); 程序:存储一组数字(手动/从文件); Scan the array and count the numbers between certain ranges. 扫描阵列并计算特定范围之间的数字。 (0-24, 24-49, 50-74 and so on until 200); (0-24,24-49,50-74等,直到200); Then display the count either on screen or saved to a .txt file. 然后在屏幕上显示计数或保存到.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.
    }
}

The last function is where my problem lies, I'm not quite sure how to go about this. 最后一个功能是我的问题所在,我不太清楚如何解决这个问题。 Any help would be wonderful! 任何帮助都会很精彩! -Chris- -克里斯-

You got confused about the comparison: 你对比较感到困惑:

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

target is undefined, but you are checking if it is in the desired range and then setting it to a boolean value. target未定义,但您正在检查它是否在所需范围内,然后将其设置为布尔值。 You are then testing whether any element of the array is equal to that value. 然后,您将测试数组中的任何元素是否等于该值。 Also, you're returning inside the loop after the first iteration. 此外,您在第一次迭代后返回循环内部。

Let's fix all this, and I'm sure you'll realise that it's just late at night and you've had too much coffee and you weren't thinking straight: 让我们解决所有这些问题,我相信你会意识到这已经是深夜了,而你喝的咖啡太多而且你没有直接思考:

int count(int x, int y)
{
    int numOf = 0;
    for(int cntr = 0; cntr < students; cntr++) {
        if( list[ctr] >=x && list[ctr] <= y ) numOf++;
    }
    return numOf;
}

Now, there are way better ways to do this. 现在,有更好的方法来做到这一点。 For example, you could count ALL the ranges in one loop by using integer division. 例如,您可以使用整数除法计算一个循环中的所有范围。 If you know for sure that all your values are in the valid range, it can be as simple as this: 如果您确定所有值都在有效范围内,则可以这样简单:

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

Also, there's no reason to duplicate your code that outputs just because you're writing to a stream. 此外,没有理由因为您正在写入流而复制输出的代码。 Move the code into a function: 将代码移动到一个函数中:

void output( ostream & s ) {
    s << "TOTAL NUMBER OF STUDENTS WITHIN RANGE:" << endl;
    // etc...
}

Then: 然后:

if (qMenu2 = 1)
{
    output(cout);
} else {
    openoutfile(outputfile);
    output(outputfile);
    outputfile.close();
}

Rather than fix the code itself, what you're looking at is called a Bucket Sort . 您正在查看的内容称为Bucket Sort ,而不是修复代码本身。 You want an set of counters, rather than just one, and you want to use your input numbers to determine which counter to increment. 您需要一组计数器,而不仅仅是一个,并且您希望使用输入数字来确定要递增的计数器。

You have a huge advantage, here, if I'm reading your introduction correctly. 如果我正确地阅读您的介绍,那么您有一个巨大的优势。 Your "buckets" are for a continuous range of numbers, each range being of equal size. 您的“桶”用于连续的数字范围,每个范围的大小相同。

So, if you haven't already figured it out from that, you would want an array of counters and you can use integer division by 25 to find the index of each bucket. 所以,如果你还没有想到它,你会想要一个计数器数组,你可以使用整数除以25来找到每个桶的索引。

That should also be more straightforward than keeping the comparisons straight. 这也应该比直接比较更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM