1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Searching on Fortran

  1. Apr 23, 2013 #1

    Nugso

    User Avatar
    Gold Member

    Write a program that creates a phone-book (Name, Surname, Phone Number).

    Ask the user the number of entries
    Read user input to an array
    Write this array into a file in the hard-disk
    Your program should avoid multiple entries! If a record already exists in the phone-book, the program should ask the user if he/she wants to update this entry.

    Your program should also be able to search this created database and find a phone number by name, if required.


    The homework I have to do is given above. The first steps are almost done, but I'm struggling with the last part which is " Your program should also be able to search this created database and find a phone number by name, if required." How can I do that?
     
  2. jcsd
  3. Apr 23, 2013 #2
    A simple search algorithm would be to just iterate over all the entries in your array and compare each record to what you're searching for. This is called linear search and is a kind of brute-force method that doesn't scale well with the number of entries.

    You could do much better, for instance, with an associative array or binary search (requires sorting).
     
  4. Apr 23, 2013 #3

    Nugso

    User Avatar
    Gold Member

    Thanks, milesyoung. I'm now trying to figure out how such searching methods work as they're sort of difficult for me to understand.
     
  5. Apr 23, 2013 #4

    Nugso

    User Avatar
    Gold Member

    I think I have another problem now I guess. All of the binary search or linear search commands I've come across are 'programs' themselves.
     
  6. Apr 23, 2013 #5
  7. Apr 23, 2013 #6

    Nugso

    User Avatar
    Gold Member

    Thank you once again milesyoung. I'd like to ask one more thing;

    "Your program should avoid multiple entries! If a record already exists in the phone-book, the program should ask the user if he/she wants to update this entry."

    Do you also know the update thingy? I'm guessing that update should as well include search command, as it cannot update without searching the arrays.
     
  8. Apr 23, 2013 #7
    It's usually good practice to subdivide your program into small functions that each does something very specific, which makes it easy to verify that each small part of your program works as intended.

    This strategy also makes your program modular, such that you might be able to reuse parts of it. You could, for instance, make a function that implements linear search to find a record in your array, which you could then use to implement both the update and search features of your program.

    Edit:
    I think you answered your own question. You need to know if there's a duplicate record in the database before you make a new entry. Simply searching it beforehand would give you an answer.
     
    Last edited: Apr 23, 2013
  9. Apr 23, 2013 #8

    Nugso

    User Avatar
    Gold Member

    Oh, geez, my bad! Thank you milesyoung. I wish that was not difficult, though. :frown:
     
  10. Apr 23, 2013 #9
    You're welcome. It might seem difficult now, but get a bit of experience with it and it suddenly won't seem so difficult.

    Have a go at it and post if there's something specific you don't understand.
     
  11. Apr 23, 2013 #10

    Nugso

    User Avatar
    Gold Member

    http://paste.servut.us/2dqg. It works fine when I want to add only one student. But It does not actually work with more than that. How can I fix that? Also it does not save it to harddisk. It only creates an empty txt file.
     
  12. Apr 23, 2013 #11

    Nugso

    User Avatar
    Gold Member

    http://paste.servut.us/jc8p

    Okay the latest version. Everything is done. But the problem is the text files shows it as;

    white
    gandalf
    9055123123

    but I want it as;

    white gandalf 9055123123


    Any ideas?
     
    Last edited: Apr 23, 2013
  13. Apr 23, 2013 #12

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    It depends on how you are writing the data to output. You want to set up a WRITE statement which prints out the name and phone number at one time.
     
  14. Apr 23, 2013 #13

    Nugso

    User Avatar
    Gold Member

    Sorry for forgetting to say, I did it. Only the search part left and I'm currenty working on it. Thanks!
     
  15. Apr 23, 2013 #14

    Nugso

    User Avatar
    Gold Member

    Okay. Here's the program I've written;

    http://paste.servut.us/7w2l

    I tried doing all of the things that I've come across in Google but they have yet to help me. So my question is I'd like to do a search and if a duplicate is found the program should ask " It already exists, do you like to update anyway?". I'm trying to do it with;

    if(name(i)==xxxx)then
    print*,"it already exists, wanna update?"
    read*,yes_or_no

    So what should I type instead of xxxx? I'm really having big problems figuring out what. Also, if anybody has another idea, I'd really like to hear.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Searching on Fortran
  1. Graphs in fortran (Replies: 1)

  2. Fortran help (Replies: 16)

  3. Fortran 90 (Replies: 1)

  4. Fortran code (Replies: 1)

Loading...