# Arrays, Selection Sort, HELP PLEASE!

This is a discussion on Arrays, Selection Sort, HELP PLEASE! within the C Programming forums, part of the General Programming Boards category; Hey! So, I have this assignment that I need to complete, here it is: " Project: Class Election A class ...

1. ## 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
{
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]);

}```

2. You might be over-thinking this.

In this example, there's five candidates. If every candidate gets at least one vote, what's the highest value you expect to see in the data file (remembering that we started counting at zero)?

3. The highest value would be 4, right?

4. Correct. So if you're looking through a list of numbers, how would you "remember" the largest value you found?

5. Ohhh do you basically have to find the largest element in the array? In this case it would be 4, and the number of candidates will always be one more than that...right?

6. You got it! Good work!

7. So did you not read this ?

EDIT:
And as Salem pointed out in the aforementioned thread, this?

8. Yeah, that was my older post on the same homework! Why?....
Oh, and a last question for Matticus: should I create another function that calculates the largest element in the array? or should I put it into the main function?

9. @nonpuz
Oh...I'm sorry, I forgot to delete it -___________- Sorry, I'm new, and I didn't have time to read all the rules! D:

10. So did you not read this ?

EDIT:
And as Salem pointed out in the aforementioned thread, this?
I don't know how I missed that thread - I wasn't aware that the OP had cross-posted before.

However, for now at least, I don't see this question cross-posted (meaning that a lesson might have been learned).

Furthermore, the previous post of the OP you linked was a different problem. Questions were asked and answered, but I didn't see this question addressed (thought I might have missed it during a brief scan). Therefore, I feel that this thread is valid because it is a different question (that happens to be built upon previously aquired advice).

And, most importantly to me, the OP received vague advice and seemed to figure it out with just that. That is very rewarding, to have been able to assist with something like that.

should I create another function that calculates the largest element in the array? or should I put it into the main function?
You probably shouldn't need another function for this. All it would take is a quick variable check/update in a strategic spot in the loop.

11. @Matticus
Ok got it, thanks!!
And about the cross-posting, I'm really sorry, of course I won't do it again. I'm generally new to the world of forums, I never used one before, so I guess I wasn't familiar with the posting rules :/ Now that I know, It wont happen again, hope you understand. Thanks again Matticus for your help!! I appreciate it ^_^

12. GUYS I"M ACTUALLY BACK. I CANT FIGURE THIS OUT I"M STUCK!!

so I made a separate function (because I couldnt figure out how to include it in the main -__-) that's supposed to output the largest element of the array...somehow when i run the program, that function is completely ignored...why?! :O
here's my program:

Code:
```/*Written by: Kalpana Chinnappan
Date: January 17, 2013
Homework 1
*/
#include <stdio.h>

void countStudents(int[]);
void getLargest(int[]);

int main (void)
{
int nums[50];   //up to 50 element int array
FILE *fp1;      //file pointer
int i;
int size, big;

//******************  code starts here ***************
for(i=0;i<10;i++)   //initialize array with 0
nums[i]=0;
i=0;        //clean up and initialize LCV
{
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);
getLargest(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]);

}

void getLargest(int nums[])
{
int i, big, size;

for (i=0; i<size; i++)
scanf("%d", &nums[i]);
big=nums[0];
for (i=1; i<size; i++)
{
if (big < nums[i])
big=nums[i];
}
printf("\nBiggest: %d", big);
}```

13. Better, consistent indentation and formatting would be very helpful for you (and for those here).

Do you receive any compiler warnings when you build? If not, which compiler are you using, and are you using an IDE?

FYI - The problem lies in your "getLargest()" function.

14. Sorry about the formatting, I'm just trying to hurry up and finish this ahhaha, its due tomorrow -____- I'm such a procrastinator! I will indent and format it when I figure this out! And I don't receive any warnings, and I'm using CodeBlocks to compile and run it...and I just updated my code a little, now it looks like this, but now it doesn't run cause I have errors in my code...

Code:
```/*Written by: Kalpana Chinnappan
Date: January 17, 2013
Homework 1
*/
#include <stdio.h>

void countStudents(int nums[]);
void getLargest(int nums[]);

int main (void)
{
int nums[50];   //up to 50 element int array
FILE *fp1;      //file pointer
int i;
int big;

//******************  code starts here ***************
for(i=0;i<10;i++)   //initialize array with 0
nums[i]=0;
i=0;        //clean up and initialize LCV
{
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);
big=getLargest(nums, 50);
printf("The largest is %d\n", max);

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]);

}

void getLargest(int nums[])
{
int i, size;
max=-32000;
for (i=0; i<size; i++)

{

if (nums[i]>big)
{
big=nums[i];
}
}
return(big);
}```

15. I've already told you this before but I will try again...

Let's look at your original input file:
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`
What you are trying to do is tally the amount of votes PER candidate, is that correct? (I am assuming this)
So as stated above, each number in the input file is ONE vote, for the candidate specified by the number, ie if you read a '4' that
is one vote for Candidate number 4. That is where the number of students comes in, there can be a max of 50 numbers (each number is 1 student vote)
in the input file, each one being 1 vote for some candidate.

So if I read in 0 3 3 4 5 5. Then I have 1 vote for Candidate 0, 2 votes for Candidate 3, 1 vote for Candidate 4 and 2 Votes for Candidate 5. In total I have 6 votes so there were 6 students in the class. It also tells you that the maximum amount of Candidates are 10, this means you can use an array of 10 items to represent those candidates and simply increase candidates[read_in_number] by 1 for each number. Then you can simply do a linear search of your array for who has the most votes (or also keep track of this when tallying votes which is easy with another variable to store the current maximum votes in)

Page 1 of 2 12 Last