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...
Create a C++ program to simulate an art gallery: 4 classes: Gallery class Has three vectors...
Create a C++ program to simulate an art gallery: 4 classes: Gallery class Has three vectors of type Painting which represent three categories of paintings (abstract, impressionism, pointillism) Has a function that reads in paintings from a file and stores each painting in the correct vector Read in paintings from file (will have multiple paintings) Title of painting Artists first name Artists last name Address street number Address street name Address city Address state Address zip Artists website Category Number...
Using Visual Studio in C#; create a grading application for a class of ten students. The...
Using Visual Studio in C#; create a grading application for a class of ten students. The application should request the names of the students in the class. Students take three exams worth 100 points each in the class. The application should receive the grades for each student and calculate the student’s average exam grade. According to the average, the application should display the student’s name and the letter grade for the class using the grading scheme below. Grading Scheme: •...
Using C++ (microsoft visual studios 2013) create a program that uses three parallel numberic arrays of...
Using C++ (microsoft visual studios 2013) create a program that uses three parallel numberic arrays of size 6. The program searches one of the arrays and then displays the corresponding values from the other two arrays. The program should prompt the user to enter ProductID. Valid ProductID's should be the numbers 24, 37, 42, 51, 66 and 79. The program should search the array for the product ID in the ID's array and display the corresponding price and quantity from...
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...
C++ Create a class called Musicians to contain three functions string ( ), wind ( )...
C++ Create a class called Musicians to contain three functions string ( ), wind ( ) and perc ( ). Each of these functions should initialize a string array to contain the following instruments:     - veena, guitar, sitar, sarod and mandolin under string ( )     - flute, clarinet saxophone, nadhaswaram and piccolo under wind ( )     - tabla, mridangam, bangos, drums and tambour under perc ( ) It should also display the contents of the arrays that...
(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...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT