Question

In: Computer Science

In C++, there are 4 students that are running for class president. Create three arrays: the...

  1. In C++, there are 4 students that are running for class president. Create three arrays: the candidate’s name, the number of votes the candidate receives, and the candidate’s percentage of the total votes. Your program should be broken down into at least 3 functions: find the total number of votes, calculate values for the percentage each candidate received, and sort the number of votes from highest to lowest. Write the candidate’s name, the total number of votes they received, and the percentage of the total using the following: Joe 20 Bill 30 Sue 40 Mary 35

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question .Be safe.

Note : check attached image for output ,code compiled and c++14 code blocks ide .

Program Plan:

  1. delcare all array with predefine name and vote .
  2. declare percentage empty array of size 4
  3. call calculatePercentage function will calculate % and assign to array
  4. sort Function sorts the votes using selection sort .
  5. selection sort first find minimum votes in array and replace with outer loop index
  6. also swap % value and name too .
  7. print all detail on console using function

Program:

#include<iostream>
#define totalCandidate 4
using namespace std;

int getTotalVotes(int voteArray[],int length);
void calculatePercentage(int voteArray[],int length,double percentageArray[]);
void sortCandidateVotes(int voteArray[],int length,double percentageArray[],string candidateNameArray[]);
void printVoteDetail(int voteArray[],int length,double percentageArray[],string candidateNameArray[]);
int main()
{
    string candidateName[]={"Joe","Bill","Sue","Marry"}; //Array of string candidate name
    int candidateVotes[]= {20,30,40,35}; //Array of integer candidate vote got
    double votingPercentage[totalCandidate]; //percentage array for calculation
    calculatePercentage(candidateVotes,totalCandidate,votingPercentage);//calculate percentage
    sortCandidateVotes(candidateVotes,totalCandidate,votingPercentage,candidateName);//sort votes in ascending order using selection sort
    printVoteDetail(candidateVotes,totalCandidate,votingPercentage,candidateName); //print detail


}

//this method get vote array as argument and its size
//return integer total votes
int getTotalVotes(int voteArray[],int length)
{
    int sum=0;
    for(int i=0;i<length;i++) //loop from 0 to size
    {
        sum+=voteArray[i]; //store in sum
    }
    return sum;//return sum
}

//this function takes vote array and percentage array and its size
//return void
//calculate the percentage of vote each candidate
void calculatePercentage(int voteArray[],int length,double percentageArray[])
{
    int totalVotes=getTotalVotes(voteArray,length); //get total votes
    for(int i=0;i<length;i++) //run a loop from 0 to size
    {
        //vote of candidate*100/total votes
        percentageArray[i]=(double)voteArray[i]*100.0/totalVotes; //calculate percentage
    }
}
//this function takes name ,vote and percentage array its length
//return void
//sort the vote array by selection sort and its name and percentage too
void sortCandidateVotes(int voteArray[],int length,double percentageArray[],string candidateNameArray[])
{
    //run a loop from 0 to size -1 (4-1)
    for(int i=0;i<length-1;i++)
    {
        int index=i; //store index in i
        for(int j=i+1;j<length;j++) //loop from next element of i index to length
        {
            if(voteArray[j]<voteArray[index]) //check index element is greater than current array element
            {
                index=j; //store minimum vote index in index variable
            }
        }
        //swap votes ,percentage and name
        int tempVotes=voteArray[i]; //store i index votes in temp
        double percentage=percentageArray[i]; //store in perc. in temp
        string name=candidateNameArray[i]; //store name
        voteArray[i]=voteArray[index]; //swap value
        voteArray[index]=tempVotes;

        percentageArray[i]=percentageArray[index];
        percentageArray[index]=percentage;

        candidateNameArray[i]=candidateNameArray[index];
        candidateNameArray[index]=name;
    }

}
//takes all array and return void print detail on console
void printVoteDetail(int voteArray[],int length,double percentageArray[],string candidateNameArray[])
{
    cout<<"****************Voting Result******************"<<endl;
    cout<<"Name\tvotes\tvote%"<<endl; //print header by tab
        for(int i=0;i<length;i++)
        {
            //print name tab votes and percentage separated by tab
            cout<<candidateNameArray[i]<<'\t'<<voteArray[i]<<'\t'<<percentageArray[i]<<"%"<<endl;
        }
}

Output :

Please up vote ,comment if any changes.


Related Solutions

Question 4. Your friend Joanna is running for class president and has the support of 55%...
Question 4. Your friend Joanna is running for class president and has the support of 55% of the total student body. You poll a group of N students (N=50 or N=150) and compute the total number of students who state that they will vote for Joanna. For samples of size N=50 or N=150, use R to simulate 10,000 independent samples and record the total votes for Joanna in each sample. For each sample, compute a Z-score test statistic for the...
Question 4. Your friend Joanna is running for class president and has the support of 55%...
Question 4. Your friend Joanna is running for class president and has the support of 55% of the total student body. You poll a group of N students (N=50 or N=150) and compute the total number of students who state that they will vote for Joanna. For samples of size N=50 or N=150, use R to simulate 10,000 independent samples and record the total votes for Joanna in each sample. For each sample, compute a Z-score test statistic for the...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement: 1)Object get(int), which takes an int index and returns the Object at that index 2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference Both your...
When you create a C++ class, you are given 4 default methods. What are they and...
When you create a C++ class, you are given 4 default methods. What are they and why is it important that you remember that these exist?
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right?...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right? There are 5 objectives and 20 points each. Please indicate in code (or comments) where each objective begins and ends. It may be prudent to place each objective in it's own function. In this lab you will be reading in a file of student results. The students each ran 3 laps in a race and their times to complete each lap are posted in...
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values...
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array. Declare a multi-dimensional array of integers, which is 10 rows of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT