473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

help with arrays

I need to create an array that consists of ints and strings (last name and age) and then sort it first by name and then by age, but i have no idea how to do that. Can anybody help??
Mar 27 '06 #1
2 1408
Banfa
9,065 Expert Mod 8TB
You're are thinking the wrong way round, starting with the idea that you need an array. You have not said what language you are programming in so I will use C as it's my first language (and English is my second :D).

You need to start with what you want to store for each person (name and age).

Additionally you are going to have move than 1 and you need to be able to store these in a list and this list needs to be sorted into a specific order.

To hold the data you could us a structure like this

Expand|Select|Wrap|Line Numbers
  1. struct person {
  2.     char surname[20];
  3.     unsigned int age;
  4. };
  5.  
Now you have said that you need an array however I do not think that is the best solution. Consider the size of the structure, 24 bytes in this case. Assume that in order to keep the ordering of the list you need to insert a person into the 4th position.

Now if your array currently contains 10 entries then you will need to move 7 entries up, at 24 bytes each that is 168 bytes of data to move. Suppose your list contains 400 entries, you will need to copy 397 entries or 9528 bytes, also suppose that rather than 24 bytes the structure is 150 bytes, then you will be copying 59550 bytes.

As you can see this solution is not scaleable, that is as the structure size and array size gets bigger more and more work is required to insert (or delete) items. AT some point the data will get too large to make this a sensible solution.

A better solutition is a linked list. In a linked list the data structure contains a pointer to the next structure in the list. What this means is that inserting (or deleting) an item from the list is much easier. Taking the example above to insert a new person into the list all I have to do is alter the pointer in the 3rd item to point to the new item and the pointer in the new item to point to the item that the 3rd item was pointing to. This is 2 pointer assignments or 8 bytes copied and it will be the same no matter how big the structure or how long the list. This solution is scalable.

The structure would be altered to

Expand|Select|Wrap|Line Numbers
  1. struct person {
  2.     struct person *pNext;
  3.     char surname[20];
  4.     unsigned int age;
  5. };
  6.  
Another advantage is that if you have more than 1 order that you want the list stored in than you can just add another pointer to the structure and link the new pointers in a different order.

In this sort of solution it is normal for the top level data to just be a pointer to the structure being stored and to use malloc to create additional structures as require (this also gets round the problem of having to create an array large enough for the data). When freeing the structures remember that you can use data once you have freed it, normally this means reading the next pointer into a temporary variable then freeing the structure.
Mar 27 '06 #2
Thanks,
Even though i forgot to mention the language, which is java, I think I got the point and should be able to complete the program.
Thanks again.
Mar 28 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.