Pairs of words

This is a discussion on Pairs of words within the C Programming forums, part of the General Programming Boards category; Hello! I am trying to write a dictionary type program where I basically just save a bunch of pairs of ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11

    Pairs of words

    Hello!

    I am trying to write a dictionary type program where I basically just save a bunch of pairs of words, e.g. Spanish -> English. I want to be able to do quick lookups both ways (from Spanish to English, or the other way around).

    The idea is that a word will pop up every x minutes, and I have to fill in the one that matches, in the other language.

    What sort of data structure do you recommend for this? I thought about making a linked list, since that's the only structure I am somewhat familiar with. Any feedback would be greatly appreciated, and I am sure it will give me some training if I am better off implementing something other than a linked list.

    (My program will never contain more than a few hundred words, so it's not like I really need that much speed).

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    for quick look-up you may implement a hash table

    actually you will build 1 table for each source language.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11
    Will that allow me to do lookups both ways? E.g. both lookup("cat") and the associated lookup("gato")?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    If you have two hash tables, yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought if you knew how to work with hash tables, you wouldn't have asked, so something simpler could be in order.

    You could use a simple array of words:
    char words[number of Words[200][30];

    and file up one half of it with English words, say 0-99, and use 100-199 for the Spanish word in this fashion:

    words[0] in English is words[100] in Spanish
    words[1] in English is words[101] in Spanish
    ....
    words[99] in English is words[199] in Spanish

    You could leave all the words unsorted, or sort the English half, and match all the swaps you make in there, with the associated swaps in the Spanish half. That would allow a much more efficient binary search for the words.

    For 100 words it's not necessary, but for 300 words or more, it will gradually become very helpful to speed up the search.

    Nothing at all wrong with using a hash table, however. Just an option that's a bit simpler.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Adak View Post
    I thought if you knew how to work with hash tables, you wouldn't have asked, so something simpler could be in order.


    You could leave all the words unsorted, or sort the English half, and match all the swaps you make in there, with the associated swaps in the Spanish half.
    Better yet to have an unsorted array of pairs (or later - bigger groups of words) and sorted array of pointers to word group.
    So one pointer array could be sorted by English words, second by Spanish, ... third by any other language added later.
    Sorting of pointers will not involve moving of main data at all.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing key, value pairs
    By tim8 in forum C Programming
    Replies: 14
    Last Post: 03-27-2013, 03:03 PM
  2. stl for pairs
    By dpp in forum C++ Programming
    Replies: 14
    Last Post: 05-18-2009, 09:46 AM
  3. vector and pairs
    By dpp in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 10:24 AM
  4. prime pairs from 3 - 10,000
    By david999 in forum C Programming
    Replies: 4
    Last Post: 11-08-2005, 11:47 PM
  5. pairs of vector..
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2002, 01:09 AM

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