Question

In: Computer Science

C++ Write a program that reads candidate names and numbers of votes in from a file....

C++

Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays.

In order to allocate the arrays you will need to know the number of records in the file. In order to determine the number of records in the file you may include the number as metadata in the first line of the file or you may read through the file to count the lines and then reset the file pointer back to the beginning of the file to read in the data. Do not hard code the number of candidates. Do not prompt the user to enter the number of candidates. You must either count the number of candidates in the file or encode the number as metadata as the first record in the file.

The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. You may, but are not required to, use the example file provided.

Solutions

Expert Solution

C++ Program:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

const int no0fcandidate = 100;

int main()
{
int cnt;
string cNames[no0fcandidate];
string cLNames[no0fcandidate];
int votes[no0fcandidate];
float voteP[no0fcandidate];
int totalVotes = 0;
float maxVotesPercentage;
string winner;

//Opening file for reading
fstream fin("votes.txt", ios::in);

//Reading number of contestants
fin >> cnt;

for (int i = 0; i < cnt; i++)
{
fin >> cNames[i] >> cLNames[i] >> votes[i];
}

//Calculating total number of votes
for (int i = 0; i < cnt; i++)
{
totalVotes = totalVotes + votes[i];
}

//Finding average
for (int i = 0; i < cnt; i++)
{
voteP[i] = ((float) votes[i] / totalVotes) * 100;
}

//Percentage
maxVotesPercentage = voteP[0];

//Winner
winner = cNames[0];

//Calculating winner
for (int i = 0; i < cnt; i++)
{
//Comparing
if (voteP[i] > maxVotesPercentage)
{
maxVotesPercentage = voteP[i];

winner = cNames[i];
}
}

//Printing result
cout << endl << left << setw(15) << " " << left << setw(25) << "Candidate " << left << setw(20) << "Votes Received" << left << setw(15) << "% of Total Votes" << endl << endl;
cout << fixed << setprecision(2);

for (int i = 0; i < cnt; i++)
{
cout << left << setw(6) << " " << left << setw(20) << cNames[i] << left << setw(20) << cLNames[i] << left << setw(20) << votes[i] << left << setw(15) << voteP[i] << endl;
}

cout << endl << left << setw(6) << " " << left << setw(20) << "Total " << left << setw(20) << totalVotes << endl;

cout << endl << "The Winner of the Election is " << winner << endl;
return 0;
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
program to get votes for candidate a #dem #rep #lib #ind= total votes candidate b dem#...
program to get votes for candidate a #dem #rep #lib #ind= total votes candidate b dem# rep# lib# ind# = total votes print a or b winner in python i am writing my own program trying to input numbers for democrat republican libiraterians and indipendents voters for each candidate. i need to be able to change the percentage of voters and thus the totals for each group at the end of the program print candidate a or b won or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT