Most of the brain of program

Assignment Help Basic Computer Science
Reference no: EM132318254

The main function is elect_candidate which has most of the brain of this program. Towards to bottom of it when i run it gives me errors for the lines where i compare ballots with -1 and set the values to 0

#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

const char *FILENAME = "elections.txt";
const int MAXBALLOTS = 500;
const int NUM_CANDIDATES = 3;
int elect_candidate(int ballots[MAXBALLOTS][NUM_CANDIDATES],
                    int numBallots) {

    int winner = -1;
    int round=0;
    int tally[NUM_CANDIDATES + 1] = { 0 };
    double percentages[NUM_CANDIDATES + 1];


    int neg = -1;
    while(winner==-1){//repeats this process until a winner is found
        cout<<"Round "<<round<<": "<<endl;
        for(int i = 0;i<numBallots;i++){
            int choice;
            for(int rank=0;rank<3;rank++){
                choice=ballots[i][rank];
                if(tally[choice]==-1){
                    tally[choice]+=2;
                }
                else
                {
                    tally[choice]++;
                    break;
                }
            
            }
            round++;
        }
   
    cout << "Percentages for each candidate: " << endl;
        for (int i = 1; i < NUM_CANDIDATES + 1; i++) {
            percentages[i] = (double)tally[i] / numBallots;
            cout << fixed << setprecision(2);
            cout << "#" << i << ": " << percentages[i];
            cout << endl;
        }


    
    for(int i=1;i<NUM_CANDIDATES+1 ;i++){
        if(percentages[i]>0.5)
            winner==i;
    }
   


    if(winner==-1){
        int lowestPercent=percentages[1];
        int lowestCandidate=1;
        for(int i=1;i<NUM_CANDIDATES+1 ;i++){
            if(percentages[i]<lowestPercent){
                lowestPercent=percentages[i];
                lowestCandidate=i;
            }
        }
        ballots[lowestCandidate]== -1;
        for(int i=1;i<NUM_CANDIDATES+1 ;i++){
            if(ballots[i]!==-1)
                ballots[i]==0;
        }
    }
}


        return -1;
        
}



int main()
{
    ifstream f(FILENAME);
    string tmp;
    int nLines;
    int numBallots = 0;


    int ballots[MAXBALLOTS][NUM_CANDIDATES];


    // While we are not at end-of-file
    while (getline(f, tmp)) {
        // Read the number of lines for this election
        nLines = atoi(tmp.c_str());
        // Read in each ballot
        for (int i = 0; i < nLines; i += 3) {
            // Read in a single ballot (3 lines each)
            cout << "Read ballot: ";
            for (int j = 0; j < NUM_CANDIDATES; j++) {
                getline(f, tmp);
                ballots[numBallots][j] = atoi(tmp.c_str());
                cout << " " << ballots[numBallots][j];
            }
            numBallots++;
            cout << endl;
        }
        cout << "Read " << numBallots << " ballots." << endl;


        int winner = -1;
        int round = 0;


        // Run the election
        winner = elect_candidate(ballots,
                                 numBallots);


        cout << "The winner is: " << winner << endl;


        // TEMPORARY -- Turn this on if you want to
        // just load 1 set of ballots.
        // return 0;


        // Reset ballot counter for next round
        // -- continue loading more elections.
        numBallots = 0;
    }
    system("pause");
    return 0;
}

 

Reference no: EM132318254

Questions Cloud

Describe the primary goal of the cobit framework : Describe the primary goal of the COBIT Framework. Describe the major objective of the Controls area.
Business impact analysis for organizations : Discuss the critical need for business impact analysis for organizations.
Superior choice of routing protocol : Here is the statement you will tackle for this post: In large enterprise network environments the superior choice of routing protocol would be RIPv2
Large enterprise network environments : Here is the statement you will tackle for this post: In large enterprise network environments the superior choice of routing protocol would be RIPv1 and not RIP
Most of the brain of program : The main function is elect_candidate which has most of the brain of this program. Towards to bottom of it when i run it gives me errors for the lines
Describe the basic windows os architecture : Distinguish Operating System (OS) component from architecture. Describe the basic Windows OS architecture.
Computer forensics-industrial espionage and cyberterrorism : What are some main differences between the methods used in a Forensics approach to discovering information vs Industrial Espionage?
C++ code runs sometimes : The below C++ code runs sometimes, but sometimes stops unexpectedly. Describe the problem and how you might change the code to resolve it.
How you as a network manager would respond to address : Please describe an example or two from each (people, technology, malware) and how you as a network manager would respond to address the risk

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd