Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP2113 Programming Technologies / ENGG1340 Computer Programming II

Assignment 2

Deadline: 16 November (Thursday) 23:59

If you have any questions, please post to the Moodle discussion forum on Assignment 2.

Total marks: 100 marks

General Instructions

Read the instructions in this document carefully.

In this assignment you will solve 3 tasks and a tester would automatically test your submitted program. So if your submitted files and program outputs do not conform to our instructions given here, your programs cannot be evaluated and you will risk losing marks totally.

Sample test cases are provided with each task in this document. Note that the test cases mayor may not cover all boundary cases for the problem. It is also part of the assessment whether you are able to design proper test cases to verify the correctness of your program. We will also use additional test cases when marking your assignment submission.

Input and output format

Your  C++  programs  should  read  from  the standard  input.  Also,  your  answer  should  be  printed  through the standard output. If you failed to follow this guide, the tester may not able to give a score for your program. Additionally, you should strictly follow the sample output format (including space, line breaker, etc.), otherwise, your answer might be considered as wrong.


How to use the sample test cases for problems 1 and 3?

Sample testcases in text file formats are made available for you to check against your work. Here’show you may use the sample test cases. Take problem  1 test case 2 as an example. The sample input and the expected output are given in the files input1_2.txt and output1_2.txt, respectively. Suppose that your program is named " 1", do the followings at the command prompt of the terminal to check if there is any difference between your output and the expected output.

./1 < input1_2.txt > myoutput.txt

diff myoutput.txt output1 2.txt

Testing against the sample test cases is important to avoid making formatting mistakes. The additional test cases for grading your work will be of the same formats as the sample test cases.

Coding environment

You must make sure that your C++ program can compile, execute and generate the required outputs on our standard environment, namely, the gcc C++11 environment we have on the CS Linux servers (academy*). Make sure the following compilation command is used to compile your programs:

g++ -pedantic-errors -std=c++11 [program name].cpp -o [object code name]

The name after the “-o” option is the name of the object code. In the above example, you can run the program using the following command:

./[object_code_name]

As a programmer/developer, you should always ensure that your code can work perfectly as expected on a target (e.g., your client’s) environment, not only on yours.

While you may develop your work on your own environment, you should always try your program (compile & execute & check results) on our standard environment before submission.

Submission

Name your C++ program files (and Makefile for Problem 3) as the following table shows and put them together into one directory. Make sure that the folder contains only these source files (*.cpp / *h / Makefile) and no other files. Compress this directory as [uid].zip file where [uid] is your university number and check carefully that the correct file have been submitted. We suggest you to download your submitted file from Moodle, extract them, and check for correctness. You will risk receiving 0 marks for this assignment if you submit incorrect files. Resubmission after the deadline is not allowed.

Filename

Description

1.cpp


Problem 1

searchword.cpp


Problem 2

quadratic.cpp, main3.cpp, Makefile


Problem 3

Late submission


If you submit within 3 days after the deadline, there will be 50% mark deduction. After that, no mark will be given.

Evaluation

Your code will be auto-graded for technical correctness. In principle, we use testcases to benchmark your solution, and you may get zero marks for not being able to pass any of the test cases. Normally partial credits will not be  given for incomplete solution, as in many cases the logic of the programs are not complete and an objective  assessment could be difficult. However, your work may still be considered on a case-by-case basis during the  rebuttal stage.

Academic dishonesty

We will be checking your code against other submissions in the class and from the Internet for logical redundancy. Please be reminded that no matter whether it is providing your work to others, assisting others to copy, or copying others will all be considered as committing plagiarism and we will follow the departmental policy to handle such cases. Please refer to the course information notes for details.

Getting help

You are not alone! If you find yourself stuck on something, post your questions to the course forum, send us emails or come to our support sessions. We want this assignment to be rewarding and instructional, not frustrating and demoralizing. But we don’t know when or how to help unless you ask.

Discussion forum

Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments. However, you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any questions about this assignment, you should post them in the discussion forums.

Problem 1 [30%]:

A playing card consists of a suit (“Heart”, “Diamond”, “Club” or “Spade”) and a number value (1 to 13). For  simplicity, let’suse characters ‘H’, ‘D’, ‘C’ and ‘S’ to represent suits “Heart”, “Diamond”, “Club” and “Spade” respectively. So, a playing card is represented by one character combined with a number such as “H2”, “C6” and “D12” . Two cards are said to be a pair when they have the same number value. Please write a C++ program to  read an integer N and then N playing cards afterwards. The program then outputs 1) the number of pairs among  the N playing cards, and 2) the sum of the values of theN playing cards.

For example, the sets  {“H3”, “D3”} and {“H3”, “D3”, “C3”} contain 1 pair each and their sum of values are 6 and 9 respectively. The sets {“H3”, “D3”, “C3”, “ S3”} and  {“H3”, “D3”, “C6”, “S6”} contain 2 pairs each and their sum of values are 12 and 18 respectively. You can assume that the input playing cards are always valid and the N playing cards are distinct (which means N must be smaller than or equal to 52).

Sample Test Cases

User inputs are shown in blue.

1_1:

8 H5 D6 C5 S6 C13 H10 D5 C6

2

56

1_2:

9 H2 H1 D2 D1 C2 C1 S2 S5 D5

4

21

1_3:

10 D6 D9 H9 C9 S12 S6 H6 C6 H12 S9

5

84

Problem 2 [35%]:

Write a C++ program that searches a word from multiple files, and output the number of lines that the word

appears and the total number of occurrences of the word in each file.

You are provided with a header file searchword.h and a main program main2.cpp. Please complete the implementation file searchword.cpp.

Input:

.     The main program accepts command line argument as input. The command line of the program is

given by: ./    E.g., if you compile your program using g++ -pedantic-errors -std=c++11

searchword.cpp main2.cpp -o main2, then the following command at the command prompt ./main2 abc t1.txt t2.txt t3.txt

will ask your object program main2 to search for the string abc in the

files t1.txt, t2.txt, t3.txt.

.     Command    line    arguments    in    C/C++    are    implemented    by    having    input    parameters     for the main() function:

int main(int argc, char* argv[])

.     Read  the  comments  in  the  provided template to  see the usage of the paramaters argc (an integer) and argv (an array of C-strings).

.     The main2.cpp program has been completed for you, which means that command line argument parsing has been done for you. But you should read the code and understand how it works, as you may need to deal with command line arguments in your later courses too.

Output:

.     A line for each file specified in the command line arguments: first the filename, then the number of lines the word appears in the file (n1), followed by the number of total occurrences of the word in the file (n2): :

or if there is any error accessing the file:

: error opening file

Requirements:

.     You       should       start       working      on searchword.cpp and       complete       the       function int SearchWord(string word, string fileName, int &nLines, int &total) which search for a word in a file named fileName, and stores the number of lines that word appears in the file in nLines and  the number of total occurrences in total. The function returns 0 if file operation is successful and 1 if otherwise.

.     The matching of word is case-insensitive.

Note:

.     The header file searchword.h and the main program main2.cpp have been completed for you, and you do not need to make any change to them. Read the code in them and understand what they do before you start working.

.     We may use another main2.cpp to test your program.

Sample Test Cases

Sample text files t1.txt, t2.txt, t3.txt, t4.txt are provided.

We show only the contentsof t1.txt, t2.txt, t3.txthere:

t1.txt

t2.txt

t3.txt

Cab AbB CAB Ab CaB ABc aBb AB cAb caB aBcAB CAB ABcAb AbcAb aBCAB AB ABcAb aBCaB ABCAB Abb

aBC abC AbC ABCAB abCab ABC

aBCAB ABcAB AB cAb

ab abcab Ab abc aBCAB AbCAb abC AbC ab abcab aBc aBc ab abcAB Abc aBC ab abC ab aB AbCAB AbcaB Ab ab ABc aBcab abC ABc AbCab

ABcAb

app THe Elf DeEd keep SHe DEED dEEd aPP Ab sheeP Cde THe sHe ElF CDE sHe KeEP aB eLF An CaT sHE hE KEeP he SHE dEED ab she

Commands entered by the users at the command line prompt “>” to run your program are shown in blue.

Note that since there is no user input from the standard input, here's how you should test your output against

the sample output (e.g., for test case 2_1) to check for correctness:

./main2 abc t1.txt t2.txt t3.txt > myoutput.txt

diff myoutput.txt output2_1.txt

2_1:

./main2 abc t1.txt t2.txt t3.txt

t1.txt: 2 5  t2.txt: 4 11  t3.txt: 0 0

2_2:

./main2 ab t1.txt t2.txt t3.txt

t1.txt: 3 4  t2.txt: 4 9  t3.txt: 3 3

2_3:

./main2 he t3.txt t4.txt t5.txt

t3.txt: 1 2  t4.txt: 3 4  t5.txt: error opening file

Problem 3 [35%]:

In this question, let us implement a simple quadratic equation solver. Given a quadratic equation ax2  + bx + c  = 0, the discriminant is defined as ∆ =  b2  − 4ac. If ∆ > 0, the equation has 2 real roots. If ∆ = 0, the equation has 1 real root. If ∆ < 0, the equation has no real root. The roots are given by the quadratic formulax = .

To allow other programstoreuse our program, let us put the code in a separate file.

Let us first define the header file quadratic.h as follows:

// quadratic.h  #ifndef QUADRATIC H  #define QUADRATIC H  double discriminant(double a, double b, double c);  int numRoots(double a, double b, double c);  double root1(double a, double b, double c);  double root2(double a, double b, double c);  #endif

The interfaces of the functions are explained below:

discriminant:

Parameter 1: The coefficient of x2  (that is a)

Parameter 2: The coefficient of x (that is b)

Parameter 3: The constant term (that is c)

Return value: A double value that represents the discriminant b2  − 4ac

numRoots:

Parameter 1: The coefficient of x2  (that is a)

Parameter 2: The coefficient of x (that is b)

Parameter 3: The constant term (that is c)

Return value: An integer value that represents the number of roots (0, 1 or 2)

root1:

Parameter 1: The coefficient of x2  (that is a)

Parameter 2: The coefficient of x (that is b)

Parameter 3: The constant term (that is c)

Return value: A double value that represents the first root

root2:

Parameter 1: The coefficient of x2  (that is a)

Parameter 2: The coefficient of x (that is b)

Parameter 3: The constant term (that is c)

Return value: A double value that represents the second root

Note that if there is only 1 root, root1() and root2() return the same value.

The skeleton of the implementation file quadratic.cpp is given below:

// quadratic.cpp

#include

#include

#include "quadratic.h"using namespace std;

double discriminant(double a, double b, double c) {

// to be implemented

}

int numRoots(double a, double b, double c) {

// to be implemented

}

double root1(double a, double b, double c) {

// to be implemented

}

double root2(double a, double b, double c) {

// to be implemented

}

Requirements

Task 1: Please complete the implementation of the 4 functions in quadratic.cpp.

Task 2: Please implement a main program main3.cpp which accepts 3 double values (a, band c in the quadratic equation). The program then prints the discriminant, the number of roots and the root(s) on screen. All double values should be printed with 3 significant figures. You may use setprecision(n) defined in the header  to set the precision parameter of the output stream.

You  must  call  the   functions   discriminant(),  numRoots(),  root1() and  root2() defined  in “quadratic.cpp” in your main program.

Please compile your programs using the command g++ -pedantic-errors -std=c++11 main3.cpp quadratic.cpp -o main3. Then run the main program using the command  ./main3 to see whether it behaves as expected.

Task 3: Please create a Makefile to generate the following targets:

quadratic.o: which depends on quadratic.cpp and quadratic.h

main3.o: which depends on main3.cpp and quadratic.h

main3: which depends on quadratic.o and main3.o

clean: for cleaning main3, main3.o and quadratic.o

You should assume that there can be a file named “clean” in the folder and you need to make sure that running make clean will not cause any conflict.

Upon Makefile is  created  appropriately,  you  should   be  able  to  compile  all   programs   by  issuing  the command make main3 and cleaning all object files by issuing the command make clean.

Sample Test Cases

User inputs are shown in blue.

3 1:

_

6

-17

12

Discriminant: 1

Number of roots: 2

The roots are 1.5 and 1.33

3 2:

_

1

4

4

Discriminant: 0

Number of roots: 1

The root is -2

3 3:

_

1

-11

28

Discriminant: 9

Number of roots: 2

The roots are 7 and 4

3_4:

6

-1

-5

Discriminant: 121

Number of roots: 2

The roots are 1 and -0.833

3_5:

3

4

4

Discriminant: -32

Number of roots: 0

No real root