Thread: Abstract data structure question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    Abstract data structure question

    Hello, I am in the process of starting to write a program, already have the design and other details fairly figured out but i am doing a extension to a payroll system where i take the data from the payroll and then process it and print multiple copies of data in the form of pay slips, forms for NIS, NHT etc. each one must only contain the necessary info(NIS shouldnt have NHT on it).

    Therefore, i was thinking of using a array but then to take the data from the payroll or say a text file, i wouldnt be able to put all the data in an array since it only has one type and cannot store differing data types. I need to use a ADT to manipulate the data, is structures a ADT? I think i know how to do this with a structure but it would be tedious(at least i think i know how to do it but i dont think i can use it for this project)

    So would a Stack, queue or an array be more suitable in this situation?
    while reading i didnt see how a stack or a queue would work, maybe a list would work but i would need to read up on them some more. arrays seem like the most logical thing to use but maybe a combination of structures and arrays could do it? but then wouldnt the array be redundant.....

    I am very sorry if this is jumbled, i will try to sum it up, to store differing data types where i will need to pick out specific fields out of the data types, what would be a suitable Abstract data type(queues, stacks, lists, arrays)?

    thanks in advance..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would use an array of structs - ez to work with. Lists are fine, but I've never seen the day that they were nearly as easy to work with.

    Just make up your struct and then make an array of them, and boom! you're good to start out.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Shadow20 View Post
    Hello, I am in the process of starting to write a program, already have the design and other details fairly figured out but i am doing a extension to a payroll system where i take the data from the payroll and then process it and print multiple copies of data in the form of pay slips, forms for NIS, NHT etc. each one must only contain the necessary info(NIS shouldnt have NHT on it).

    Therefore, i was thinking of using a array but then to take the data from the payroll or say a text file, i wouldnt be able to put all the data in an array since it only has one type and cannot store differing data types. I need to use a ADT to manipulate the data, is structures a ADT? I think i know how to do this with a structure but it would be tedious(at least i think i know how to do it but i dont think i can use it for this project)

    So would a Stack, queue or an array be more suitable in this situation?
    while reading i didnt see how a stack or a queue would work, maybe a list would work but i would need to read up on them some more. arrays seem like the most logical thing to use but maybe a combination of structures and arrays could do it? but then wouldnt the array be redundant.....

    I am very sorry if this is jumbled, i will try to sum it up, to store differing data types where i will need to pick out specific fields out of the data types, what would be a suitable Abstract data type(queues, stacks, lists, arrays)?

    thanks in advance..
    How is the data in your source file organized?
    If it's a random access file consisting of records, the record should be described by a struct... This will give you the means to access the files... If they're in a data base this gets a whole lot more complicated... so the source of your data is crucial to making this work.

    You describe a single data source with 2 outputs... that's the easy part. Load one employee file and do the required printouts, then load the next, and repeat the process... one record at a time.

    And what on earth do you mean by ADT?

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by Adak View Post
    I would use an array of structs - ez to work with. Lists are fine, but I've never seen the day that they were nearly as easy to work with.

    Just make up your struct and then make an array of them, and boom! you're good to start out.
    how would you do that? could you give an example if possible?

    hmm, would it be something like making a struct and using the fields you want from the struct and storing them in an array and using them from there? i have a little experience with struct from my last project but i am pretty new to arrays, time to get reading i suppose

    thanks.

    @CommonTater

    in the source file i was planning to have it like this in a text file:Eg
    employee id gross pay net pay NIS
    john brown 005 $25000 $19500 650

    so yes the data is in a random access file, definitely not in a database, so in this case a struct is actually mandatory?

    and ADT: abstract data type EG stacks, queues and arrays

  5. #5

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Therefore, i was thinking of using a array but then to take the data from the payroll or say a text file, i wouldnt be able to put all the data in an array since it only has one type and cannot store differing data types. I need to use a ADT to manipulate the data, is structures a ADT?
    Yes, a structure is an abstract data type, in every sense you want to use ADT. structs exist to make user-defined types, which are not abstract. An abstract data type is an object oriented programming design where a programmer takes the common functionality out of similar classes and reorganizes those parts into a base class, and derives all of the similar classes instead. In short, you're not really discussing procedural C, which I think is a mistake.

    Even if that's intentional, I'm not sure an ADT would help anyway. ADTs do little to improve homogeneous structures like arrays, stacks, and queues. The moment you flex your muscle and try to make these things heterogeneous, you have to employ RTTI to know what element is what type of object, etc.

    OTOH, if you make a user-defined type Employee, you can easily contain all of the data related to such a person. Since every element of a stack, queue, or array would be an Employee as well, the data is homogeneous.

    So would a Stack, queue or an array be more suitable in this situation?
    while reading i didnt see how a stack or a queue would work, maybe a list would work but i would need to read up on them some more.
    Whether a stack, queue, list or array is appropriate depends on a number of factors. Queues are basically stacks ordered by priority, so you need to know if that aids processing items before you use a queue or a stack. Arrays on the other hand are simply a fixed number items stored contiguously. Most of the time, an array will be a suitable data structure, unless you need the specific properties of a queue or a stack, or something else. Last but not least, lists might also be appropriate, because they aren't limited to a fixed number of elements, but they don't provide random access to elements in C.
    Last edited by whiteflags; 03-26-2011 at 05:12 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a simple example of working with an array of structs. Instead of reading the data from a file, in this example, it's initialized into the array.

    Code:
    /*
    
    */
    
    #include <stdio.h>
    #define SIZE 5
    
    typedef struct {
      char name[30];
      int points;
      int level;
    }player;
    
    int main() {
      int i, j, sorted;
      player temp;
      player p[SIZE]={
      {"Alice",222, 4},
      {"Tyler", 194, 5},
      {"Chris", 190, 5},
      {"Tom", 210, 6},
      {"Mary", 208, 6}};
    
      //sort the records, by ascending points
        
      for(i=0; i<SIZE-1; i++)
      {
          for(j=i+1; j<SIZE; j++)
          {
              if(p[i].points > p[j].points)
              {
                  temp = p[i];
                  p[i] = p[j];
                  p[j] = temp;
              }
          }
      }
      //show the records:
      printf("\nPlayer records, in sorted order: \n");
      for(i=0;i<SIZE;i++) {
        printf("%20s  %3d  %2d\n", p[i].name,p[i].points,p[i].level);
      }
      printf("\n\n\t\t              press enter");
      (void) getchar(); 
      return 0;
    }
    Whiteflags posted:
    Last but not least, lists might also be appropriate, because they aren't limited to a fixed number of elements, but they don't provide random access to elements in C.
    < And are generally a righteous PITA! >
    Last edited by Adak; 03-26-2011 at 05:45 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Shadow20 View Post
    in the source file i was planning to have it like this in a text file:Eg
    employee id gross pay net pay NIS
    john brown 005 $25000 $19500 650

    so yes the data is in a random access file, definitely not in a database, so in this case a struct is actually mandatory?

    and ADT: abstract data type EG stacks, queues and arrays
    That doesn't look like a struct to me... a struct would contain binary numerical data not $25000... So most likely that's a sequential access file, organized in lines. That makes things real easy... read one line, process it, do your printouts, read the next line, process that and so on until you hit the end. There should be no reason to load the whole file at once... Just go line by line... no arrays, no linked lists, vectors or stacks... just good old fashioned loops...

    I'd say you just lucked out on how easy this will be...

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Adak View Post
    < And are generally a righteous PITA! >
    I hope you mean righteous in the sense that they are still useful even if they are hard. And if they are hard, that means that pointers are hard for you, which probably means your understanding of pointers is lacking. The ins-and-outs of linked list operations are pretty generic, so apply some of your ten thousand hours to understanding the data structure.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    I hope you mean righteous in the sense that they are still useful even if they are hard. And if they are hard, that means that pointers are hard for you, which probably means your understanding of pointers is lacking. The ins-and-outs of linked list operations are pretty generic, so apply some of your ten thousand hours to understanding the data structure.
    I work with linked lists from time to time... and I think they're a royal PITA as well.
    Whenever possible I avoid them.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by CommonTater View Post
    That doesn't look like a struct to me... a struct would contain binary numerical data not $25000... So most likely that's a sequential access file, organized in lines. That makes things real easy... read one line, process it, do your printouts, read the next line, process that and so on until you hit the end. There should be no reason to load the whole file at once... Just go line by line... no arrays, no linked lists, vectors or stacks... just good old fashioned loops...

    I'd say you just lucked out on how easy this will be...
    Well i am not really sure, but i made an error, no $ sign is supposed to be there, sorry
    i used a struct to do a payroll system last time, so i figured it was as you said. I am a bit confused...


    @whiteflags
    i think you lost me a little but are you sayings structs are not procedural C but OO like C++?

    @Adak:
    Thanks! will look through it and see how it is


    Thanks for all the replies and help guys

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I work with linked lists from time to time... and I think they're a royal PITA as well.
    Whenever possible I avoid them.
    Who asked your opinion? I'm not in the mood to care about what people find to be a pain, obviously.

    That doesn't look like a struct to me... a struct would contain binary numerical data not $25000...
    What exactly isn't numerical about currency? I mean, turning text into manipulable data types is basic input. Data doesn't have to be formatted so plainly.

    @whiteflags
    i think you lost me a little but are you sayings structs are not procedural C but OO like C++?
    An ADT is different from a user-defined type. Rather than just saying those two things are different, I wanted to explain what an ADT is. It probably doesn't matter now. A struct makes a user-defined type, which is what you probably wanted to know about and use.
    Last edited by whiteflags; 03-26-2011 at 06:41 PM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by whiteflags View Post
    I hope you mean righteous in the sense that they are still useful even if they are hard. And if they are hard, that means that pointers are hard for you, which probably means your understanding of pointers is lacking. The ins-and-outs of linked list operations are pretty generic, so apply some of your ten thousand hours to understanding the data structure.
    Very useful and elegant. Just take more effort to code up, etc.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Shadow20 View Post
    Well i am not really sure, but i made an error, no $ sign is supposed to be there, sorry
    i used a struct to do a payroll system last time, so i figured it was as you said. I am a bit confused...
    Well, usually a struct that is written to disk will have certain recognizeable markers... numerical data will be stored in it's binary form (i.e. not human readable), strings will have their full buffer size with the text at the beginning and a number of nulls after, etc. What you showed me had none of those markers... unless you weren't showing me actual disk content...

    Struct or not... you still have a relatively easy task here. You can load one item (struct or line) and work on it, promptly forget it then load the next... So basically you're writing a processing loop that exits at the end of file... easy stuff.

    Quote Originally Posted by whiteflags View Post
    What exactly isn't numerical about currency? I mean, turning text into manipulable data types is basic input. Data doesn't have to be formatted so plainly.
    I said BINARY numerical data not ASCII numerical data... I'm guessing you've never worked with records based files or at least never bothered to look at one in a hex editor...

    Matter of fact... now that it's on the table... I'm realizing that this might be the first time since I joined up that record base binary data files have even been mentioned... Surprising given they are pretty much an industry standard for data storage.
    Last edited by CommonTater; 03-26-2011 at 08:25 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I said BINARY numerical data not ASCII numerical data... I'm guessing you've never worked with records based files or at least never bothered to look at one in a hex editor...
    I understood what you said, however, you would be wrong if structs are only used on binary data. Just because the file is text it does not preclude the use of a structure.

    Matter of fact... now that it's on the table... I'm realizing that this might be the first time since I joined up that record base binary data files have even been mentioned... Surprising given they are pretty much an industry standard for data storage.
    You should factor in that you are bringing up something irrelevant to the topic.
    Last edited by whiteflags; 03-26-2011 at 08:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  3. Data structure question.
    By ronenk in forum C Programming
    Replies: 9
    Last Post: 10-03-2004, 10:58 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM