Like Tree3Likes

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. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    41

    Exclamation 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.
    Last edited by ringoraymes; 04-18-2012 at 03:13 PM. Reason: code update

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Perhaps you can edit it again, so it isn't all on one long line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    76
    Why do you have 20 ages and 20 weights ?
    they are ints not strings ( char * or char[] ).

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    41

    Exclamation

    [C] C - Pastebin.com


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

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    41
    hello?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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.
    Last edited by anduril462; 04-18-2012 at 04:07 PM.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by ringoraymes View Post
    i cant seem to paste the code into the body. i will use pastebin.
    What OS and editor / IDE are you using?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    41
    i just couldnt paste the code into the body of the reply space, thats it.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    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. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    41
    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. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    You could qsort the array of structs.

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    41
    whats that?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by ringoraymes View Post
    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. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by anduril462 View Post
    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.

    Quote Originally Posted by anduril462 View Post
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structs and classes problem!!!!
    By faisal791 in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2010, 02:14 PM
  2. Problem with structs
    By darkstorm in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2009, 05:18 PM
  3. Problem with pointers to structs
    By mike_g in forum C Programming
    Replies: 2
    Last Post: 03-23-2008, 01:03 PM
  4. Basic problem structs
    By delete6145 in forum C Programming
    Replies: 1
    Last Post: 11-27-2007, 09:43 AM
  5. problem in structs
    By St0rM-MaN in forum C Programming
    Replies: 6
    Last Post: 05-06-2007, 08:13 AM

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