ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a...

17
CSE1PES: PROGRAMMING FOR ENGINEERS AND SCIENTISTS ASSIGNMENT 2 2020 (10%) Assignment developed by Matthew Felicetti 2020 TOOLS Unix server through Putty for compiling and testing Notepad ++ for writing solutions LMS for submission SUBMISSION The task requires ONE submission: The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.) o There is NO late submissions for this assignment. o Requests for extensions, need to be sent by email: Doctors certificates need to cover a period greater than 3 days, and not submitted less than 3 days before the assignment is due. Special consideration will be rejected due to the assignment being worth less than 15%. o Filenames must be in the format <student_num>_assign2.c DUE DATE AND TIME 23 RD of May 2020, 11PM There is NO late submissions for this assignment. No assignment will be accepted after the due date, without a legitimate extension granted by the subject coordinator before the due date. ACADEMIC INTEGRITY Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. For individual assignments, plagiarism includes the case where two or more students work collaboratively on the assignment. The School of Engineering and Mathematics treats plagiarism very seriously. When it is detected, penalties are strictly imposed. http://www.latrobe.edu.au/students/academic-integrity

Transcript of ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a...

Page 1: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

CSE1PES: PROGRAMMING FOR ENGINEERS AND SCIENTISTS

ASSIGNMENT 2 2020 (10%)

Assignment developed by Matthew Felicetti 2020

TOOLS

• Unix server through Putty for compiling and testing

• Notepad ++ for writing solutions

• LMS for submission

SUBMISSION

The task requires ONE submission:

• The code for assignment 2

o Submitted as a .c file in the normal assignment submission portal.

o (Do not submit the executable.) o There is NO late submissions for this assignment.

o Requests for extensions, need to be sent by email:

▪ Doctors certificates need to cover a period greater than 3 days, and not submitted less

than 3 days before the assignment is due.

▪ Special consideration will be rejected due to the assignment being worth less than

15%.

o Filenames must be in the format

▪ <student_num>_assign2.c

DUE DATE AND TIME 23RD of May 2020, 11PM

There is NO late submissions for this assignment. No assignment will be accepted after the due date,

without a legitimate extension granted by the subject coordinator before the due date.

ACADEMIC INTEGRITY Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the

work is your own. For individual assignments, plagiarism includes the case where two or more students

work collaboratively on the assignment. The School of Engineering and Mathematics treats plagiarism

very seriously. When it is detected, penalties are strictly imposed.

http://www.latrobe.edu.au/students/academic-integrity

Page 2: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PRELIMINARY TASK – DOWNLOAD THE CODE PACKAGE

On Unix, create a directory named 'assignment_2' if you have not done so already (see lab0)

Change into this directory

Copy the files using the command below:

cp ~csilib/cse1pes/students/assign2/<student_number>* .

replace <student_number> with your student number

What is cp?

cp is the copy command in Unix

The first argument is the source and the second the destination

cp source destination

Why is the destination a .

If you put a . as the second argument, it will take the name of the file/s you are copying

Make sure you have your version.

Even though they look like others, they are NOT the same.

Every student’s template contains various unique signatures.

Using someone else's version or code from another version

will result in a mark of 0

Page 3: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

BACKGROUND

BACKGROUND - TASK

You are tasked with writing one c program related to chemical compounds and elements. In the task, given

a set of compounds, two reactants and two products, and you will determine if the reactants and products

are balanced.

BACKGROUND – CHEMICAL ELEMENTS

A chemical element is a type of atom that makes up all the ordinary matter in the universe, and is defined

by the number of protons in its nucleus. Currently there are 118 known elements.

These 118 can be represented on the periodic table of chemical elements as shown below:

BACKGROUND – ATOMIC NUMBERS AND ELEMENT SYMBOLS

The elements are all given names but are often represented as a series of letters (element symbol),

starting with an Upper-case letter and following with zero, one or two lower case letters. These can be

seen on the periodic table as shown below.

Periodic Table

Source: LeVanHan

Creative Commons

Element Name

Element Symbol

Beryllium

Source: LeVanHan

Creative Commons

Page 4: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

BACKGROUND – CHEMICAL COMPOUNDS

A chemical compound or molecule is collection of atoms of more than one chemical element held together

by chemical bonds. A common example of this is pure water, which is made up of two Hydrogen atoms and

one Oxygen atom that are chemically bound.

BACKGROUND – CHEMICAL FORMULA (MOLECULAR FORMULA)

A chemical formula lists the elements and the proportion of atoms per element using element symbols.

More specifically the chemical formula written as a molecular formula includes the exact number of atoms

per element using element symbols. This is done by listing the symbols and a subscript to indicate the

number of atoms for a particular atom (with no subscript indicating one).

Example:

𝐻2𝑂

Looking at our periodic table we can see the symbol H stands for Hydrogen, and O for oxygen. The subscript

2, indicates that there are two Hydrogen atoms and one Oxygen atom.

BACKGROUND – CHEMICAL REACTION

A chemical reaction is a process where one or more compounds/elements known as the reactants

are converted into one or more different compounds/elements known as the products. The

reactants and products will have an equal amount of atoms of the same elements.

Example

When iron and sulphur combine they form iron sulphide

This can be represented using the following form (chemical equation)

𝐹𝑒 + 𝑆 → 𝐹𝑒𝑆

The compounds on the left-hand side of the arrow are the reactants and the

products on the right. A + on the left-hand side can be thought of as “plus”, where as

a + on the right-hand side can be thought of as “and”.

For our program we will accept two reactants and two products.

Model of a Pure Water Molecule

Public Domain

Page 5: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

BACKGROUND – BALANCED CHEMICAL EQUATION

Given the following chemical equation

𝐶3𝐻8 + 𝑂2 → 𝐻2𝑂 + 𝐶𝑂2

On the left hand side we can see there are

3 carbon (C) atoms

8 hydrogen (H) atoms

2 oxygen (O) atoms

On the right hand side we can see there are

1 carbon (C) atom

2 hydrogen (H) atoms

3 Oxygen (O) atoms

As these are not equal the chemical equation is NOT balanced.

To balance these we apply coefficients to the compounds. These coefficients multiply the number of atoms in the

compounds.

Hence a balanced version of the above equation is

𝐶3𝐻8 + 5𝑂2 → 4𝐻2𝑂 + 3𝐶𝑂2

On the left hand side we can see there are

3 carbon (C) atoms

8 hydrogen (H) atoms

10 oxygen (O) atoms (5x2 =10)

On the right hand side we can see there are

3 carbon (C) atom

8 hydrogen (H) atoms (4x2 = 8)

10 Oxygen (O) atoms (4x1 + 3x2 = 10)

Page 6: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

BACKGROUND – INPUT

As we cannot use subscript, the number of atoms will be always to the right of the element

symbol.

Example:

4𝐻2𝑂 will be written as 4H2O

2𝐵𝐵𝑒4𝐶𝑎𝐻2 will be written as 2BBe4CaH2

Errors:

It is expected that all molecular formulas will be correctly formatted but may not be balanced.

BACKGROUND - LIMITATIONS AND NOTES

Error checking – User input

The data entered by the user will be in the correct format of a chemical equation

Number of compounds – User input

Any of the products or reactants may be an empty String.

User input

The user input must accept any compound, i.e. do not hard code the compounds.

Compounds and Elements

An element will not appear twice in any single compound but may appear in both products and/or

both reactants.

The number of atoms for each element in any single compound will be between 1 and 9, but the

total combined for the reactants and/or products may be higher

Elements will be limited to those with 1 or 2 character element symbols

CONSTRAINTS

• Formatting needs to be as specified. (Your program will be tested against automated test cases.)

• Text to user needs to be easily understandable.

• The program must print your student number as specified.

• Types should be used and named scientifically and appropriately.

• You must use comments to explain significant lines of code.

• The code must be done using ANSI C and compile on latcs5.cs.latrobe.edu.au using gcc.

• You must not use break, goto, or global variables in the assignment.

• You are only allowed to include the stdio.h, stdlib.h and string.h header file. No others. Do not

use conio.h.

• Checks must be commented out - NOT REMOVED - as they will be used in marking.

• Do not change function prototypes

Page 7: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

TASK

STOP: READ THE BACKGROUND INFORMATION FIRST

PART 1

Given that the function numeric_char_to_decimal_int has been written for you.

Add a comment above the function (not the prototype) numeric_char_to_decimal_int

> Explain the parameters and what is returned

> Explain what the function is for basically

> Explain how the function works using proper terminology covered in the subject

Given that the function get_coefficient has been written for you.

The purpose of this function is that, given a compound (see background), it will find the coefficient if there is one, "remove" it

from the string, and return the value, else it will return 1.

Add a comment above the function (not the prototype) get_coefficient

> Explain the parameters and what is returned

> Explain how the function checks if there is a coefficient

> Explain how the function "removes" the coefficient (is it really removed?)

> Explain why the double pointer is necessary (why would this not work with a single pointer?)

PART 2

Given the empty function and function prototype, get_compound

Complete this empty function so that it behaves as follows:

This function should ask the user for the compound, indicating the number n (see example below).

Then should store the string response from the user.

Once the function is written, check the code, using the checks provided in main.

An example is shown below:

Comment out the checks when done checking

Page 8: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PART 3

Given the empty function and function prototype, remove_chars_from_str

Complete this empty function so that it behaves as follows:

This function takes as input a number, num. It then moves the characters in the string “to the left” by that number of

characters, so as to shorten the string by num characters.

Write the code that moves characters in the string, as to shorten the string by num characters

Example:

Given the string:

'H' 'e' 'l' 'l' 'o' '\0' (null terminator)

And we want to remove the first two characters, the string becomes

'l' 'l' 'o' '\0' (null terminator)

'o' '\0' (null terminator)

Notice that we don't move what is after the null terminator? You should understand why.

1. For loop that counts from 0, to the length of the string minus num (inclusive), incrementing by 1

2. str at index i becomes str at index i+num

Test this function yourself, by uncommenting the test in main

Comment these lines once you have finished testing this

Page 9: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PART 4

Uncomment the next section of main, as shown below:

If you run the program now, it will likely crash at run time.

We want to store the two reactant strings together in a single string, and the two product strings together in a single string

(concatenated).

To do this we need to first allocate memory for storing these.

We have the length of each string (reactant_1_length, reactant_2_length and product_1_length, product_2_length)

We require enough space to for the two strings concatenated plus the null terminator.

Edit these two lines to dynamically allocate memory for the reactants and products efficiently.

The below is given to get you started, you need to fill in the blank spot.

Once complete, test

Comment the checks out when complete. (Be aware, just because the checks work does not mean it is correct)

Page 10: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PRE - PART 5

In the main function, uncomment the balance_check function call

PART 5

In this part we will look at getting each element and number of atoms from the reactants, to confirm we can read

them correctly in the balance_check function.

You are given some variables:

balance – indicates if the reaction is balanced (1 for true, 0 for false)

number_of_atoms - indicates the number of atoms of the element we are currently looking at

current_element – the current element (remember this could be one or two characters, also the null terminator)

Note: current_element currently has the first character from the reactants string

We iterate from 1 to the length of the string reactants + 1.

In this for loop, you need to write an if-elseif-else statement as follows:

if reactants[i] is a lower case letter

then the second character in current_element becomes this letter

else if reactants[i] is a numeric character

then the numeric character is converted into a decimal integer using the appropriate function, and the

number_of_atoms is set to this value

else

CHECK this is correct by printing out the current_element and the number of atoms

Then set number_of_atoms back to 1

And current_element[0] to reactants[i] and the second character to 0 (null terminator)

An example of the output is provided:

Comment the checks out when complete.

Page 11: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PRE - PART 6

In the balance_check function, you should now have a for loop with an if-elseif-else

Update the else as shown below:

PART 6

Part 6: Useful information

Given the function get_total_reactant_atoms_current_element you are going to find the total number of reactant

atoms for the current element.

If the current element is part of the first reactant, it is possible that it can appear twice in the

reactants. To check we will use strstr function from the string library. strstr returns NULL if it

cannot find a match.

We can pass a shortened string to a function by passing a pointer to a location in the string:

Example:

char str[] = "HelloHello";

char* loc = strstr(&(str[3]),"ell");

This will search starting from the fourth index.

Page 12: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

Part 6: Example

You have the following information:

The coefficients

The reactant_1_length

The reactants

The current_element

The number_of_atoms

The iteration variable i

Say we have below:

coefficients 2 4 2 4

reactant_1_length 4

reactants 'C' 'H' '4' 'O' 'C' '2' 'H' '\0'

current_element 'H' '\0' '\0'

number of atoms 4

i 3

1. We first check if "H" is part of the second half of the reactants, using i and reactant_1_length.

We can do this by checking if i is greater than reactant_1_length.

2. It is not. So we now check if "H" appears in the second half of the reactants, we do this by using strstr and

passing the address of reactants[reactant_1_length] and the current_element. We store the pointer

return into a variable named loc_ptr

3. loc is not NULL, so now we check the number of elements of the second "H", we have a pointer to the

H(loc)

'C' 'H' '4' 'O' 'C' '2' 'H' '\0'

Given the pointer and we can get the length of the current element, we can see what is after the "H",

using loc[strlen(current_element)]. This gives us '\0', this is not a numeric character so the number of

atoms is 1 for the second H

4. So we now have the number of atoms of the first "H" and second "H", 4 and 1, and the coefficients, so the

total number of atoms for the current element is 2 * 4 + 4 * 1 = 12.

Page 13: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

Part 6: Implementation

1. Check if the current_element is part of the second reactant using the variables i and reactant_1_length

2. The current_element is part of the second reactant, hence the total_reactant_atoms_current_element is the

second coefficient multiplied by the number_of_atoms

3. The current_element is part of the first reactant, so now we need to check if it is in the second reactant.

We create a pointer to a char, named loc_ptr. To store the result of using strstr and passing the address of

reactants[reactant_1_length] and the current_element.

4. We check if loc_ptr is not NULL, to test if the current_element also appears in the second reactant

5. Store the string length of the current_element into a variable named length_element_2 using the strlen function

6. We then check if the current_element in the second reactant is followed by a numeric character, using loc_ptr

and length_element_2 (see above example)

7. You now can calculate the total_reactant_atoms_current_element, as you have the coefficients of the two

reactants, and the number of atoms of both elements. Use the background information to calculate.

Also increase the length_element_2 by 1, as it includes the numeric character.

8. You now can calculate the total_reactant_atoms_current_element, as you have the coefficients of the two

reactants, and the number of atoms of both elements. Use the background information to calculate.

9. Now call your function you developed in PART 3, remove_chars_from_str, and pass loc_ptr and

length_element_2

10. The current_element is only in the first reactant, you can calculate the

total_reactant_atoms_current_element, as you have the coefficients of the first reactants, and the number of

atoms. Use the background information to calculate.

Page 14: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

Part 6: Testing

Test as much as you can with as many examples as you can, I would recommend testing AT LEAST 10

times before continuing. What do you expect vs what actually is produced?

3 examples are given to get you started.

5*2 = 10

5+4*2 = 13

Page 15: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

PRE - PART 7

In the balance_check function, update the else as follows:

In the main function, uncomment the final section

PART 7

Do not start this part until everything else works. There are intentionally less steps given. This is to test if you can solve a given problem using the tools provided by yourself, which is essential for employment/research in IT, CS and Engineering. If you cannot get it to completely work, show what you can do, and make sure that at a minimum it compiles. We will be providing less help with this part to ensure that you can apply and be marked on your independent knowledge and problem solving skills.

Part 7: Implementation

1. You will need to find the location of the current element using strstr

2. You will need to find if the element is followed by a numeric character, if it is then the number of

atoms is that value else it is 1

3. You will need to determine if the current_element is part of the first or second product.

You need to use pointer arithmetic to solve this, using the location and products pointers, and

product_1_length to compare to

4. You will need to find the element in the second product, and then find its number of atoms

5. You will need to calculate the total product atoms for the current element using the background

information

Page 16: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

Part 7: Testing

Test as much as you can with as many examples as you can, I would recommend testing AT LEAST 20

times. 4 examples are given to get you started.

Page 17: ASSIGNMENT 2 2020 (10%) - files.transtutors.com · • The code for assignment 2 o Submitted as a .c file in the normal assignment submission portal. o (Do not submit the executable.)

FINAL PRODUCT

Remove the checks from Part 7.

Now you have a full program that checks the balance of a chemical equation

RUBRIC

Use break or global variables minus 20% of final mark

Use goto minus 60% of final mark

Use conio.h or other headers minus 80% of final mark

Part Criteria Marks

Part 1 Comment - numeric_char_to_decimal_int 3

Part 1 Comment - get_coefficient 4

Part 2 Implementation - get_compound 2

Part 3 Implementation - for loop 3

Part 3 Implementation - pointer 2

Part 4 Implementation - malloc 4

Part 5 Implementation - general 2

Part 5 Implementation - lower case 3

Part 5 Implementation - numeric 3

Part 6 Implementation - S1 2

Part 6 Implementation - S2 1

Part 6 Implementation - S3 3

Part 6 Implementation - S4 1

Part 6 Implementation - S5 1

Part 6 Implementation - S6 3

Part 6 Implementation - S7 1

Part 6 Implementation - S8 1

Part 6 Implementation - S9 2

Part 6 Implementation - S10 1

Part 7 Implementation - S1 3

Part 7 Implementation - S2 3

Part 7 Implementation - S3 6

Part 7 Implementation - S4 3

Part 7 Implementation - S5 3

General Indentation 8

General Types 2

General Variable Names 5

General Safe 5