Question: A survey of 10 pop artists is made. Each person votes for an artist by specifying the number of the artist(a value from 1 to 10). Each voter is allowed one vote for the artist of his/her choice. The vote is recorded as a number from 1 to 10. The number of voters is unknown beforehand but the votes are terminated by a vote of 0. any vote which is not a number from 1 to 10 is a spoilt vote. A file, vote.txt contains the names of the candidates. The first name is considered as candidate 1, the second as candidate 2, etc. etc. The names are folled by the votes. Write a program using structures to read the data and evaluate the results of the survey. Print the results in alphabetical by artist name and in order by votes received(least votes first). Output to a file.
CODE
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int voting(num); //function prototype
main ()
{
clrscr();
char name[30];
int num_vote;
typedef struct vote {
name;
num_vote;
}ballot;
//declarations
int most_ votes=0;
int spoilt=0;
int valid=0;
FILE * in=fopen("votes.txt","r"); //read from file
FILE * out=fopen("results.txt","w"); //write to file
printf("\nPlease enter the vote of your choice");
scanf("%d", &ballot);
while(ballot !=0) { //terminates when 0 is entered
if(ballot < 1 || ballot > 10) { //if votes is in this range
++spoilt; } //count the number of spoilt votes
if(ballot >= 1 && ballot <= 10) {//if votes is between this range
++valid; } //count the number of valid votes
//Calculates the most votes
if(ballot > most_votes) {
most_votes = ballot; }
scanf("%d", &ballot); }
//bubble sort
//count the number of candidates
for(int d=0; d<10; d++)
{
fprintf(out, "Artist Name is: %s",name);
fprintf(out, "Number of Votes received: %d",num_vote);
close(in);
fclose(out); }