Parralrel工作但未达到预期效果
Parrallel Working But Not To Desired Effect
我在大学里被分配了一项作业。我被要求创建一个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
相关文章:
- 为什么stream::忽略未按预期工作
- 随机数未达到限制
- 未达到的情况会影响开关外壳性能
- 输入数据成员未按要求工作
- 在编译时,C++项目抛出错误 C2228,这是预期的,因为控件在运行时未达到该点
- 排序功能在C++中未按预期工作
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- C++ 在条件未按预期工作时执行
- Poco 库 PUT 方法未按预期工作,尽管主机、方法、内容类型设置正确
- 我无法让我的程序工作,我一直得到未定义的符号:C
- 如果逻辑未按预期工作
- 模板元编程三元值未达到基本情况
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 表达式中的宏未按预期工作
- C++ 正则表达式未按预期工作 (regex_search)
- 连接循环准确的Systemc编码样式,具有未达到的编码样式
- 为什么未达到的 try-catch 块会增加运行时时间
- 在功能中操纵未达到的阵列
- Parralrel工作但未达到预期效果
- 长双精度未达到要求