Hello.. This is my first time posting here, and I'm hoping I can find some help

I have missed about a week and some of my programming class, and I am very behind. I haven't been able to figure these out on my own.

I need to write a practice program that prompts the user to enter an Element Name, the Symbol and the Atomic Mass, in a procedure. It also needs to offer the list sorted by Element Name, and Atomic Mass.

Basically, I need to create/ overwrite a file, a readln to enter the Element Name/Atomic Mass/Symbol, and have the option to display by Element Name, and by Atomic Mass.

I know that I need to use a basic array, and I know how to create a text file and overwrite it, but after my absence, this is overwhelming.

Thank you in advanced

Recommended Answers

All 3 Replies

Send some code ,please.
I can help you.

Hi,

I think the best approach is to create a record with those elements you have to read and then declare an array of that type.

type 
Element =record 
//Element, Atomic mass etc
end;

var 
Elements : array[1..100] of Element; 
anElement : Element;

In your main program, you assign your file and open it for writting ( rewrite(f)) and create a loop to read a new element for your array:

i :=0;
repeat 
       i := i + 1 ;
       Writeln('Element Type'); readln(anElement.ElementType);
       Writeln('Symbol'); readln(anElement.Symbol);
       Writeln('AtomicMass'); readln(anElement.AtomicMass);
       Elements[i] := anElement;
       Writeln("Do you want to read a new element? [Y/N]"); readln(aChar);
until (aChar ='N');

Sort(Elements);
//display results
...

Your sort procedure can implement any sorting algorithm, but pay attention at comparison:

if (Elements[i].ElementType > Elements[i +1 ].ElementType) or ((Elements[i].ElementType = Elements[i +1 ].ElementType) and 
(Elements[i].Atomic > Elements[i +1 ].AtomicMass))
//do the change

Hope this will help you.

Cheers,
Ionut

that was a good practice.... :D

program atomicmass_v1;{by FlamingClaw}

uses crt;

{create 2 new types}
type newRecord = record  {first is a record}
                   name:string;
                   symbol:string;
                 end;

      {second is an array,it has 100 elements}
      newArray = array[0..99]of newRecord;


{global variable section}
var Elements:newArray;{this one is an array,that can contains only records}
    OneElement:newRecord;{one record}
    f:file of newRecord;{this file can store only records}
    num1:integer;
    ok,{array filled}
    open,{file opened}
    saved:boolean;{array saved to a file or not}


{1}procedure Line;Forward;
{2}procedure OpenForWrite(var op:boolean);Forward;
{3}procedure OpenForRead(op:boolean);Forward;
{4}procedure CloseTheFile;Forward;
{5}procedure DataIn(var x:newArray;var num:integer;var af:boolean);Forward;
{6}procedure ShortArray(var x:newArray;num:integer;af:boolean);Forward;
{7}procedure MainMenu;Forward;
{8}procedure SaveToFile(x:newArray;af:boolean;num:integer;var asa:boolean);Forward;
{9}procedure ReadBack(asa:boolean);Forward;

{1,write a '=' char to the screen to separate the records}
procedure Line;
var i:byte;
begin
     for i:=1 to 40 do write('=');
     writeln;
end;

{2,opens a file for writting,if the file exists
then deletes its contents}
procedure OpenForWrite(var op:boolean);
begin
     assign(f,'c:\elements.dat');
     rewrite(f);
     writeln('File is created.');
     op:=true;
end;

{3,opens an existing file for reading}
procedure OpenForRead(op:boolean);
begin
     if op then begin
        assign(f,'c:\elements.dat');
        reset(f);
        writeln('File is opened.');
     end else writeln('Write The Array Into A File.');
end;

{4,closes the opened file}
procedure CloseTheFile;
begin
     close(f);
     writeln('File is closed.');
end;

{5,store the user's data in the array,
you can exit if you press the 'y'
}
procedure DataIn(var x:newArray;var num:integer;var af:boolean);
var indexofarray:integer;
    character:char;
begin
     character:=#0;
     indexofarray:= -1;
     repeat
           inc(indexofarray,1);
           with x[indexofarray] do begin
               Line;
               writeln(indexofarray,'. Record Storing Into Array.');
               write('Element''s name: ');
               readln(name);
               write('Element''s symbol: ');
               readln(symbol);
           end;
           Line;
           writeln('Continue? y = yes, n = no');
           readln(character);
           character:=upcase(character);
     until (character = 'N') or (indexofarray > 99);
     num:=indexofarray;
     af:=true;
     writeln('Array Filled.');
     readln;
end;

{6,this procedure shorts the array's elements by its name}
procedure ShortArray(var x:newArray;num:integer;af:boolean);
var a,b,c:integer;
    one:newRecord;  {this is a temp var}
begin
     if af then begin
     for a:=0 to num-1 do begin
          for b:=a+1 to num do begin
              if (x[b].name < x[a].name) then begin
                 one:=x[a];
                 x[a]:=x[b];
                 x[b]:=one;
              end;{if}
          end;{for}
     end;{for}
     clrscr;
     writeln('Shorted results are: ');
     c:=0;
     while c <= num do begin
           Line;
           writeln(c,'.Record.');
           write('Name:    ',x[c].name,'    Symbol: ',x[c].symbol);
           inc(c);
           writeln;
           Line;
     end;
     writeln('Array Sorted.');
     end else writeln('First Fill The Array.');
     readln;
end;

{7,this is the main menu}
procedure MainMenu;
var choice:byte;
begin
     repeat
           clrscr;
           writeln('1 Fill The Array.');
           writeln('2 Short The Array.');
           writeln('3 Save The Array Into A File.');
           writeln('4 Read The Array From A File.');
           writeln('5 Exit.');
           write('Select One Number.Your choice is: ');
           readln(choice);
           case choice of
                1:DataIn(Elements,num1,ok);
                2:ShortArray(Elements,num1,ok);
                3:SaveToFile(Elements,ok,num1,saved);
                4:ReadBack(saved);
           end;
     until choice = 5;
end;
{8,saves the array into a file}
procedure SaveToFile(x:newArray;af:boolean;num:integer;var asa:boolean);
var i:integer;
begin
     if af then begin
        writeln;
        OpenForWrite(open);
        for i:=0 to num do begin
            write(f,x[i]);
        end;
        writeln('Array Saved.');
        CloseTheFile;
        asa:=true;
     end else writeln('First Fill The Array.');
     readln;
end;
{9,read back the data from a file and
write the results to the screen}
procedure ReadBack(asa:boolean);
var i:integer;
begin
     if asa then begin
        writeln;
        OpenForRead(open);
        seek(f,0);
        i:=0;
        while not eof(f) do begin
              read(f,OneElement);
              Line;
              writeln(i,'.Record.');
              writeln('Name: ',OneElement.name,'  Symbol: ',OneElement.symbol);
              inc(i);
              Line;
        end;
        CloseTheFile;
     end else writeln('First Save The Array To A File.');
     readln;
end;
{here begins the main program}
begin
     clrscr;
     num1:=99;
     ok:=false;
     open:=false;
     MainMenu;
     readln;
end.
{created by FlamingClaw 2009}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.