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

时间:2014-03-23 21:40:34

标签: 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 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.
    }
}

最后一个功能是我的问题所在,我不太确定如何解决这个问题。任何帮助都会很精彩! -Chris -

2 个答案:

答案 0 :(得分:2)

你对比较感到困惑:

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

target未定义,但您正在检查它是否在所需范围内,然后将其设置为布尔值。然后,您将测试数组中的任何元素是否等于该值。此外,您将在第一次迭代后返回循环内部。

让我们解决所有这些问题,我相信你会意识到这已经是深夜了,你喝的咖啡太多了,而你并没有直接思考:

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;
}

现在,有更好的方法可以做到这一点。例如,您可以使用整数除法计算一个循环中的所有范围。如果您确定所有值都在有效范围内,则可以这么简单:

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();
}

答案 1 :(得分:0)

不是修复代码本身,而是将您所看到的内容称为Bucket Sort。您需要一组计数器,而不仅仅是一个,并且您希望使用输入数字来确定要递增的计数器。

如果我正确地阅读您的介绍,那么您有一个巨大的优势。您的“桶”用于连续的数字范围,每个范围的大小相同。

所以,如果你还没有想到它,你会想要一个计数器数组,你可以使用整数除以25来找到每个桶的索引。

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