Thread: Using structures 1st time..I am stuck

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Using structures 1st time..I am stuck

    This program I am trying to write has a structure of persons with a first name last name and age for each.

    What I need it to do it display the order of the persons before sorting and then display the persons after sorting by age.

    So far I have written the overall program but am stuck at the most important step...which is defining the functions that will be doing all of the work.

    For sort_by_age I want use bubble sorting by writing the function myself and for print_person_info I am confused on how to print data out of the struct. I am pretty lost here please help me out thank you.

    Code:
    #include <stdio.h>
    struct person
    {
       char first_name[20];
       char last_name[20];
       int age;
    };
    void print_person_info(struct person clone);
    void sort_by_age(int n, struct person a[]);
    
    int main(void)
    {
       int I, n=5;
      struct person student[5] =
      {
       { "Bob",   "Smith",    21},
       { "Jimmy", "John",     18},
       { "Amy",   "Goldberg", 20},
       { "Dan",   "Marlo",    17},
       { "Sally", "Sorrow",   16}
      };
       for(I=0; I<n; I++)
        print_person_info(student[I]);
        sort_by_age(n, student);
      
    printf("------AFTER SORTING-------\n");
     for(I=0; I<n; I++)
      print_person_info(student[I]);
    
                    return 0;
    }
    void print_person_info(struct person clone)
    {
      printf("Name = %s  %s, person.first_name, person.last_name");
      printf("Age    = %I, person.age");
    }
    
    void sort_by_age(int n, struct person a[])
    {
         int I=0, j, temp;
      
         for(I=0; j<n-1;i++)
        {
          for(j=I+1;j<n;j++)
          { 
             if(student[I] > student[j])
             {
                  temp =student[I];
                  student[I]=student[j];
                  student[j]=temp;
              }
           }
        }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When I use a struct, I work with the struct members using the dot operator.

    To print out the five student records for instance, I'd use:
    Code:
    for(int i = 0; i < 5; i++) 
       printf("First name: %s  Last_name: %s  Age: %d n", student[i].first_name, student[i].last_name, student[i].age);
    In your sorting function, you have a nice sort, except:

    1) It's a Substitution sort - very similar to a bubble sort, but not quite the same.

    2) You need to compare the student[i].age with student[j].age. And please, don't use uppercase i's because this I looks too much like 1.

    Lowercase that i, please.

    We have an excellent C tutorial link at the top middle portion of this website. You should spend some time there, and work through them. This is basic stuff, so you need to up your game to keep up with a class.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Procedure print_person_info

    The print person procedure is wrong, the printf prints "Name = %s %s, person.first_name, person.last_name" instead of you want to print, here some example printf - C++ Reference and this is how you can write the printf to make your task:

    Code:
        printf("Name = %s  %s ,Age = %d\n", clone.first_name, clone.last_name,clone.age);
    struct person is the struct tag, clone is the name of the var, why are you using person.age?

    Procedure sort_by_age

    There are some errors:

    -you've declared temp like an int but you are using it as temporary container of a person info

    -"student[I] > student[j] " I suppose you want to compare the age of two persons, so change this.

    - for(I=0; j<n-1;i++) you typed j instead of I (as Adak correcty says don't use uppercase i)
    Last edited by root; 11-16-2013 at 02:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. Replies: 1
    Last Post: 06-28-2011, 05:27 PM
  4. completely stuck understanding how to use time.h
    By pastitprogram in forum C++ Programming
    Replies: 11
    Last Post: 04-04-2008, 10:11 AM
  5. stuck on time!!!!
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2002, 07:51 AM

Tags for this Thread