0

I have an assignment in which I have to create a struct with some data:

typedef struct MEMBER{
char name[20];
char surname[20];
int age;
}member;

Then I need to read a file into stucture and perform some functions like adding, displaying and searching. However it is not specified how to store these structs, probably it's my fault becasue I do not know. So my question is: do I need to create an array of struct MEMBER to perform search and diplaying? Or maybe it is other way to solve it?

  • What does the file look like? – ForceBru Dec 09 '19 at 17:44
  • ``` John Smith Smith 20 Harry Potter 31 Hermiona Fiona Granger 31 ``` – Wojciech Sowiński Dec 09 '19 at 17:45
  • 1
    you can use arrays or lists. You should start with a simpler problem; just hard code the input and test your ideas without using the files. – john elemans Dec 09 '19 at 17:45
  • An array is one of many possible data structures that could hold this data. It's likely not the optimal one for searching. – Mad Physicist Dec 09 '19 at 17:46
  • Wait, I'm pretty sure Harry had a middle name or two. Something's off with your data :) – Mad Physicist Dec 09 '19 at 17:47
  • Is a binary tree one of possible option to store the members? – Wojciech Sowiński Dec 09 '19 at 17:47
  • @WojciechSowiński, well, "John Smith 20" is one person, "Harry Potter 31" is another, and so on. So, there is a potentially unknown amount of `MEMBER`s. It's not hard to see that you need to store multiple `struct`s – ForceBru Dec 09 '19 at 17:47
  • Yes, the thing is that some person have more than one name and I need to read the whole line into one variable of struct. So surname for John is "Smith Smith". – Wojciech Sowiński Dec 09 '19 at 17:50
  • if the assignment doesn't specify what kind of data structure to use, use an array or linked list. Anything more complicated is overkill IMO.. you won't be dealing with a huge data set. – yano Dec 09 '19 at 17:51
  • @ForceBru yes, that's why I asked for advice in storing these. Should it be an array or maybe a tree, or something different? – Wojciech Sowiński Dec 09 '19 at 17:52
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] of your own attempt and show it to us together with a description about what problems you have with it. – Some programmer dude Dec 09 '19 at 17:52
  • @WojciechSowiński, the easiest way is to use an array, of course. I mean, nobody can stop you from using trees or linked lists, but that'd probably be overkill – ForceBru Dec 09 '19 at 17:53
  • Now I'm pretty sure they require binary search tree to hold the data using name as a key... awesome – Wojciech Sowiński Dec 09 '19 at 18:01
  • thanks guys anyway!! – Wojciech Sowiński Dec 09 '19 at 18:02

1 Answers1

0

However it is not specified how to store these structs

Well, that is exactly your job to figure that out.

There are multiple ways of storing data. For any solution you would want to use dynamic memory allocation - that is malloc and friends. You can use an array, a list (singly or double), a tree, a hash map, a ... And you can make them sorted or unsorted.

Which one to use depends on the programs typical usage. For instance - if the program usage often insert new element in the middle (or delete in the middle), an array is likely a bad solution - a list would be better. However, if elements are typically added to the end of the data structure, a dynamic allocated array (with realloc) is probably a good way.

What you need to do...

Study the different data structures and learn the benefits and downsides of them. Then analyze your programs behavior.

What is most important?

Fast insert?

Fast delete?

Fast search?

Then you can select the optimal data structure.

C++ offers these data structures - aka containers - and you can probably learn from them. See How can I efficiently select a Standard Library container in C++11? for more information.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63