Uploaded by Lindelani Tshikalange

COS1512 Assignment 2 2021

advertisement
lOMoARcPSD|9014637
Assignment 2 2021 Questions 1 and 2
Introduction to programming II (University of South Africa)
StuDocu is not sponsored or endorsed by any college or university
Downloaded by Lindelani Tshikalange (lindelanitshikalange0@gmail.com)
lOMoARcPSD|9014637
Assignment 2 2021
UNIQUE NUMBER:
777008
DUE DATE:
24 May 2021
TUTORIAL MATTER:
Chapters 4,5, 6, 8 and 9 of the Study Guide
Chapters 4 (section 4.6), 5 (section 5.5), 6, 8 and 9
(excluding the optional parts of section 9.2) of
Savitch
WEIGHT:
30%
MARKS:
70
Question 1
(5)
Conventionally it was thought that 1 year in a dog's age equals 7 years in human age.
A fairly new concept in aging studies claims that chemical modifications to a person's
DNA over a lifetime essentially serve as an "epigenetic clock." This tracks an
individual's "biological age". The “epigenetic clock” also applies to dogs.
The most precise method to convert a dog’s age to human years uses the empirical
equation that the researchers in aging studies discovered, which is 16 x ln(dog’s age)
+31 = human age, (that is the natural logarithm of the dog’s real age, multiplied by 16,
with 31 added to the total.)
Write two overloaded functions to calculate a dog’s age in human years, one using the
conventional method (simply multiplying the dog’s age in years by 7) and the other
using the empirical equation proposed by the researchers in aging studies (16 x
ln(dog’s age) + 31 = human age).
Hint: The natural logarithm can be calculated using the C++ library function log
available in the <cmath> library. See Appendix 4 in Savitch for more on the log library
function.
Test your program for both versions of calcDogsAge()and show your output as part
of your answer.
Question 2
(5)
Write a C++ program to validate whether a user keys in a valid birth date. Using the
assert() function, validate that the day, month and year entered, fall within the valid
ranges. Take care of leap years. Display to the user how old he will be this year, and
whether he has been born in a leap year or not. Test you program with the following
input, and submit the output for all three dates:
29 2 1958
Downloaded by Lindelani Tshikalange (lindelanitshikalange0@gmail.com)
lOMoARcPSD|9014637
9 3 1958
20 10 2004
NB: Note that you are expected to use the assert macro in this question.
Question 3
(10)
Write a program that will calculate a student’s year marks for all his subjects. The
program must read the subject code, the assignment marks for two assignments and
the percentage that each assignment contributes towards the year mark, for each
subject that the student is registered for.
Create an input file called assignments.dat that contains the following information
for a specific student:
COS1511
COS1512
COS1521
COS1501
INF1501
INF1511
30
25
10
50
40
20
66
76
58
62
82
24
70
75
90
50
60
80
49
67
62
57
78
55
The first field in each line represents the subject code, followed by the percentage that
assignment 1 contributes towards the year mark and the student’s mark (a
percentage) for assignment 1. Then follow the percentage that assignment 2
contributes towards the year mark and the student’s mark for assignment 2. You
should not count the number of entries in the file to determine how many times the
file must be read.
Your program must read the file line by line, calculate the student’s year mark for the
subject as a percentage and write the subject code and the year mark to an output
file yearmark.dat. If your program works correctly, the output file will look as follows:
COS1511
COS1512
COS1521
COS1501
INF1501
INF1511
54.10%
69.25%
61.60%
59.50%
79.60%
48.80%
NB: First plan your program on paper (using your computational thinking to do so).
You have to submit your plan for your program as well as the actual program
code, input and output. Planning your program can take the form of a flowchart,
pseudocode, or notes to guide you in the development of the program.
Downloaded by Lindelani Tshikalange (lindelanitshikalange0@gmail.com)
lOMoARcPSD|9014637
Question 4
(10)
You have to write a program to read an input file character by character to help
Peter solve the following activity in his activity book. The paragraph below is given:
We h2pe that 32u e5723ed the acti4it3. A6ter 32u ha4e c2mpleted
the acti4it3, 0e5d 32ur
re0ult
t2:
The
Acti4it3
C2mpetiti25, Bett3 Da4i0 0treet 99, Auckla5d Park, 989, a5d 0ta5d
a cha5ce t2 wi5 a hamper c250i0ti51 26 c2l2uri51 a5d acti4it3
b22k0, c2l2uri51 pe5cil0 a5d pe50.
Create an input file activity.dat with the paragraph above. The numbers 0 to 7
have to be replaced as follows:
0
1
2
3
4
5
6
7
must be replaced by s
must be replaced by g
must be replaced by o
must be replaced by y
must be replaced by v
must be replaced by n
must be replaced by f
must be replaced by j
Ask the user to input the names of the input and output files. Read the input file
character by character, and write the character (if it stays the same) to the output
file, or write the changed character to the output file. Call your output file
competition.txt.
NB: First plan your program on paper (using your computational thinking to do so).
You have to submit your plan for your program as well as the actual program
code, input and output. Planning your program can take the form of a flowchart,
pseudocode, or notes to guide you in the development of the program.
Question 5
(5)
Write a program that reads a person’s name in the following format: first name, then
middle name or initial, and then last name. The program then outputs the name in the
following format:
last name, first name. middle initial.
For example the input
Mary Average User
should produce the output
User, Mary A.
Downloaded by Lindelani Tshikalange (lindelanitshikalange0@gmail.com)
lOMoARcPSD|9014637
Your program should work the same and place a full stop after the middle initial even
if the input did not contain a full stop. Your program should allow for users who give
no middle name or initial. In that case, the output of courses contains no middle name
or initial. For example, the input
Mary User
should produce the output
User, Mary
Your program should also accept names in lowercase, uppercase or a mix of
lowercase and uppercase, and display that in the correct format, e.g. if the input is
mArY average USER
should produce the output
User, Mary A.
Use C-strings and assume that each name is at most 20 characters long.
Hint: it may be easier to use 3 C-strings.
Remember to plan your program!
Question 6
(5)
Given the following header:
vector<string> split(string target, string delimiter);
implement the function split() so that it returns a vector of the strings in target
that are separated by the string delimiter. For example,
split("do,re,me,fa,so,la,ti,do", ",") should return a vector with the
strings "do", "re", "me", "fa", "so", "la", "ti" and "do".
Test your function split() in a driver program that displays the strings in the vector
after the target has been split.
“Without ambition one starts nothing. Without work one finishes nothing. The prize will not be sent
to you. You have to win it.” Socrates
©
Unisa 2021
Downloaded by Lindelani Tshikalange (lindelanitshikalange0@gmail.com)
Download