In: Computer Science
Code should be Written in C
Use the data from the example below.
* Your C program should take the data in the arrays and produce the output below, neatly formatted as shown:
Candidates Subdivisions Totals
Brampton Pickering Markham
Aubrey 600 800 800 2200
Blake 700 700 600 2000
Chubbs 800 700 800 etc
Oliver 400 450 300
Zamier 900 900 900
Totals 3400 etc etc
IDE Used: Dev C++
Code: election.c (can be given any name with .c extension)
#include <stdio.h>
#include <stdlib.h>
//this function is to calculate total number of
votes in each subdivision
//input parameter will be the name of array storing votes of
subdivision
int total_sub_vote(int votes[])
{
int i;
int total=0;
for(i=0; i<5;i++)
{
total=total+votes[i];
}
//It will return the total votes of that
subdivision
return total;
}
//This function is to calculate total votes of each
candidate
//Input will be votes of that candidate in each
subdivision
int tot_can_vote(int subdiv1, int subdiv2, int subdiv3)
{
return subdiv1+subdiv2+subdiv3;
}
//function to display the first two lines of the
header in output
void heading(char **subdiv_array)
{
char column1[15] = "Candidate";
char column2[15] = "Subdivision";
char column3[15] = "Totals";
printf("%s %17s %20s\n", column1,column2,
column3);
printf ("\t %10s %10s %8s\n", subdiv_array[0],
subdiv_array[1], subdiv_array[2]);
}
//main function
int main()
{
int i;
//to store total votes of
candidates
int tot_vote_cand;
//array to store candidate
names
char *candidates[5] = {"Aubrey", "Blake", "Chubbs",
"Oliver", "Zamier"};
//array to store subdivision
name
char *subdivision[3] = {"Brampton", "Pickering",
"Markham"};
//array to store votes in
Brampton
int vote_Brampton[5] = {600,700,800,400,900};
//array to store votes in
Pickering
int vote_Pickering[5] = {800,700,700,450,900};
//array to store votes in
Markham
int vote_Markham[5] = {800, 600, 800, 300, 900};
//calling the function to print
heading
heading(subdivision);
for(i=0;i<5;i++)
{
//calling the function to calculate total
votes of candidates
//it will be called three times
tot_vote_cand =
tot_can_vote(vote_Brampton[i],vote_Pickering[i],vote_Markham[i]);
//printing the
result
printf("%7s %10d %10d %8d
%8d\n",candidates[i],vote_Brampton[i],vote_Pickering[i],vote_Markham[i],
tot_vote_cand);
}
//calling the fucntion to calculate the
total vote of each subdivision
//it is called three times
printf("%7s %10d %10d
%8d\n","Total",total_sub_vote(vote_Brampton),total_sub_vote(vote_Pickering),total_sub_vote(vote_Markham));
return 0;
}
Code Snippets
Output