0

I've created a solution to week 3's tideman problem which is entirely logically correct. Unfortunately, I had to dodge the standard solution by commenting out parts of a function and altering its parameters, so my solution cannot be accepted by check50.

The problem i'm facing is using arrays in functions.

C implicitly converts arrays when they are used as parameters in functions, so how am I supposed to code with a function such as "int record_preferences(int ranks[])".

What does ranks[] even mean? An array? An element of the array?

I altered the parameters once to "int record_preferences(int k, int ranks[k])" and called it with "record_preferences(j, ranks[j])" so I could actually use an element of the array, but that produced errors.

Before anyone mentions pointers - they are only taught in week 4 - I believe this problem should be solvable without pointers .

// Query for votes for (int i = 0; i < voter_count; i++) { // ranks[i] is voter's ith preference int ranks[candidate_count];

    // Query for each rank
    for (int j = 0; j < candidate_count; j++)
    {
        string name = get_string("Rank %i: ", j + 1);

        if (!vote(j, name, ranks[j]))
        {
            printf("Invalid vote.\n");
            return 3;
        }
        else
        {
        ranks[j] = save;

        //Record_preferences should start here.
        for(int k = 0; k < candidate_count; k++)
        {
        int c = j;

        if(ranks[j] == k)
        {

            continue;
        }
        if(j > 0)
        {
            while(c > 0)
                {

                    if(k == ranks[c-1])
                    {
                        --c;

                        goto next;
                    }

                    --c;

                }
            ++preferences[ranks[j]][k];

            next:
            continue;
        }

        else
        {
            ++preferences[ranks[j]][k];

        }
        }
           //I commented out the record_preferences() function. 
           //record_preferences();
        }
    }

    printf("\n");
}

add_pairs();
sort_pairs();
lock_pairs();
print_winner();
return 0;

}

1 Answer 1

1

ranks[] means an array of values that has been named ranks; from the declaration, we can tell that the values will all be integers. Any time you see a name followed by square brackets in C-based languages (C, C++, Java, etc.), you know that you are dealing with an array. If you have an integer or integer variable (such as i) in the square brackets, then you are dealing with an element of the array. But, the record_preferences function deals with the full array and not with only one of its elements.

As for how you are supposed to code it, the assignment's specification says to make this function "update the global preferences array to add the current voter's preferences." This is using the rankings that were captured in the vote function--the same ranks[] array is being passed to both functions.

Hope that helps.

7
  • Thanks for the lengthy comment, but it doesn't adresss the main issue: this would imply that I have knowledge of pointers at week 3 - I do not. Commented Feb 25, 2021 at 11:12
  • No, it doesn't. I never once mentioned pointers; nothing about what we are discussing here is talking about pointers. You will use an index to get values from the ranks array in the record_preferences function just as was needed to add the values in the vote function. Your questions where "what does ranks[] mean?" and "how am I supposed to code record_preferences(ranks[])?", which my answer has answered. :) This is doable without the use of pointers and you have all of the knowledge you need. Don't overthink it and treat it like any other array. Commented Feb 25, 2021 at 11:25
  • But just examine the syntax for a second: to me, logically, record_preferences(ranks[]) means an entire array is being passed into a function - and for that to work I'd need to understand pointers, while for your indexing solution to work, the parameter would have to be record_preferences(int i , int ranks[i]), but I've tried that and it failed to compile. Commented Feb 25, 2021 at 17:33
  • It does pass an entire array, but you don't need to understand pointers. You just need to understand arrays. You also don't need to pass an index to the function, as this should be taken care of inside of the function itself--you should not be changing the function declarations. If you want to edit your question to add your code for your record_preferences function, I can try to help you more, but I assure you that you can complete this without any knowledge of pointers. Your code inside the function will index into the array just as if you were writing code in the main function. Commented Feb 25, 2021 at 18:06
  • I've edited it - all the code was what was supposed to be written inside record_preferences, but I couldn't get around the pointer/array issue so I just plucked it all straight into main. Commented Feb 26, 2021 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .