Part B - Foundations

Dynamic Memory

Workshop 3

In this workshop, you are to process an array of objects of compound type where the user specifies the number of elements in the array at run-time. 

Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities

  • to allocate and deallocate dynamic memory for an array of compound types
  • to use standard library functions to accept input data
  • to use standard library functions to format output data

Molecules - All Groups

Molecule Class

Code a compound type named Molecule that holds information about a single molecule.  The information consists of

  • a C-style string of no more than 20 characters, which holds the symbol for the molecule
  • a C-style string of no more than 20 characters, which holds the description of the molecule
  • a floating-point variable, which holds the molecular weight

Your compound type includes the following member functions:

  • void set(const char*, const char*, double) - a modifier that stores valid data in an instance of the compound type.  The first parameter receives the address of a C-style string that holds the symbolic structure.  The second parameter receives the address of a C-style string that holds the name of the molecule.  The third parameter receives the molecular weight.  Valid data consists of strings that do not exceed 20 characters in length and a molecular weight that is positive-valued.
  • void display() const - a query that displays molecular information on standard output as follows: the symbol left-justified in a field width of 20, a single space, the description left-justified in a field width of 20, a single space, the molecular weight right-justified in a field width of 7 with 3 decimal places.  If the object is empty, your function displays the message no data available.

Place your definition of the compound type in a header file named Molecule.h and your function definitions in an implementation file named Molecule.cpp

Client Module

Complete the following implementation file for the w3 main module.  The missing regions of code are highlighted.  In your solution (group x) you may assume that the user inputs the data correctly:

 // Workshop 3 - Molecules
 // w3.cpp

 #include <iostream>
 using namespace std;
 #include "Molecule.h"

 int main() {
     int n;
     // allocate static memory here

     cout << "Molecular Information\n";
     cout << "=====================" << endl;

     cout << "Number of Molecules : ";
     cin >> n;
     // allocate dynamic memory here

     for (int i = 0; i < n; i++) {
         char symbol[21];
         char description[21];
         double weight;

         // ... add code to accept user input for a Molecule

         molecule[i].set(symbol, description, weight);
     cout << endl;

     cout << "Structure            Name                     Mass\n";
     cout << "==================================================" << endl; 

     for (int i = 0; i < n; i++) {
         cout << endl;

     // deallocate dynamic memory here

Your completed application, when used with your Molecule type, produces output that looks something like: 

 Molecular Information
 Number of Molecules : 3
 Enter structure : H2O
 Enter full name : Water
 Enter weight    : 18.015
 Enter structure : CO2
 Enter full name : Carbon Dioxide
 Enter weight    : 44.010
 Enter structure : NaCl
 Enter full name : Sodium Chloride
 Enter weight    : 58.443

 Structure            Name                     Mass 
 H2O                  Water                  18.015
 CO2                  Carbon Dioxide         44.010
 NaCl                 Sodium Chloride        58.443

Note the sections of your code that required special attention.  Post advice to the forum letting other students know where they should take care.

You can find some data for amino-acids here and a molecular weight calculator here.

Robust Validation - Group Y

Design and code the Molecule type described above.  Complete the w3.cpp implementation file.  Upgrade this file to include robust validation of the input data provided by the user.



Create a typescript of your complete solution using the following commands: 

 + At the prompt, type: script w3.txt
 + At the prompt, type: whoami
 + At the prompt, type: cat w3.cpp Molecule.h Molecule.cpp 
 + At the prompt, type: g++ -o w3 w3.cpp Molecule.cpp
 + At the prompt, type: w3
 + At the input prompt, enter test data
 + At the prompt type: exit

These commands will produce a file named w3.txt.  Download this file to your local computer. 


Upload your typescript file to Moodle: 

  • Login to Moodle
  • Select your course code
  • Select W3 under Assignments and Tasks
  • Upload w3.txt
  • Write a short note to your instructor
    • press "Edit"
    • enter your conclusions about this workshop in the notes textbox
    • press "Save Changes"
  • When ready to submit, press "Send for Marking"

Other Submissions

Submit your typescript file as your instructor has specified.

  Designed by Chris Szalwinski   Copying From This Site   
Creative Commons License