Like Tree3Likes

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

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

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    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. #3
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    The highest value would be 4, right?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    Correct. So if you're looking through a list of numbers, how would you "remember" the largest value you found?

  5. #5
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    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?
    Matticus likes this.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    You got it! Good work!

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    So did you not read this ?

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

  8. #8
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    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. #9
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @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. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    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.
    nonpuz likes this.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @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. #12
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    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
        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 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);
    }
    Last edited by kal123456; 01-16-2013 at 09:30 PM.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    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.
    laserlight likes this.

  14. #14
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    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
        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);
        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. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    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 LastLast
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, 11:17 AM
  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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21