Need Help Please!

I have an assignment and i am stuck at part 7, please if anyone can help me! Thanks…
In an election, a candidate is identified by the following information:
ID Number, name, Age, Number of voices obtained.

  1. Write the proposed data structure,
  2. Write a function to create a file CAND.DTA and to fill it, before the elections, a
    negative ID number indicates the end of data,
  3. Write a function to add candidates to the file,
  4. Write a function to find if given candidate (ID number) exists in the file,
  5. Write a function to delete a candidate from the file,
  6. Write a function to read the number of voices obtained for each candidate after the
  7. Write a function that uses only one file other than Cand.dta to sort Cand.dta in
    decreasing order of number of voices.

What are you stuck on? It’s best if you describe what you already have and where you have trouble, so people can suggest improvements. :slightly_smiling_face:

i am stuck at part 7, if you can help me please!

Yes, but what specifically? Do you have trouble understanding the task? Doing a specific part of it? How so? To get help you’ll have to provide details of what you need help with.

To be blunt: I’m happy to help with programming questions, but I’m not going to do your homework. And I doubt anyone else here feels like doing that.

1 Like

okay, so basically i need an algorithm to sort the file.

After step 6 you should already have a list (or array) of the candidates. So what you really need to do is sort that, and then write it to the output file.

Many programming languages already have sorting functions, where you’d just need to define how things should be sorted, e.g. sorted() in Python. And if you write your own sorting mechanism the key question is the same: How do you decide which element goes first?

So, I have to place the information in an array then sort this array then copy the the array into the file, right ?
by the way i am using C language.

That’s one way to do it, yes. Another would be to find the first element (in this case the candidate with the most votes), write it to the output file, remove from the array, repeat. The first option is probably more efficient.

For sorting the array in C you might want to check out qsort(), unless you’re supposed to implement sorting by yourself. If you’re going to implement sorting yourself consider storing pointers (instead of the actual structs) in the array. That’s an additional layer of reference, but swapping pointers is easier than swapping structs. :slightly_smiling_face:

just one more question, if i want to sort it using an array, how much the array should be?(the question didn’t specify the number)

Do you mean the size of the array? That’ll depend on the input data, as many candidates as there are.

yes i mean the size of the array. In this case there is no information about it. what can I do ?

According to the rest of the assignment you’re supposed to read the candidate data from a file. Reading that file will give you the right number. Based on that, a few options:

  • Read the file once to get the number, allocate memory for an array of the right size (calloc()), then read the file again into the array. Simple, but with the disadvantage that you have to read the file twice.

  • Allocate memory for a reasonable number of candidates (again calloc(), or malloc()), read into that and keep count. If there isn’t enough memory, use realloc() to extend the memory area before continuing. This can handle any number of candidates with just one read of the input.

  • Finally, you can use a large, fixed size and hope it’ll be enough. You still need to keep count when reading data and stop with an error message if the array doesn’t have enough space. Simple, but limits the number of candidates you can handle.

Thank you very much!!
Have a great day!

1 Like