# Problem with Structs

This is a discussion on Problem with Structs within the C Programming forums, part of the General Programming Boards category; hi everyone. before i continue, i would like to mention that this is a homework assignment involving c, but im ...

1. ## Problem with Structs

hi everyone. before i continue, i would like to mention that this is a homework assignment involving c, but im naturally deficient in programming nor am i inducted into a profession involving programming (i am a networking manager). this is the last assignment i've missed, and this paper is due tomorrow. i hope people arent reluctant to help, since this does seem a bit unethical. all i ask is for help, that is all. any help will be greatly appreciated. thank you.

On the keyboard type sequence records containing data of a medical examination of schoolchildren: Full name, date of birth, height and weight.Print out entered data in a table sorting them by names of the students alphabetically.

this is what i have so far

Code:
```
struct data { char name[20]; int age[20]; int weight[20]; };  int main() { struct data Students[NO_OF_STUDENTS]; // 20 students. int Counter = 0;  for(; Counter < NO_OF_STUDENTS; ++Counter) { Students[Counter].name[19] = '\0'; // Set the end of the string. scanf("%19s", Students[Counter].name); // Extract 19 characters. Students[Counter].age[19] = '\0'; // Set the end of the string. scanf("%19d", Students[Counter].age); // Extract 19 characters. Students[Counter].weight[19] = '\0'; // Set the end of the string. scanf("%19d", Students[Counter].weight); // Extract 19 characters.  /i can put getchar or fflush, for clearing memory. }  return 0; }```
thank you to anyone in advance.

2. Perhaps you can edit it again, so it isn't all on one long line.

3. Why do you have 20 ages and 20 weights ?
they are ints not strings ( char * or char[] ).

4. [C] C - Pastebin.com

i cant seem to paste the code into the body. i will use pastebin.

5. hello?

6. Patience child...

EDIT: Besides, sparkomemphis already gave you some clues as to some issues, yet I don't see revised code on your pastebin, nor any specific questions related to his/her suggestions or what it is exactly that you're having trouble with.

7. Originally Posted by ringoraymes
i cant seem to paste the code into the body. i will use pastebin.
What OS and editor / IDE are you using?

8. i just couldnt paste the code into the body of the reply space, thats it.

9. Ok, you've got an array of structures.

Code:
`struct data Students[NO_OF_STUDENTS]; // 20 students.`
But you also have an structure of arrays:

Code:
```struct data
{
char name[20];
int age[20];
int weight[20];
};```
You do not need both, but you will need to leave the name as is (if 20 bytes is enough). I recommend the array of structures.

Additionally, you haven't defined NO_OF_STUDENTS, but you also have magic numbers of 20 for your arrays. I recommend defining these numbers symbolically.

Since age and weight are ints, you don't need to have:

Code:
`scanf("%19d", Students[Counter].age); // Extract 19 characters,`
just

Code:
`scanf("%d", Students[Counter].age); // Extract 19 characters.`
Also, scanf needs the address of the variable it is to store the input.

There is also the issue with setting Counter to 0 outside of a for loop. Don't even bother with a for loop in this case. A do/while will do what you need. Have the while condition look for an end condition (20 records input or a terminating signal of some kind).

Once you have input working, you can work on the sorting aspect.

10. i really don't how to go about sorting any of this. the inputs been working already, my question was more so directed towards the sorting process, i cant do this on my own.

11. You could qsort the array of structs.

12. whats that?

13. We don't expect you to do it on your own, hence the existence of this forum. But we do expect you to try it on your own, and we will help. We can't just write it for you, you wont learn that way, and it violates our homework policy (read the board guidelines if you haven't). You were "natrually deficient" in network management before you ever really tried it (as are all humans -- we just weren't designed for it), but I'm sure you're pretty good at it now. Programming is the same, everybody sucks at it at first. It's the approach and attitude to learning the craft that makes one a master. So no more "I suck" excuses or pity pleas .

I strongly suggest making the age and weight arrays in your struct single variables, as sparkomemphis and Cynic suggested. Then, you need to make sure you can successfully print your data. You wont know whether your sort works if you can't print the data before and after you sort.

Then, you need to pick a sorting algorithm. If the assignment doesn't specify one, I would suggest insertion sort. It's fairly simple and intuitive. Read up on it at Wikipedia. Their sort algorithm pages tend to be quite good and have good pseudo code to get you started. Make your best attempt at writing something, and post back here, no matter how ugly. We'll go from there.

14. Originally Posted by ringoraymes
whats that?
qsort is a standard C library function that does the sorting for you. Since you didn't know that, I'm guessing the prof didn't mention it, thus you're not supposed to use it.

15. Originally Posted by anduril462
Then, you need to pick a sorting algorithm. If the assignment doesn't specify one, I would suggest insertion sort. It's fairly simple and intuitive. Read up on it at Wikipedia. Their sort algorithm pages tend to be quite good and have good pseudo code to get you started. Make your best attempt at writing something, and post back here, no matter how ugly. We'll go from there.
If this is an entry level programming class, I don't think they would have covered such algorithms yet. I could be wrong, but my first programming class involved only the most basic. It wasn't until the second class that we start to go over searching and sorting.

Originally Posted by anduril462
qsort is a standard C library function that does the sorting for you. Since you didn't know that, I'm guessing the prof didn't mention it, thus you're not supposed to use it.

I'd hazard that the prof doesn't care how it's done if he/she didn't cover sorting algorithms. A simple linear would be fine in this case.

Page 1 of 2 12 Last