How to Search Database & Find Phone Number in Fortran Program?

  • Context: Comp Sci 
  • Thread starter Thread starter Nugso
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around creating a phone-book program in Fortran that allows users to add, update, and search for entries. Participants are exploring various algorithms for searching and updating records, as well as addressing issues related to file output formatting and program functionality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant outlines the requirements for the phone-book program, including the need for searching and updating entries.
  • Another participant suggests using a linear search algorithm as a straightforward method for searching through entries, while also mentioning the potential for more efficient methods like associative arrays or binary search.
  • Some participants express difficulty in understanding search algorithms and seek clarification on their implementation.
  • There is a suggestion to implement the search functionality first to check for duplicate entries before adding new records.
  • One participant shares a link to a resource for searching arrays in Fortran.
  • Another participant raises a concern about the program not saving multiple entries correctly and only creating an empty text file.
  • Participants discuss the formatting of output data in the text file, with suggestions on how to structure the WRITE statement to achieve the desired format.
  • A participant shares their code and asks for help with implementing a search feature that prompts for an update if a duplicate entry is found.

Areas of Agreement / Disagreement

Participants generally agree on the need for a search function to check for duplicates before adding new entries. However, there is no consensus on the best approach to implement the search and update functionalities, as different methods and algorithms are proposed and discussed.

Contextual Notes

Participants express uncertainty regarding the implementation details of search algorithms and the handling of file output. There are also unresolved issues related to the program's ability to manage multiple entries and save data correctly to the hard disk.

Nugso
Gold Member
Messages
170
Reaction score
10
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?
 
Physics news on Phys.org
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).
 
Thanks, milesyoung. I'm now trying to figure out how such searching methods work as they're sort of difficult for me to understand.
 
milesyoung said:
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).

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.
 
milesyoung said:
Try implementing linear search first, it's by far the easiest.

This was the first link on Google for 'search array fortran':
http://www.shocksolution.com/2011/03/finding-value-in-unordered-fortran-array/

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.
 
Nugso said:
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.

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:
milesyoung said:
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.

Oh, geez, my bad! Thank you milesyoung. I wish that was not difficult, though. :frown:
 
Nugso said:
Oh, geez, my bad! Thank you milesyoung. I wish that was not difficult, though. :frown:

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.
 
  • #10
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.
 
  • #11
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 9055123123Any ideas?
 
Last edited:
  • #12
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.
 
  • #13
SteamKing said:
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.

Sorry for forgetting to say, I did it. Only the search part left and I'm currenty working on it. Thanks!
 
  • #14
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, want to 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.
 

Similar threads

Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K