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. 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. 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...
c++ Please read the instructions carefully. You must put the proper items in the proper file...
c++ Please read the instructions carefully. You must put the proper items in the proper file (eitehr SSandwich.h or SSandwich.cpp. Do NOT include any main function in your submission. You are encouraged to write your own main function to test what you are submitting. You are submit two files via BlackBoard: A file named SSandwich.h, a header file defining the class SSandwich. No inline methods are permitted. Any enum classes should be defined here also. Your header file should have...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
What is the band of stability and why is it important? Please give alot of details
What is the band of stability and why is it important? Please give alot of details
I paid to be able to post question here. Please read carefully and give quality answer....
I paid to be able to post question here. Please read carefully and give quality answer. there is nothing to clarify. just do what is required. Do not mess up my question as there are limited. Thanks. Choose at least two concepts from the following: equity method, fair value method, partnership, corporation, bankruptcy chapter 7, bankruptcy chapter 11, subsidiary, parent company, consolidation of financial statements, trust, estates, liquidation, partnerships termination, estates tax, acquisition, variable interest entity, EPS, income tax, reorganization....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT