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

  • Thread starter Thread starter Nugso
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
The discussion focuses on creating a phone-book program in Fortran that allows users to input names, surnames, and phone numbers while avoiding duplicate entries. Users are advised to implement a linear search algorithm to find existing records, which can also facilitate updates to entries. The importance of modular programming is emphasized, suggesting that functions for searching and updating should be created for better organization and reusability. Additionally, participants discuss formatting output correctly when writing to a file and share resources for further assistance. Overall, the thread provides guidance on implementing essential features for a functional phone-book application.
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
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K