hi ..pleas any one can help me to write aprogram that allows the user to enter the last names of 5 candidates in a local election and the number of votes received by each candidate, the program should then out put each candidates name the number of vote percentage of the total votes received by the candidates received.
It should too output the winner of the election…
Write the program by using dynamic array…??
i dont understand the dynamic array very well and i wrote a code but it was wrong any one can help..
:)

Dani AI

Generated

Good progress from ; the original program already reads names and vote counts but stops short of printing meaningful percentages and a clear winner. pointed toward dynamic allocation and flagged common pitfalls. A cleaner, safer path is to use standard containers and algorithms so memory and edge cases are handled automatically.

Recommended approach (high level): read the candidate count, then read candidate names (use getline so last names with spaces work). Read and validate integer vote counts. Compute the total with std::accumulate and guard against total==0 to avoid division-by-zero. Compute each percentage with floating-point arithmetic (not integer division), format with fixed precision, and determine the winner with std::max_element — collecting ties if more than one candidate has the top vote total. Avoid global variables; keep data local or pass it to functions.

Example implementation (C++11+). This uses std::vector for dynamic storage, std::accumulate to get the total, and std::max_element to find the winner:

#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <iomanip>

// see code block in the post above for a complete example

Notes and cautions: validate input (nonnegative integers), consume the newline after reading the candidate count before using getline, and handle the "no votes cast" case explicitly. Prefer std::vector over manual new/delete to avoid leaks and simplify lifetime management. For reference, see std::vector, std::accumulate, and std::max_element.

Recommended Answers

All 5 Replies

Show the code You wrote.

use the "new" keyword and have a pointer point to this new data. That's what dynamic allocation is and make the new data an array which can have any size. So may use a variable.

this is my code its work to enter the names and votes numbers but this code don't give the percentage of the votes to every candidate..

#include<iostream>
#include<string>;


using namespace std;
double pv;
int no;
int sum=0;


void getperc(int vno[],int &sum,double pv)
{
    for(int i=0;i<no;i++)
    {

        pv=vno[i]/sum;
        cout<<pv;
    }
};

void getcname(string cname[],int no)
{
    for(int i=0;i<no;i++)
    {
        cin>>cname[i];
    cout<<cname[i]<<endl;
    }
};


void getvno(int vno[],int no)
{

    for(int i=0;i<no;i++)
    {
    cin>>vno[i];
    sum=sum+vno[i];
    cout<<sum<<endl;

    }

}




int main()
{
string *cname;
double *perc;


cin>>no;

cname=new string[no];

int *vno;

vno=new int[no];
perc=new double[no];

getcname(cname,no);
getvno(vno,no);
getperc(vno,sum,pv);



return 0;
}

1. Use Code Tags.
2. pv, sum, no - these are global variables, You don't need to pass them to functions.
3. Line pv=vno[i]/sum; - since both variables are integer, the result is integer as well. Thus You always get zero. Cast vno[i] to double.
4. You have memory leak. Use delete for pointers.

thanks alot...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.