Arrays, Selection Sort, HELP PLEASE!
Hey!
So, I have this assignment that I need to complete, here it is:
"Project: Class Election
A class has up to 50 students. There are up to 10 candidates numbered from 0 to 9 (use these numbers instead of names for now). One class might have 43 students and 5candidates, another class might have 32 students and 6 candidates, etc. The data file contains numbers from 0 to 9, each one representing one vote for that candidate, as shown below. Assume that each candidate gets at least one vote."
The data file is named votes.txt and here are its contents:
Code:
0 3 3 2 3 0 4 2 4 4 2 0 0 0 4 2 3 3 3 3 0 2 0 0 1 1 1 2 3 4 4 0 3 4 0 0 3 3 4 4 4 4 0
So far I wrote a program that opens the data file, and outputs the number of students in the class, which is 43. My question is, how do I get the program to output the number of candidates? The number of candidates in this case is 5, because they are 0, 1, 2, 3, 4. so ya, five numbers(names), so 5 candidates. How do I get the program to recognize the numbers and count them up? Somehow I'm really confused and stuck on this :(
Please don't give away the answer, just give me little hints or tell me what direction should I be going in! Thank You for your help!
And here's the program I wrote:
Code:
/*Written by: Kalpana Chinnappan
Date: January 17, 2013
Homework 1
*/
#include <stdio.h>
void countStudents(int[]);
int main (void)
{
int nums[50]; //up to 50 element int array
FILE *fp1; //file pointer
int i;
//****************** code starts here ***************
for(i=0;i<10;i++) //initialize array with 0
nums[i]=0;
i=0; //clean up and initialize LCV
if ((fp1=fopen("votes.txt","r"))==NULL)
{
printf("votes.txt failed to open\n");
return 1;
}
else
while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF
{
printf("num[%d] is %d\n",i+1,nums[i]);
i++;
}
countStudents(nums);
return 0;
}
void countStudents(int nums[])
{
int total = 0;
int count;
for (count = 0; count <=50; count ++)
{
if (nums[count] > total)
{
total = nums[count];
}
}
printf("The total number of students is: %d\n", nums[count]);
}