Like Tree4Likes
  • 2 Post By tabstop
  • 2 Post By tabstop

Modifying parallel arrays to arrays of structures

This is a discussion on Modifying parallel arrays to arrays of structures within the C Programming forums, part of the General Programming Boards category; I been away from C programming for awhile now, and it's difficult for me to figure this out. The program ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Modifying parallel arrays to arrays of structures

    I been away from C programming for awhile now, and it's difficult for me to figure this out. The program itself works, however I am supposed to turn this code into an array of structures instead of having 3 parallel arrays.

    I heard it doesn't take long if you already have a working set of code.


    Code:
    #include <stdio.h>  // preprocessor directive to allow use of printf()/sca$
    #include <string.h> //preprocessor directive to allow use of strings
       
     //typedef below used to create a structure tag for structure definitions
    typedef struct person { //Algorithm step 2
                           char first_name[20]; //array "first_name"element
        
                           //Algorithm step 3
                           char last_name[20]; //array "last_name"element
           
                           //Algorithm step 4
                           char age[4]; //array "age" element
     
                           }ppl ; //tag name
     
      
     void populate (ppl []); //PCF populate prototype
     void last_name_sort (ppl []); //PCF last_name_sort prototype
      
     int main (void ) //Main function- returns and integer/ accepts no argumen$
     { //Marks beginning of the main function's statement
       int i; //integer i created and initialized
       
       //Algorithm step 1 completed in next four lines
       printf("Hello! \nThe purpose of this program is to use parallel arrays $
       printf("order the names aphabetically (by last name) without mixing the$
       printf("names and ages. The newly sorted information will be printed to$
       printf("screen. \n\n\n\n");
       
       ppl person[7]; //defined structure "person"
     
       populate (person); //defined structure passed to PCF populate
     
        last_name_sort (person); //defined structure passed to PCF last_name_s$
        printf("\n\n<-------------------------~SORTED LIST~-------------------$
        for (i=0; i<= 6; i++)
        {
           //Algorithm step 8 in for loop
           printf("%s %s %s\n", person[i].first_name , person[i].last_name, pe$
        }
      
     printf("\n\n\n");
     printf("Thank you for using my program. I hope it was helpful!\n\n");
     return (0) ;
     }
     
     void populate (ppl person[])
     {
     int i; //creation of integer variable i
     
       printf("Please enter a first name, last name, and age with a space in b$
       printf("and the first letter of the first and last name capitalized: \n$
       printf("\n\n<-----------------------~UNSORTED LIST~--------------------$
     
      for (i=0; i<= 6; i++)
      {
       scanf("%s %s %s",person[i].first_name , person[i].last_name, person[i].$
      }
     
     return;
     }
     void last_name_sort( ppl person[])
     {
     int i, j;
     char temp[20]; //creation of first temporary array for 3way switch of las$
     char temp_1[20]; //creation of first temporary array for 3way switch of f$
     char temp_2[4]; //creation of first temporary array for 3way switchof age
     
     for (i=0; i<=5; i++) //outer for loop
     {
     for(j=0; j<=5; j++) //inner "nested" for loop
     {
     if (strncmp(person[j].last_name, person[j+1].last_name, 7) >0)
     {
     //Algorithm for last name sorting
     strcpy (temp, person[j].last_name);
     strcpy (person[j].last_name, person[j+1].last_name);
     strcpy (person[j+1].last_name, temp);
     
     //Algorithm so first name follows where last name is sorted
     strcpy (temp_1, person[j].first_name);
     strcpy (person[j].first_name, person[j+1].first_name);
     strcpy (person[j+1].first_name, temp_1);
     
     //Algorithm  so age follows where last name is sorted
     strcpy (temp_2, person[j].age);
     strcpy (person[j].age, person[j+1].age);
     strcpy (person[j+1].age, temp_2);
     }
     }
      
     return;
     }
    }

    Any tip, help or comment would be great!
    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    ...

    ...

    You have an array of structures.

    (Although you can assign structures these days, so you can do the swap using a temporary ppl object, without having to copy each field over, if that was your question.)
    Last edited by tabstop; 07-27-2011 at 07:08 PM.
    Matticus and AndrewHunter like this.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You're going to run into problems with this line...
    Code:
       scanf("%s %s %s",person[i].first_name , person[i].last_name, person[i].$

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CommonTater View Post
    You're going to run into problems with this line...
    Code:
       scanf("%s %s %s",person[i].first_name , person[i].last_name, person[i].$
    People who use emacs need our compassion and understanding, not our scorn.
    TheBigH and KCfromNC like this.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Yeah, that's what I thought. The ppl structure is already consisted of having 3 arrays inside it, making it arrays of structure, right?

    I was confused 'cause he goes "aight, make this arrays of structure." and I'm staring at the code thinking exact that.

    Thank you though, for not bashing me lol.


    and Tater, am I going to have a problem because the line ends with $? Isn't it doing that because the code I pasted did not copy the entire thing? Or?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xkohtax View Post
    Yeah, that's what I thought. The ppl structure is already consisted of having 3 arrays inside it, making it arrays of structure, right?
    No. It makes it a structure that has some arrays. This is an array of structures:
    Code:
    struct foo
    {
        ... whatever you want here ...
    } arrayof[ 5 ];
    arrayof is an array of five struct foo things.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or in our case
    Code:
    ppl person[7];

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by xkohtax View Post
    ......
    I was confused 'cause he goes "aight, make this arrays of structure." and I'm staring at the code thinking exact that.
    He was probably referring to how you sort you array of structures. Refer to:

    Quote Originally Posted by tabstop View Post
    .......
    (Although you can assign structures these days, so you can do the swap using a temporary ppl object, without having to copy each field over, if that was your question.)
    Which would clean your code up quite a bit.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb question: Modifying Arrays
    By Almina in forum C++ Programming
    Replies: 1
    Last Post: 07-07-2008, 11:17 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Parallel Arrays
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2003, 03:18 PM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 10:14 PM
  5. parallel arrays
    By KeeNCPP in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2001, 09:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21