Parralrel工作但未达到预期效果

Parrallel Working But Not To Desired Effect

本文关键字:未达到 工作 Parralrel      更新时间:2023-10-16

我在大学里被分配了一项作业。我被要求创建一个c++控制台应用程序,该应用程序将从一个简单的.txt文件中读取五个国家的内容,并允许用户为每个团队分配投票[6,8,10,12]。每个团队只能投票一次,不得重复得分,也不得为自己投票。然后我们必须使用气泡排序按降序向用户显示分数。实际上,应用程序的每个元素都在工作,直到我必须按顺序打印分数。我在计算总分和"getValidCountry()"中的while循环时遇到了困难

#include <iostream> 
#include <fstream>      // include the 'fstream' standard library header file 
#include <string> 
#include <vector>
#include <algorithm>
using namespace std;
//VARIABLES
#define POINTS 4
#define NUM_COUNTRIES 5
int points[POINTS] = {6, 8, 10, 12};
vector<string> countries; 
int countryInd[NUM_COUNTRIES] = {0, 1, 2, 3, 4};
int votes [NUM_COUNTRIES][NUM_COUNTRIES];
int voteTotal[NUM_COUNTRIES] = {0,0,0,0,0};
//METHOD HEADINGS
void printarray (int arg[], int length);
void voting(int votingCountry);
int getValidVote();
void printRankedTable();
void printContestants();
void BubbleSort(int arr[], int n);
void resetPoints();
int getValidCountry();
int main ()
{
    ifstream inFile;  
    inFile.open("countries.txt");   // bind the inFile stream to a file name  
    if( !inFile )                // test to see if it opened successfully  
    {  
        cout << "Failed to open file." << endl;     
        exit( EXIT_FAILURE );  
    }
    int i = 0; 
    string str;  
    string fileContents;  
    //RESERVE 5 SPACES IN THE VECTOR
    countries.reserve(NUM_COUNTRIES);
    vector<string>::iterator iter;

    while( getline( inFile, str ) )  
    {  
        countries.push_back(str);
    }  
    inFile.close();
    printContestants();
    int round = 5;
    int cInd = -1;
    while(round > 0)
    {
        cout << endl;
        cInd = getValidCountry();
        voting(cInd);
        BubbleSort(voteTotal, NUM_COUNTRIES);
        printRankedTable();
        cInd = -1;
        round--;
    }
}
int getValidCountry()
{
    bool temp = false;
    string foo;
    cout << "Enter Country You Wish To Vote For: ";
    getline(cin, foo);
    while(!temp)
    {
        for(int i = 0; i < NUM_COUNTRIES; i++)
        {
            if(foo.compare(countries.at(countryInd[i])) == 0)
            {
                temp = true;
                return countryInd[i];
            }
        }
        cout << "[ERROR]: Enter A Valid Country: ";
        getline(cin, foo);
    }
    return -1;
}

void printarray (int arg[], int length) {
    for (int n=0; n<length; ++n)
        cout << arg[n] << ' ';
    cout << 'n';
}
void voting(int votingCountry)
{
    int score = 0;
    for(int i = 0; i < NUM_COUNTRIES; i++)
    {
        if(countryInd[i] != votingCountry)
        {
            cout << "Please Enter Score For " << countries.at(countryInd[i]) << ": ";
            score = getValidVote();
            cout << score << endl;
            voteTotal[countryInd[i]] = voteTotal[countryInd[i]] + score;
            cout << "vote total: " << voteTotal[countryInd[i]] << "t CountryInd: " << countryInd[i] << endl;
        }
    }
    resetPoints();
    cout << endl;
}
void resetPoints()
{
    points[0] = 6;
    points[1] = 8;
    points[2] = 10;
    points[3] = 12;
}

int getValidVote()
{
    bool isValid = false;
    int vote;
    while(!isValid)
    {
        cin >> vote;
        for(unsigned int i = 0; i <= POINTS; i++)
        {
            if (vote == points[i] && vote > -1)
            {
                isValid = true;
                points[i] = -1;
                return vote;
            }
        }
        cout << "Enter Valid Score: ";
    }
    return 0;
}

void printContestants()
{
    cout << endl << "EUROVISION CONTESTANTSn";
    for(unsigned int i = 0; i < NUM_COUNTRIES; i++)
    {
        cout << countries[countryInd[i]] << endl;
    }
    cout << endl;
}
void printRankedTable()
{
    cout << endl << "EUROVISION CONTESTANTS [RANKED]n";
    for(unsigned int i = 0; i < NUM_COUNTRIES; i++)
    {
        cout << countries.at(countryInd[i]) << "t" << voteTotal[i] << endl;
    }
    cout << endl;
}
void BubbleSort(int arr[], int n) 
{
    bool swapped = true;
    int j = 0;
    int tmp;
    int tmpC;
    while (swapped) 
    {
        swapped = false;
        j++;
        for (int i = 0; i < n - j; i++) 
        {
            if (arr[i] < arr[i + 1]) 
            {
                tmp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = tmp;
                tmpC = countryInd[i];
                countryInd[i] = countryInd[i+1];
                countryInd[i+1] = tmpC;
                swapped = true;
            }
        }
    }
}   

问题:

当您是voting()时,您使用countryInd索引将分数输入到voteTotal

voteTotal[countryInd[i]] = voteTotal[countryInd[i]] + score;  // in voting()

因此,您希望国家名称的顺序及其相关的分数id都使用countryInd[]中定义的访问顺序。

// score of countries.at(countryInd[i]) corresponds to valueTotal[countryInd[i]]

BubbleSort()时,将交换countryInd[]中的元素和voteTotal[]中的元素。这就是你的问题的原因,因为国家名称的正确访问顺序仍然是countryInd[i],但现在是i表示分数:

//score of countries.at(countryInd[i]) corresponds now to valueTotal[i]

你已经知道了,因为这正是你在printRankedTable() 中使用的逻辑

不幸的是,当您进行第二次投票时,您再次假设两个表使用countryInd[]中定义的相同访问序列,这是一个不再有效的假设。你最终会把一个国家的投票加在另一个国家。

解决方案

BubbleSort()中if子句的更改如下:

if (arr[countryInd[i]] < arr[countryInd[i + 1]]) // indirect access through countryInd
        {
            //tmp = arr[i];           delete => no longuer needed
            //arr[i] = arr[i + 1];    delete => no longuer needed
            //arr[i + 1] = tmp;       delete => no longuer needed
            tmpC = countryInd[i];
            countryInd[i] = countryInd[i + 1];
            countryInd[i + 1] = tmpC;
            swapped = true;
        }

还将printRankedTable()的输出指令更新为:

 cout << countries.at(countryInd[i]) << "t" << voteTotal[countryInd[i]] << endl;  // only indirect access via countryInd