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..
:)

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;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

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.

This article has been dead for over six months. Start a new discussion instead.