C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 22nd, 2013, 11:05 AM
Seramet005 Seramet005 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 Seramet005 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
Arrow Memory & arrays - Molecular Weight Search and Sort

This is my assignment due 2/25/2013.
The Chemistry Dept. has asked you to develop a program that will calculate molecular weights of compounds given the chemical formula. For example H2O (water) would contain 2 hydrogen atoms weighting 1.008 and 1 oxygen weighting 15.999. The weight of water is 2 x 1.008 + 1 x 15.999 yielding 18.015 atomic weight.

The input to you program will be in the following form:
element ( number of atoms) element (number of atoms) ...
for example H2O would be represented H(2)O. If the ()'s are not present, assume one (1) atom. Another example acetic acid would be represented CH(3)COOH or C(2)H(4)O(2).

If an element has two letters symbol representation ie Silver is Ag, the second letter will be lower case indicating it is part of the symbol representation.

A list of all the chemical elements can be found in the data file 'Element.dat'. There is one input elements per line where the element name appears first followed by its atomic weight.
Ex. Al 26.98
Sb 121.75
S 32.06
Ba 137.34
...

A second input file 'Formula.dat' contains the test formulae to us in testing your program. There will be one formula per line. For the output, print out the formula and its Molecular weight in a nice table form (ie line up the columns).


Restrictions: You are to use an array of structure to hold the Symbol and its weight.
Use a Binary search to look up elements in the element table.
You are to use Functions/Procedures in your implementation.
Format your output in a table form (ie headings and straight columns)

I figured out how to sort the element symbols and their atomic weights, but I don't know how use the Binary Search with the Formulas to produce the Molecular weight for each formula. Here's what I've written so far:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

struct Element
{
string Name;
float Weight;
};

void SortElements(Element AR[]);
void BinarySearch();
void ShowTable();
const int SIZE = 30;
ifstream inputFile1;
ifstream inputFile2;
ofstream outputFile;

int main()
{
Element ABRS[SIZE];
inputFile1.open("Elements.txt");
inputFile2.open("Formula.txt");
outputFile.open("Molecular Weight.txt");

if(!inputFile1)
{
cout << "Sorry dude but the Elements.txt file didn't open." << endl;
return 0;
}
if(!inputFile2)
{
cout << "Sorry dude but the Formula.txt file didn't open." << endl;
return 0;
}

cout << "\t\t MOLECULAR WEIGHT CHART \t\t\n" << endl;
cout << "\n ELEMENT |" << " ATOM.WT |" << " FORMULA " << endl;
cout << "______________________________\n";
outputFile << "\t\t MOLECULAR WEIGHT CHART \t\t\n" << endl;
outputFile << "\n ELEMENT |" << " ATOM.WT |" << " FORMULA " << endl;
outputFile << "______________________________\n";

SortElements(ABRS);
StringMake();
BinarySearch();
ShowTable();

inputFile1.close();
inputFile2.close();
outputFile.close();

return 0;
}

void SortElements(Element AR[])
{
bool swap;
Element temp;

for(int s = 0; s < SIZE; s++)
{
inputFile1 >> AR[s].Name;
inputFile1 >> AR[s].Weight;
}
do
{
swap = false;
for(int i = 0; i < (SIZE - 1); i++)
{
if (AR[i].Name > AR[i + 1].Name)
{
temp.Name = AR[i].Name;
temp.Weight = AR[i].Weight;
AR[i].Name = AR[i + 1].Name;
AR[i].Weight = AR[i + 1].Weight;
AR[i + 1].Name = temp.Name;
AR[i + 1].Weight = temp.Weight;
swap = true;
}
}
}while(swap);

}

void StringMake()
{
string NEL;
char FR;
inputFile2 >> FR;
while(inputFile2.get(FR))
{
if (FR == '\n')
{
break;
}
else if (islower(FR))
{
NEL = NEL + FR;
}
if (isupper(FR))
{
NEL = NEL + FR;
}
if (isdigit(FR))
{
NEL = NEL + FR;
}
if (ispunct(FR))
{
NEL = NEL + FR;
}
}
}
// Here's where I need help.
void BinarySearch()
{
int first = 0;
int last = SIZE - 1;
int middle ;
int pos = -1;
bool found = false;
float weight = 0;

while (!)
}

void ShowTable()
{
for(int x = 0; x < SIZE; x++)
{
inputFile2 >> A[x].Name;
inputFile1 >> A[x].Weight;
cout << " " << A[x].Name << "\t" << A[x].Weight << "\n";
outputFile << " " << A[x].Name << "\t" << A[x].Weight << "\n";
}
}

Again the deadline for this is 2/25/2013 so any help is appreciated. I'll also listen any correction made to the program.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Molecular Weight Search and Sort


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap