Question

In: Computer Science

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY.

alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD.

void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0.


void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list.


void displayCandidate(Candidate candidates[]) – prints the complete information about the candidate

.
Candidate First(Candidate candidates[]) – returns single struct element: candidate with highest score


Candidate Last(Candidate candidates[]) – returns single struct element: candidate with lowest score


void Votes(Candidate candidates[]) – function sorts the candidates[] array by number of votes, the order in candidates[] array is replaced


void Scores(Candidate candidates[]) – calculates the percentage score for each candidate. Use the following formula: ??????=(CandidateVotes)/(sum of votes)*100%

Correct line for the reference: F=John,L=Smith,V=3342

The line errors that your program needs to detect, are as follows:

incorrect token / separator, example in line 5: F=Steven,L=JohnV=4429 --- (comma missing) – lines with this error need to be ignored

space in token, example in line 3: F=Hillary,X=Clinton, V=1622 --- lines with this error need to be read, error fixed, data included in your dataset

empty line, example in line 6 – empty lines need to be ignored

Example Textfile

F=Michael,L=John,V=3342

F=Danny,L=Red,V=2003

F=Hillary,L=Clinton, V=1588

F=Albert,L=Lee,V=5332

F=Steven,L=JohnV=4429

*IMPORTANT* Please be DETAILED in explanations of each part of code. Beginner Coder. *IMPORTANT*

Code Skeleton We ***HAVE*** to follow. How Would i go about using this skeleton? YOU CANNOT CHANGE FUNCTIONS OF VARIABLES, ***BUT YOU MAY ADD TO IT***. THE CODE MUST HAVE WHAT IS LISTED IN THE SKELETON CODE:

#include <iostream>

#include <iomanip>

#include <stdlib.h>

#include <fstream>

#include <string>

using namespace std;

struct Candidate {
string Fname;
string Lname;
int votes;
double Score;
};

const int MAX_SIZE = 100;

void readFile(Candidate[]);

void List(Candidate[]);

void Votes(Candidate[]);

void displayCandidate(Candidate);

Candidate First(Candidate[]);

Candidate Last(Candidate[]);

void Scores(Candidate[]);

int main() {

}

void readFile(Candidate candidates[]) {

string line;

ifstream infile;

infile.open("elections.txt");

while (!infile.eof()) {

getline(infile,line);

// your code here

}

infile.close();

}

void List(Candidate candidates[]) {

}

void Votes(Candidate candidates[]) {

}

void displayCandidate(Candidate candidates) {

}

Candidate First(Candidate candidates[]) {

}

Candidate Last(Candidate candidates[]) {

}

void Scores(Candidate candidates[]) {

}

Solutions

Expert Solution

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <fstream>

#include <string>

using namespace std;

struct Candidate {

        string Fname;

        string Lname;

        int votes;

        double Score;

};

const int MAX_SIZE = 100;

void readFile(Candidate[]);

void List(Candidate[]);

void Votes(Candidate[]);

void displayCandidate(Candidate);

Candidate First(Candidate[]);

Candidate Last(Candidate[]);

void Scores(Candidate[]);

int main() {

        Candidate candidates[MAX_SIZE];

        readFile(candidates);

        Scores(candidates);

        Votes(candidates);

        cout << endl;

        List(candidates);

        

}


// trim a string, it removes any spaces at end and start of the string

string trim(string& str) {

        str.erase(0, str.find_first_not_of(' ')); //prefixing spaces

        str.erase(str.find_last_not_of(' ')+1); //surfixing spaces

        return str;

}

// this method counts the occurrences of a character in string.

int countCharacter(string s, char c) {

int count = 0;

for (int i = 0; i < s.size(); i++) {

if (s[i] == c) {

count++;

}

}

return count;

}

// this method parses the token which is in format:

// F=<firstName>

// L=<lastName>

// V=<Votes>

void parseToken(string s, Candidate &c) {

if(s[0] == 'F') {

c.Fname = s.substr(2);

} else if(s[0] == 'L') {

c.Lname = s.substr(2);

} else if(s[0] == 'V') {

c.votes = atoi(s.substr(2).c_str());

}

}

// this method converts a comma separated string to tokens

// it returns all tokens in form of a vector

void tokens(string s, Candidate &c) {

        

// while string has a comma

while(s.find(",") != string::npos) {

// find index of comma in string

int index = s.find(",");

// break token till comma

string token = s.substr(0, index);

// remove the token from original string

s = s.substr(index + 1);

// remove space from start of token, and push to vector of tokens

parseToken(trim(token), c);

}

// if last token is now not empty, push that too on vector

if(!s.empty()) {

parseToken(trim(s), c);

}

}


void readFile(Candidate candidates[]) {

        string line;

        ifstream infile;

        infile.open("elections.txt");

        if(infile.fail()) {

                cout << "unable to open file elections.txt" << endl;

                return;

        }

        int i = 0;

        while (!infile.eof()) {

// read a line from the file

getline(infile,line);

// Here comes the main logic to parse the line

int commaCount = countCharacter(line, ',');

if(commaCount != 2) {

// Ignore line,

cout << "Incorrect token separator: " << line << endl;

} else if(line.empty()) {

// ignore empty line.

} else {

// We now have a line which does contain correct commas

// and is not empty line.

tokens(line, candidates[i++]);

}

        }

        infile.close();

}

void List(Candidate candidates[]) {

        for(int i=0; i<MAX_SIZE; i++) {

                if(!candidates[i].Fname.empty()) {

                        displayCandidate(candidates[i]);

                }

        }

}

void Votes(Candidate candidates[]) {

        int size = MAX_SIZE;

        for(int i=0; i<MAX_SIZE; i++) {

                if(candidates[i].Fname.empty()) {

                        size = i;

                        break;

                }

        }


        for(int i=0; i<size-1; i++) {

                int minIndex = i;

                for(int j=i+1; j<size; j++) {

                        if(candidates[j].Score < candidates[minIndex].Score) {

                                minIndex = j;

                        }

                }

                Candidate tmp = candidates[i];

                candidates[i] = candidates[minIndex];

                candidates[minIndex] = tmp;

        }

}

void displayCandidate(Candidate candidate) {

        cout << candidate.Fname << " " << candidate.Lname << " " << candidate.Score << endl;

}

Candidate First(Candidate candidates[]) {

        double max = 0;

        Candidate result;

        for(int i=0; i<MAX_SIZE; i++) {

                if(!candidates[i].Fname.empty() && candidates[i].Score > max) {

                        max = candidates[i].Score;

                        result = candidates[i];

                }

        }

        return result;

}

Candidate Last(Candidate candidates[]) {

        double min = 100;

        Candidate result;

        for(int i=0; i<MAX_SIZE; i++) {

                if(!candidates[i].Fname.empty() && candidates[i].Score < min) {

                        min = candidates[i].Score;

                        result = candidates[i];

                }

        }

        return result;

}

void Scores(Candidate candidates[]) {

        int total = 0;

        for(int i=0; i<MAX_SIZE; i++) {

                if(!candidates[i].Fname.empty()) {

                        total += candidates[i].votes;

                }

        }

        

        for(int i=0; i<MAX_SIZE; i++) {

                if(!candidates[i].Fname.empty()) {

                        candidates[i].Score = candidates[i].votes * 100.0/total;

                }

        }

}

please upvote.


Related Solutions

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY....
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY. WE HAVE TO DO WHAT THIS ASSIGNMENT DOES OR WE WILL MARKED OFF POINTS. IT DOES NOT HELP WHEN YOU CHANGE THE SKELETON TO YOU'RE PREFERENCE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG IN TERMS OF WORKING CONDITIONS SO PLEASE HELP FIX THESE. *IMPORTANT* void readFile(Candidate candidates[]) – reads...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY ****IMPORTANT**** ***GOALS*** HOW TO CHECK FOR COMMAS, TILL THE END OF FILE. IT WILL CHECK THE LINE FOR THE APPRORIATE FORMAT IN THE TEXT FILE. IF THERE IS MISSING A COMMA, IT WILL IGNORE, IF THERE IS A WHITE SPACE, IT WILL CORRECT AND READ LINE, IF IT IS MISSING 1 OF THE 3 INFORMATION, IT WILL IGNORE. Display candidates’ names using displayList() function...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. void readFile(Candidate candidates[]) –...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list. void displayCandidate(Candidate candidates[]) – prints the complete information about the candidate . Candidate First(Candidate candidates[]) – returns single struct element: candidate...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. ***DO NOT REPLY ANUNAGA.*** HOW TO DO?...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. ***DO NOT REPLY ANUNAGA.*** HOW TO DO? *****IMPORTANT******* PLEASE READ CAREFULLY. WE HAVE TO DO WHAT THIS ASSIGNMENT DOES OR WE WILL MARKED OFF POINTS. IT DOES NOT HELP WHEN YOU CHANGE THE SKELETON TO YOU'RE PREFERENCE. YOU CAN ADD TO IT, BUT NOT CHANGE WHAT IS ALREADY THERE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? void readFile(Candidate candidates[]) –...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list. void displayCandidate(Candidate candidates[]) – prints the complete information about the candidate . Candidate First(Candidate candidates[]) – returns single struct element: candidate...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? void readFile(Candidate candidates[]) –...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list. void displayCandidate(Candidate candidates[]) – prints the complete information about the candidate . Candidate First(Candidate candidates[]) – returns single struct element: candidate...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
In python! please be thorough and read question carefully :) if possible, give explanation for code...
In python! please be thorough and read question carefully :) if possible, give explanation for code We’re going to create a class which stores information about product ratings on a website such as Amazon. The ratings are all numbers between 1 and 5 and represent a number of “stars” that the customer gives to the product. We will store this information as a list of integers which is maintained inside the class StarRatings. You will be asked to write the...
c++. please read this task carefully. First, try given examples of input to your code, if...
c++. please read this task carefully. First, try given examples of input to your code, if it works, then send it here. Create structure Applicants with following fields: struct applicants{ int id; string name; string surname; int subject1,subject2,subject3,selectedSubject; string specialCase; int total; }; SpecialCase (if applicant has "Awardee of Olympiads", then this field is "true", otherwise "false"). Do not forget that all "Awardees of Olympiads" will gain grants automatically. If the total points are same your algorithm have to choose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT