Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,


i am having a problem. i have made a file "dataele" where i have stored all the atomic number and their respective atomic mass, name, symbol. but i want to open only a particular element according to user choice. The user will input the atomic number and the program will search for its details form the computer file dataele...am unable to do it....plz help me out.................

THANKS :-}
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 3:39am    
What does it mean "open an element"? And what's the problem?
--SA

Let's remember how many elements are there is the periodic table: just 118. Very small number, just nothing. So, all you need is this: read the whole file at once in the array of elements, each element being a structure, a cell of the periodic table.

As the array is indexed by the index, put each element at the index equal to its atomic number. No more reading data, no search needed. You need atomic mass in input, the structure is found by its index. That's it.

—SA
 
Share this answer
 
Comments
Amir Mahfoozi 29-Jan-12 3:51am    
+5 good point. He can use user input as the element index. :) that means the whole search is just an access to an array by index.
Sergey Alexandrovich Kryukov 29-Jan-12 3:52am    
Exactly, and this access in one CPU instruction.
Thank you, Amir.
--SA
Espen Harlinn 29-Jan-12 13:35pm    
Nice and simple, my 5 :)
Sergey Alexandrovich Kryukov 29-Jan-12 13:37pm    
Thank you, Espen.
--SA
So start by defining a struct for each element then make an array of those structs.

Then load data from the storage file and fill that array at program startup.

After that let user input their desired atomic number.

According to the input number search that array and show the details of the found array element.

Writing the steps of a project is almost 80% (or even more) of the job ;)

Hope it helps.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 3:48am    
Amir, this time you made it more complex than it should be. There are only 118 elements. Read them all in the array of elements (structures) and you are done. Can anything be easier? (So I did not vote this time.)

Please see my answer.
--SA
Espen Harlinn 29-Jan-12 13:34pm    
5'ed!
Amir Mahfoozi 29-Jan-12 23:35pm    
Thanks Espen.
This smells heavily of homework, so no code!

How you do it will depend on how you have stored your data:
If it is as a text file, with each line representing a single element, and comma delimited values, it is relatively simple:
1) Get the user input, convert it to a number.
2) Set up a loop to read each line of the file.
3) For each line, convert the first field (up to a comma) to a number.
4) Compare this number against the user entered number.
5) If they are not the same, go back to step (3)
6) You now have the details you need and can present them to your user.

If you haven't stored it like this, then it may be more complex to access each row of data, but the principle is still the same.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 3:46am    
Griff, this time you made it more complex than it should be. Only 118 elements. Read them all in the array of elements (structures) and you are done. Can anything be easier?
(I did not vote this time.)

Please see my answer.
--SA
deepak.hunk 29-Jan-12 4:34am    
if i would have done it using binary file den wht would hav been da solution????
and what is the diff. b/w binary and text file?????
OriginalGriff 29-Jan-12 4:59am    
A Text file is human readable: so it might look like this:
1, 1.00794, Hydrogen, H
2, 4.002602, Helium, He
...
The same info in a binary file could look like anything - it will depend on how it is stored - but will likely not be understandable by normal humans at all. For example, the atomic number could be stored as a single byte with a hex value between 1 and 76, followed by a four byte value representing the floating point atomic weight. Then the element name could be stored compacted (since there are only 27 relevant characters in an element name (A-Z plus a terminator) each character would fit nicely into 5 bits). It depends on who created it, and for what purpose.
Or it could be stored as XML, or an SQLCE database, or...
How you would have read it from a binary file, would depend on how you wrote it! :laugh:
Espen Harlinn 29-Jan-12 13:36pm    
5'ed! - it would definitely work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900