# Arrays, Selection Sort, HELP PLEASE!

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 01-16-2013
kal123456
@nonpuz Yes you're totally correct!! AND I JUST FIGURED OUT MY LARGEST FUNCTION SO IT WORKS!!!!! :D :D Thanks soo much guys for thoroughly explaining this to me, and taking the time and being patient with me! I greatly appreciate it! Here is my fixed code that works!! :)

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;     //******************  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);     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 largest = 0;     int count;     for (count = 0; count < 10; count ++)     {         if (nums[count] > largest)         {             largest = nums[count];         }     }     printf("The largest value is: %d\n", largest); }```
• 01-16-2013
kal123456
GUYS.........ok i thought i was all done..........cant believe i have a problem again.............but i went back to my .txt file and changed it a little so the biggest value would be 5 instead of 4..........and i ran my program, and IT STILL GAVE ME 4 AS THE LARGEST VALUE even thought i added a 5 and it should have outputted 5 as the largest!! I can't find a single error in my code! I tried putting in 9, 8, or 7 into the .txt file....the largest value still remains 4........please help!!!!! :O
• 01-17-2013
AndiPersti
Code:

```int main (void)  {     int nums[50];  //up to 50 element int array ...   void countStudents(int nums[]) {     int total = 0;     int count;     for (count = 0; count <=50; count ++)```
This for-loop is wrong. "nums" has 50 elements thus valid indices are 0 to 49. But in your loop "count" will become 50. What value has nums[50]?

Code:

```{     if (nums[count] > total)     {         total = nums[count];     } }```
Your student counting method is wrong. nums[count] does store the number of the candidate. So your total will end up with the number of candidates not with the number of students.

Code:

```void getLargest(int nums[]) {     int largest = 0;     int count;     for (count = 0; count < 10; count ++)```
You only look at the first 10 students in your "nums"-array.

I'm pretty sure you are still confused about what values are stored in your "nums"-array and what the indices of your array represent.

Each number in your file is a vote for a candidate. The first number is the vote from the first student, the second number the vote from the second student and so on.
Thus "nums[count]" should better be called like "votes[student]". Good variable names makes it much easier to understand a problem.

Bye, Andreas
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12