Thread: Arrays, Selection Sort, HELP PLEASE!

  1. #16
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @nonpuz Yes you're totally correct!! AND I JUST FIGURED OUT MY LARGEST FUNCTION SO IT WORKS!!!!! 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);
    }

  2. #17
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    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

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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
    Last edited by AndiPersti; 01-17-2013 at 12:14 AM. Reason: wrong array name (nums)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  2. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 0
    Last Post: 03-13-2005, 12:17 PM
  3. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  4. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 06:54 AM
  5. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM

Tags for this Thread