Thursday, July 30, 2009

Dynamic Array Question in C++?

Write a program using dynamic arrays that asks the user for the number of candidates in a local election, and then creates the appropriate ar rays to hold the data. The program then asks the user to enter the last name of each candidate and the number of votes received by her/him. The program then outputs each candidate’s last name, the number of votes received and the percentage of the total votes received by the candidate. Finally, it also outputs the winner of the election.

Dynamic Array Question in C++?
class CCandidate


{


private:


char * m_szLastName;


int m_iNumVotes;


double m_dPercentVotes;





public:


CCandidate()


{


m_szLastName = NULL;


m_iNumVotes = 0;


m_dPercentVotes = 0;


};





CCandidate( char * szName, int iVotes )


{


setValues( szName, iVotes );


};





void setValues( char * szName, int iVotes )


{


m_szLastName = new char[ strlen( szName ) + 1 ];


strcpy( m_szLastName, szName );


m_iNumVotes = iVotes;


m_dPercentVotes= 0;


};





void setPercent( int iTotalVotes )


{


m_dPercentVotes = ( double ) ( m_iNumVotes * 100 ) / ( double ) iTotalVotes;


}





void printDetails()


{


printf( "Candidate %s: Total Votes Received is %d, percent votes received is %.02f\n\n", m_szLastName, m_iNumVotes, m_dPercentVotes );


}





void printWinnerMessage()


{


printf( "Candidate %s, with %.02f percent votes, wins!\n\n", m_szLastName, m_dPercentVotes );


}





~CCandidate()


{


if( m_szLastName != NULL )


{


delete [] m_szLastName;


m_szLastName = NULL;


}


};


};





int _tmain(int argc, _TCHAR* argv[])


{


char szTemp[ 256 ];


int iNumCandidates = 0, iVote = 0, iTotalVotes = 0, iWinnerIndex = 0, iMaxVotes = 0;


printf( "Please enter the number of candidates: " );


scanf( "%d", %26amp;iNumCandidates );


printf( "\n" );





CCandidate * arrayCandidates = new CCandidate[ iNumCandidates ];


for( int iCnt = 0; iCnt %26lt; iNumCandidates; iCnt++ )


{


printf( "For Candidate Number %d, type last Name:\n", iCnt + 1 );


scanf( "%s", szTemp );


printf( "How many votes did %s get?\n", szTemp );


scanf( "%d", %26amp;iVote );


iTotalVotes += iVote;


if( iVote %26gt; iMaxVotes )


{


iMaxVotes = iVote;


iWinnerIndex = iCnt;


}





arrayCandidates[ iCnt ].setValues( szTemp, iVote );


}





for( int iCnt = 0; iCnt %26lt; iNumCandidates; iCnt++ )


arrayCandidates[ iCnt ].setPercent( iTotalVotes );





printf( "\n\nNow for the results:\n\n" );


for( int iCnt = 0; iCnt %26lt; iNumCandidates; iCnt++ )


arrayCandidates[ iCnt ].printDetails();





printf( "And the winner is...\n" );


arrayCandidates[ iWinnerIndex ].printWinnerMessage();





delete [] arrayCandidates;


arrayCandidates = NULL;





printf( "\nPress a few enters to quit!" );


getc( stdin );


getc( stdin );


return 0;


}





======================================...


DISCLAIMERS:


(a) Code does not handle the case where user enters multiple winners, i.e., more than one candidate has the highest number of votes - do it yourself.





(b) Code does not handle the case where multiple candidates have same last names. Right now, they are undistinguishable. To resolve this, you have to add a "number" feature being printed - do it yourself.





(c) Code will need some #include headers to compile. stdio.h, stdlib.h, iostream.h etc basic ones I can think of. The getc() at the end I think needs ctype.h. Resolve such errors youself.
Reply:If you pay me 50 dollars.


No comments:

Post a Comment