In: Computer Science
In a C language program CODE BLOCKS !!!!!!!!
Program that stores the number of votes obtained by 3
amounts in five different zones. Is required:
+ Request the total of votes by zone of each candidate, the values
must be entered on the keyboard and validate that it does not
accept negative numbers
+ Menu that requests the operation to be performed, in case of
entering an invalid data send an error message and request the
operation again
+ Option to perform another calculation
Note: The code must perform the calculations throughout the array, operations are not accepted assuming different values to the elements of the array
What will be told in the program:
* The area that obtained the most votes
* Total votes for each candidate
* The candidate with the fewest votes
* The number of votes obtained by zones
*Option menu
* Validate Negative data
* Request if you want to make another calculation
code:
#include<stdio.h>
#include<conio.h>
// Declaring a function which takes input
void read_input(int a[],int x);
// Declaration of arrays which stores votes
int p1[5],p2[5],p3[5],total[5];
void main()
{
//Declaration of necessary variables
int
i,choose,cal,p1_total=0,p2_total=0,p3_total=0;
// calling read_input function which reads
input from the user
read_input(p1,1);
read_input(p2,2);
read_input(p3,3);
// Calculating total votes in each Zone
for(i=0;i<5;i++)
{
total[i] = p1[i]+p2[i]+p3[i];
}
// Calculating each person Total votes
for(i=0;i<5;i++)
{
p1_total = p1_total+p1[i];
p2_total = p2_total+p2[i];
p3_total = p3_total+p3[i];
}
// Taking do while loop to iterate the menu until user
exits
do
{
printf("\n*********MENU********\n");
printf("1.The area that obtained
the most votes\n");
printf("2.Total votes for each
candidate\n");
printf("3.The candidate with the
fewest votes\n");
printf("4.The number of votes
obtained by zones\n");
scanf("%d",&choose);
// Calculating which area has most
votes
if(choose==1)
{
int max =
total[0],area;
for(i=1;i<5;i++)
{
if(total[i]>max)
{
max = total[i];
area = i;
}
}
printf("Area %d
has obtained most votes with %d votes\n",area+1,max);
}
// Displaying each person
votes
else if(choose==2)
{
printf("Person1
total votes are: %d\n",p1_total);
printf("Person2
total votes are: %d\n",p2_total);
printf("Person3
total votes are: %d\n",p3_total);
}
// Finding the person who got
fewest votes
else if(choose==3)
{
int
per,min;
if ((p1_total
< p2_total) && (p1_total < p3_total))
{
min = p1_total;
per = 1;
}
else
if((p2_total < p1_total) && (p2_total <
p3_total))
{
min = p2_total;
per = 2;
}
else
{
min = p3_total;
per = 3;
}
printf("Person
%d has got fewest votes with %d votes\n",per,min);
}
// Displaying number of votes in
each zone
else if(choose==4)
{
for(i=0;i<5;i++)
{
printf("Number of votes in Zone %d are:
%d\n",i+1,total[i]);
}
}
printf("\nDo you want to perform
another calculation?\n1.Yes\t2.No: ");
scanf("%d",&cal);
}while(cal==1);
printf("GoodBye!!!\n");
}
// Function definition which reads input
void read_input(int a[],int x)
{
int n=0;
do
{
while(n<5)
{
printf("Enter
person %d votes in zone %d: ",x,n+1);
scanf("%d",&a[n]);
//Negative
number validation
if(a[n]<0)
{
printf("Please enter a positive
number\n");
break;
}
else
{
n++;
}
}
}while(n<5);
printf("\n");
}
OUTPUT