The programming language that i am required to use is C++ A poll was taken to de
ID: 3910652 • Letter: T
Question
The programming language that i am required to use is C++
A poll was taken to determine which of 6 vehicle models had the highest ranking amongst the population. Each vehicle model was assigned a unique code from 1 to 6. Each person polled was asked to select the top three in the order first, second and third. The information was compiled and saved in a text file, pol1.txt. Each line in the file corresponds to one person's preferences. For example the line 2 6 3 means that, for this person, vehicle model 2 is the first choice, vehicle model 6 is the second choice and vehicle model 3 is the third choice. The number of persons participating in the poll is unknown beforehand. The last line is the file contains the value 0 only, indicating the end of the data. Write a program to read the file and determine the results of the poll based on the following: All preferences must be validated; if any choice is invalid the entire line is rejected. A vehicle model selected first gets 5 points, the second choice gets 3 points and the third choice gets 1 point. . · The program must output The total number of persons participating in the poll The number of persons with valid preferences The number of persons with rejected selections A table showing the vehicle model number and their total number of points The highest ranking vehicle model. If there is a tie all vehicle models with the highest points are printedExplanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVehicleValid(int number) // Check if vehicle is valid
{
return number > 0 && number <= 6;
}
struct Model // Define model as model number and points
{
int num;
int points;
};
Model modelArray[6];
bool compareTwoModels(Model a, Model b) // Compare two models by matching their points
{
return a.points > b.points;
}
int main ()
{
int totalPersons;
int validPersons;
int points[6]; // store points for each model
int n = sizeof(points) / sizeof(points[0]);
for (int x; x < n; x++)
{
points[x] = 0;
}
ifstream myfile ("example.txt"); // Sample input file
if (myfile.is_open())
{
int a, b, c;
while (myfile >> a >> b >> c)
{
cout << a << " " << b << " " << c << " ";
totalPersons++;
if (isVehicleValid(a) && isVehicleValid(b) && isVehicleValid(c))
validPersons++;
else
continue; // Don't store points for rejected lines
points[a - 1] += 5;
points[b - 1] += 3;
points[c - 1] += 1;
// process pair (a,b)
}
}
else
cout << "Unable to open file";
cout << "Total Persons: " << totalPersons << " ";
cout << "Valid preferences: " << validPersons << " ";
cout << "Rejected selections: " << totalPersons - validPersons << " ";
for (int x; x < n; x++)
{
cout << "Model number " << x + 1 << " has " << points[x] << " points" << " "; // Print each model total points
modelArray[x].num = x + 1;
modelArray[x].points = points[x];
}
// Need to sort the points
sort(modelArray, modelArray + n, compareTwoModels);
cout << "Rankings are ";
int firstRanked = modelArray[0].points;
for (int x; x < n; x++)
{
if (modelArray[x].points == firstRanked)
cout << "Model no. " << modelArray[x].num << " with points: " << modelArray[x].points << " ";
else
break; // Don't print lower ranked models
}
}
I ran the above program on a sample input file and got the following results
File content is
2 6 3
4 3 1
1 6 3
0
Output is
2 6 3
4 3 1
1 6 3
Total Persons: 3
Valid preferences: 3
Rejected selections: 0
Model number 1 has 6 points
Model number 2 has 5 points
Model number 3 has 5 points
Model number 4 has 5 points
Model number 5 has 0 points
Model number 6 has 6 points
Rankings are
Model no. 1 with points: 6
Model no. 6 with points: 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.