C++, where Programming Meets Chemistry.

  • Context: Comp Sci 
  • Thread starter Thread starter asz304
  • Start date Start date
  • Tags Tags
    Chemistry Programming
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment related to calculating the molar mass of organic molecules using C++. Participants are exploring how to effectively utilize arrays and control structures like switch statements in their code, while also addressing the conversion of input strings into numerical values representing molecular weights.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about how to use arrays and switch statements in their code for the assignment.
  • Another participant suggests that using arrays is not mandatory and encourages finding a solution that suits the individual’s understanding.
  • A participant mentions that their lab instructor indicated arrays should be used, indicating a reliance on recent learning.
  • Concerns are raised about the need to understand how to convert a string of characters into values for calculating molecular weights before writing code.
  • There is a discussion about the correct way to define character values for elements, with one participant pointing out that using string literals for characters is incorrect.
  • Participants discuss the need for pseudocode or pre-coding to clarify the algorithm before coding, emphasizing the importance of understanding the problem.
  • One participant describes their thought process for calculating molar mass by multiplying element weights by their respective counts in a compound.
  • Another participant emphasizes the need for the algorithm to correctly identify and count the number of each type of atom in the input string.
  • There is a suggestion to use loops and functions to find the counts of elements in the input string.
  • One participant shares a revised code snippet but expresses uncertainty about the correctness of their loop structure.

Areas of Agreement / Disagreement

Participants generally agree on the importance of understanding the algorithm and the need for pseudocode before coding. However, there are differing views on the necessity and implementation of arrays, as well as the correct approach to defining and using variables for molecular weights.

Contextual Notes

Some participants note that the initial definitions of element weights as characters are incorrect, and there is confusion about the use of arrays and the structure of loops in the provided code. The discussion reflects a mix of understanding and uncertainty regarding programming concepts and chemistry principles.

Who May Find This Useful

This discussion may be useful for students learning C++ programming, particularly those working on assignments that involve chemistry concepts and require the manipulation of strings and arrays.

  • #31
I appreciate your great effort for helping me in my last assignment, I compiled everything and they worked fine except for the example "C12". Thanks again
 
Physics news on Phys.org
  • #32
Check what atoi() does.

Calling it chemistry is completely off, especially the idea of treating upper case and lower case interchangeably. Co and CO are two completely different things. I know it is not your fault; it doesn't make the assignment any better.
 
  • #33
I passed my assignment online, I was almost done but decided to ignore inputs such as C12. But my atoi works fine for the other inputs.
 
  • #34
Suppose you're at index 3 of a string and find that the character C is there. I.e., str[3] == 'C'. If the characters '1' and '2' follow 'C', you can get them with this expression: atoi(&str[4]).

&str[4] is the address of the character at index 4 of the string. atoi would take any numeric characters starting at index 4, and convert them to an int that it returns.
 
  • #35
Shameless ad plug: if you want to see (reasonably) well designed and well written program that does real analysis of chemical formulas, go to my site (www.chembuddy.com), download EBAS, install it (risk free trial), start, click on "%" icon on the toolbar and enter any formula.

Note that the list of elements is editable - you can add moieties like "Acetate" so that your formulas are more readable (far easier to understand NaAcetate than NaC2H3O2). The real elements (those present in periodic table) are protected against edition. But it is not because they are treated differently internally, it required additional coding to prevent them from being modified.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K