In: Computer Science
Code should be written in C
Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays.
1) Using initialization lists, create 5 one-dimensional arrays, one for each of these:
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
Executable code for the given problem is:
#include <stdio.h>
#include <stdlib.h>
void layout()
{
//printing first two lines of output
printf("candidates\t\tsubdivisions\t\ttotal");
printf("\n\t\tbrampton pickering zamier");
}
int total_votes_sub(int *a)
{
int i,t=0;
for(i=0;i<5;i++)
{
/*finding sum of all the votes in a
subdivision*/
t=t+*(a+i);
}
return t;
}
int total_votes_cd(int *a,int *b,int *c,int i)
{
int t=0;
/*finding all votes of single candidate from all
subdivisions*/
t=*(a+i)+*(b+i)+*(c+i);
return t;
}
int main()
{
/*declearing and initializing required
variables*/
int
total_votes_brampton,total_votes_pickering,total_votes_zamier,i;
int total_votes_count[100];
/*storing names of candidates in names array*/
char
*names[5]={"aubrey","blakes","chubbs","oliver","zamier"};
char *sub_names[100]={ "Brampton",
"Pickering","Markham"};
int brampton_count[5]={600,700,800,400,900};
int
pickering_count[5]={800,700,700,450,900};
int
zamier_count[5]={800,600,800,300,900};
layout();
/*finding total votes at each subdivision*/
total_votes_brampton=total_votes_sub(brampton_count);
total_votes_pickering=total_votes_sub(pickering_count);
total_votes_zamier=total_votes_sub(zamier_count);
for(i=0;i<5;i++)
{
/*finding all the votes of every
candidate and storing in total_votes_count array*/
total_votes_count[i]
=total_votes_cd(brampton_count,pickering_count,zamier_count,i);
}
/*printing according to the requirement*/
for(i=0;i<5;i++)
{
printf("\n%s\t\t%d \t
%d\t
%d\t\t%d",names[i],brampton_count[i],pickering_count[i],zamier_count[i],total_votes_count[i]);
}
printf("\ntotals\t\t%d\t %d \t
%d",total_votes_brampton,total_votes_pickering,total_votes_zamier);
}
output of the given code is: