I am using function befoure the main, One day I was told that if you use a function befour the main you do not need to declare it, is that right?
Here is my program:
Code:
#include<stdio.h>
int A[5]={192,147,186,114,267};
int B[5]={48,90,12,21,13};
int C[5]={206,312,121,408,382};
int D[5]={37,21,38,39,29};
int total[4]={0};
int percent_A, percent_B, percent_C, percent_D;
void display_table(void)
{
int i;
printf(" Candidate Candidate Candidate Candidate\n");
printf("Precinct A B C D\n") ;
printf("___________________________________________________\n");
for(i=0;i<5;++i)
printf("%3i\t %i %i %i %i\n",i+1,A[i],B[i],C[i],D[i]);
}
int get_num_of_votes(int X[5])
{
int total=0;
int i;
for(i=0;i<5;++i)
{
total+=X[i];
}
return total;
}
int get_percent_of_total_votes(int a, int b)
{
return((a*100)/b);
}
void get_winner(void)
{
if(percent_A>=50)
printf("\nCandidate A the winner!\n\n");
else if(percent_B>=50)
printf("\nCandidate B the winner!\n\n");
else if(percent_C>=50)
printf("\nCandidate C the winner!\n\n");
else if(percent_D>=50)
printf("\nCandidate D the winner!\n\n");
else
printf("Two condidates will run:\n\n");
get_two_candidates();
}
void get_two_candidates(void)
{
int n, m, temp;
for(n=0;n<3;++n)
for(m=n+1;m<4;++m)
if(total[n]<total[m])
{
temp=total[m];
total[m]=total[n];
total[n]=temp;
}
if(percent_A==total[0])
printf("A will run");
else if(percent_B==total[0])
printf("B will run");
else if(percent_C==total[0])
printf("C will run");
else if(percent_D==total[0])
printf("D will run");
else if(percent_A==total[1])
printf("A will run");
else if(percent_B==total[1])
printf("B will run");
else if(percent_C==total[1])
printf("C will run");
else
(percent_D==total[1]);
printf("D will run");
}
main();
{
int total_A, total_B, total_C, total_D;
int votes_total;
display_table();
total_A=get_num_of_votes(A);
total_B=get_num_of_votes(B);
total_C=get_num_of_votes(C);
total_D=get_num_of_votes(D);
votes_total=total_A+total_B+total_C+total_D;
percent_A=get_percent_of_total_votes(total_A, votes_total);
total[0]=percent_A;
percent_B=get_percent_of_total_votes(total_B, votes_total);
total[1]=percent_B;
percent_C=get_percent_of_total_votes(total_C, votes_total);
total[2]=percent_C;
percent_D=get_percent_of_total_votes(total_D, votes_total);
total[3]=percent_D;
printf("\n");
printf("Candidate: precent\n");
printf(" A%10i\n B%10i\n C%10i\n D%10i\n",percent_A, percent_B, percent_C, percent_D);
get_winner();
}