Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 28-Jan-12 22:14pm
Comments
SAKryukov at 29-Jan-12 3:39am
   
What does it mean "open an element"? And what's the problem?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Amir Mahfoozi at 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.
SAKryukov at 29-Jan-12 3:52am
   
Exactly, and this access in one CPU instruction.
Thank you, Amir.
--SA
Espen Harlinn at 29-Jan-12 13:35pm
   
Nice and simple, my 5 :)
SAKryukov at 29-Jan-12 13:37pm
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 Wink | ;)
 
Hope it helps.
  Permalink  
Comments
SAKryukov at 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 at 29-Jan-12 13:34pm
   
5'ed!
Amir Mahfoozi at 29-Jan-12 23:35pm
   
Thanks Espen.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
SAKryukov at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 235
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 29 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100