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

  • Comp Sci
  • Thread starter Nugso
  • Start date
  • Tags
    Fortran
In summary: 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.
  • #1
Nugso
Gold Member
170
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
  • #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).
 
  • #3
Thanks, milesyoung. I'm now trying to figure out how such searching methods work as they're sort of difficult for me to understand.
 
  • #4
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.
 
  • #6
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.
 
  • #7
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:
  • #8
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:
 
  • #9
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.
 

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

1. How do I search a database using Fortran?

Fortran has built-in functions and features for working with databases, such as the SELECT and WHERE statements. These can be used to search for specific data within a database.

2. Can I use Fortran to find a phone number in a database?

Yes, Fortran can be used to search for a phone number in a database. You can use the WHERE statement to specify the phone number you are looking for and retrieve the corresponding data.

3. What is the best way to search a large database efficiently in Fortran?

To search a large database efficiently in Fortran, it is recommended to use optimized algorithms and techniques such as binary search or hash tables. These can greatly reduce the time and resources needed for the search.

4. Is there a specific format for phone numbers in Fortran?

There is no specific format for phone numbers in Fortran. It will depend on the format used in the database you are searching. However, you can use functions and string manipulation to format the phone number in a specific way if needed.

5. Can I search for a phone number in a database using only partial information?

Yes, you can search for a phone number in a database using partial information in Fortran. The LIKE statement can be used to search for a pattern or partial string within a database column, such as a phone number.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top