63 821
Assignments Done
99,4%
Successfully Done
In August 2018

Answer to Question #51302 in C++ for lucracia khoza

Question #51302
Write the deviation of the function insertBTree to recursively insert a record into a B-tree. Also write a program to test various operations on a B-tree.

Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidates name, votes received by that candidate, and the percentage of the total votes received by the cndidate. Your program should also output the winner of the ekection. A sample output is as follows:
Candidate. Votes Recieved. % of Total Votes
Johnson. 6000. 25.91
Miller. 7000. 20.72
Duffy. 8000. 31.09
Robinson. 3000. 12.95
Sam. 2000. 9.33
Total. 26000.
The winner of the election is Duffy
Expert's answer
Solution:
First part:
Insertationof binary tree can be implemented in next way:
void insertBTree(Node *ptr, Data data) {
    if (ptr == NULL)  {
       ptr = new Node;
       ptr->data = data;
    }
    else if (data < ptr->data)        
       insertBTree(ptr->left, data);
    else
       insertBTree(ptr->right, data);
}
Second part:
Code.
#include <iostream>
#include <cstdio>
#include <string>
 
#define MAX_NUM 10
 
using namespace std;
 
int main() {
    stringname[MAX_NUM];
    int numberOfVotes[MAX_NUM];
    int num;   // the number of candidates
    int total = 0; // total number of votes
    int winner = 0;
 
    // Input
    cout << "Enter number ofcandidates: ";
    cin >> num;
    cout << "Entercandidates:\n";
    for (int i = 0; i < num; i++) {
       cin >> name[i] >> numberOfVotes[i];
        // Compute total numberof votes
       total += numberOfVotes[i];
        // Determine winner
        if (numberOfVotes[i] > numberOfVotes[winner]) {
           winner = i;
        }
    }
 
    // Output
    printf("ss s\n", "Candidate", "VotesRecieved", "% of TotalVotes");
    for (int i = 0; i < num; i++) {
       printf("sd .2f\n", name[i].c_str(), numberOfVotes[i], (float) numberOfVotes[i] * 100 / total);
    }
    printf("Total %d\n", total);
    printf("The winner of theelection is %s",name[winner].c_str());
 
    return 0;
}
Sample run.
Enter number of candidates: 5
Enter candidates:
Johnson 6000
Miller 7000
Duffy 8000
Robinson 3000
Sam 2000
      Candidate    Votes Recieved    % of Total Votes
          Johnson              6000               23.08
              Miller              7000               26.92
               Duffy              8000               30.77
        Robinson              3000               11.54
                 Sam              2000                7.69
Total 26000
The winner of the election is Duffy


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
13.03.15, 16:48

Dear Lucracia Khoza, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Lucracia khoza
13.03.15, 11:33

Thank you alot

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions