100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

introduction to programming

Rating
-
Sold
-
Pages
35
Grade
A+
Uploaded on
10-09-2022
Written in
2022/2023

introduction to programming












Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
September 10, 2022
Number of pages
35
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Content preview

Solutions


Introduction to Programming II
COS1512

solution to Assignment 2

, 2



Content
1. Introduction .................................................................................................................................... 2
2. Tutorial matter distributed to date ................................................................................................... 2
3. Allocation of marks ......................................................................................................................... 3
4. Solution to Assignment .................................................................................................................. 2


1. Introduction




2. Tutorial matter distributed to date
DISK 2017 Prescribed software
First tutorial letter: General information, study programme, exam admission
and assignments
Study Guide
How to create an assignment as a PDF file
Solution to Assignment 1
This letter: Solution to Assignment 2

, 3 COS1512/202/1/207



3. Allocation of marks
When we mark assignments, we comment on your answers. Many students make the same mistakes and
consequently we discuss general problems in the tutorial letters. It is, therefore, important to work through
the tutorial letters and to make sure you understand our solutions and where you went wrong.

The maximum number of marks you could obtain for Assignment 2 is 80. This is converted to a
percentage. If you for instance obtained 40 marks for Assignment 2, you received 50% for Assignment
2. This percentage in turn contributes a weight of 80% to the year mark, as can be seen in the
summary of the weights allocated to the assignments for COS1512 below.

Assignment Weight
number
1 20
2 80
3 0

Questions 3, 4 and 6 have not been marked. However, 5 marks are awarded if you attempted questions 3,
4 and 6 (note that this is not the way in which questions will be marked in the examination!). We include
complete solutions for all questions.

The marks you received for question 1 were determined on the following basis:
Question not done 0/5
Question attempted, but the program does not work at all 1/5
A good attempt, but there are a few problems with your answer 3/5
The program works correctly and produces the correct output 5/5

The marks you received for question 2 were determined on the following basis:
Question not done 0/30
Each correct response to questions (a,b,c,d,e,g,h,j,k,m,n and q) = 2 marks 24/30
Each correct response to questions (f,i,l,o,p and r) = 1 mark 6/30


The marks you received for questions 5 and 7 was determined on the following basis:
Question not done 0/15
Question attempted, but the program does not work at all 5/15
A good attempt, but there are a few problems with your answer 10/15
The program works correctly and produces the correct output 15/15


In other words, you can obtain a maximum of 5 marks for questions 1, 3, 4 and 6; a maximum of 30 marks
for question 2; and a maximum of 15 marks for questions 5 and 7.

Note that not all mistakes are corrected – but we will provide informative comments.

If you did not include the program output for questions 1, it means there are “a few problems with your
answer” and the maximum you can get is then 3/5. If you did not include the program output for questions 5
and 7, it means there are “a few problems with your answer” and the maximum you can get is then 10/15.

We did not award any marks to assignments submitted more than four weeks after the due date. However,
we still provided informative comments.

, 4



4. Solution to Assignment
Question 1 [max of 5 marks]

Discussion:
For this question, you had to convert the struct Student into a class. There is essentially only a slight
difference between a struct and a class. A class is the same as a struct (i.e. a struct may also
contain both member variables and member functions just like a class, even though the examples in
Savitch do not show that) except that, by default, all members are inaccessible to the general user of the
class. This means that all members of a struct are by default public, and all members of a class are
by default private. Therefore, we have to specify that the member functions are public. (As an exercise,
omit the public keyword from the class and recompile it.) While there are situations where a struct is
more suitable, as a rule, you should use classes rather than structs. In general, it is preferable to use
classes, as classes offer better protection.

An object encapsulates or combines data and operations on that data into a single unit. In C++, the
mechanism that allows you to combine data and the operations on that data in a single unit is called a
class.

As name, quiz1, quiz2, midtermExam and finalExam are private member variables (see page
549 of Savitch 6th edition/ page 581 of Savitch 7th edition/ pages 573-582 of Savitch, 8th edition/pages
589-595 of Savitch, 9th edition) of class Student, they cannot be accessed directly in the main() function.
As a result, public member functions getQuiz1(), getQuiz2(), getMidtermExam(),
getFinalExam() and Term() are used to access these member variables in order to determine their
values. These functions are known as accessor functions, while setName(), setQuiz1(), setQuiz2(),
setMidtermExam()and setFinalExam()are mutator functions. (Study pages 553-554 of Savitch, 6th
edition/ pages 585-586 of Savitch 7th edition/ pages 581-582 of Savitch, 8th edition/ pages 597-598 of
Savitch, 9th edition).

Mutator functions are used to change or modify member variables of an object. The parameter of the
mutator function typically indicates the value according to which the member variable should be changed.
For example, the mutator function setQuiz1()below modifies member variable quiz1 to q1:
void Student::setQuiz1(int q1)
{
quiz1 = q1;
}

Note the prototypes for member functions:
string getName()const;
int getQuiz1()const;
int getQuiz2()const;
int getMidtermExam()const;
int getFinalExam()const;

These member function are accessors - hence the const keyword at the end of the function definition.
Note that a member function that does not have the const keyword at the end of it could mutate (change
or modify) the state of the object. Although member function calcAverage() is not an accessor, it should
not modify the object, and therefore the prototype for member function calcAverage() also has the
const keyword at the end of the function definition:
int calcAverage()const;

Program listing:
#include <iostream>
R145,00
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
theotakalani

Get to know the seller

Seller avatar
theotakalani University of South Africa (Unisa)
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 year
Number of followers
0
Documents
2
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions