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

C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
Overview: You will write a program that reads up to 100 numbers from a file. As...
Overview: You will write a program that reads up to 100 numbers from a file. As you read the numbers, insert them into an array in ascending order. Specifics: 1A. Write a function called insertIntoSortedArray . i. It should take three arguments - a. myArray[ ] : sorted array that should be able to hold at most 100 integers. b. numEntries : the number of elements inserted so far. c. newValue : the incoming value to be inserted into the...
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 C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a C/C++ program which reads in a list of process names and integer times from...
Write a C/C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read the list until end of transmission (^d). You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT