Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

The University of the South Pacific

School of Computing, information and Mathematical


Sciences

CS111: Introduction to Computing

Test 2 Semester 1, 2019

Time Allowed: 50 minutes

Total Marks: 40 (20% of final grade)

Name: _______________________

Student ID: _______________________

Campus: _______________________

The test is composed of one section only with an overall test score of 40 points. You will have 50
minutes to complete the test.

All questions in the test are mandatory. All answers should be written using a black or blue ball
point pen.

NOTE:
This test covers one learning outcome:
1. Apply programming constructs to computing problems

Page 1
1. You have been given the task of creating a program that will help your lecturer decide
whether is at risk of failing the course. The goal of the program is to classify students into
two categories; At Risk Student or Non At Risk Student. The classification will be purely
based on the marks the students attain in their Test 1. Students with marks less than 55
are classed as At Risk Students. Otherwise, they are classified as Non At Risk Students.
The test is marked out of 100.

a. What are the input (s) to your program and what are the output (s) (2 marks)

Input: student test mark

Output: classification (at risk or not at risk)

b. Draw a flowchart that will demonstrate the flow of this program (4 marks)

Start

Ask for mark and


store in variable

No Yes
If marks
< 55

Classification is Classification is
non AtRisk AtRisk

End

Page 2
c. As this is a class of approximately 400 students, create an array called “stuMarks”
that will be used to hold all the student marks. Assign an initial mark of 0 to all the
students. You should declare appropriate variables and constants as needed (4
marks)

cons int CAPACITY = 400;

double stuMarks[CAPACITY] = {0};

d. The student marks will be fed into your program using console input by your
lecturer. Since you are populating this array through console input, what would be a
good looping structure to use in order to populate your array? Explain your answer.
(2 Marks)

While loop or do while loop

Reason-> because it is indefinite how many inputs the user will be


providing.

Page 3
e. Write code that will use the looping structure you chose in (d) above to populate
your “stuMarks” array with student marks. You are expected to declare appropriate
variables and constants necessary for you to complete this task. You do not need to
re-declare your array, you can use the variables declared from question (d).
Validation of students’ marks is necessary. (6 marks)

Option 1
---------------------------------------------

int size = 0;

cout<< “enter student marks press q to quit”<< endl;


while (cin >> stuMarks[size]){
cout<< “enter student marks press q to quit”<< endl;
size++;

---------------------------------------------
Option 2
---------------------------------------------

int size = 0;
double marks;

do{

cout<< “enter student marks press -1 to quit”<< endl;


cin >> marks ;
while (cin.fail()){

cin.clear();
string notNum;
cin >> notNum;

cout<< “enter student marks again”<< endl;


cin >> marks;
}
stuMarks[size] = marks;
size++;

} while (marks != -1);

Page 4
f. The problem with the program described above is that it is very laborious for the
lecturer to enter the student marks through console input. You are now supposed to
adapt your program to take input from a file called stuData.txt instead of console
input. Write code that will open the file and load all the data into your array. A
snapshot of the stuData file is given below. (6 marks)

ifstream infile;
infile.open(“stuData.txt”);

if (!infile){

cout<< “file not found”;


return 1;
}

int size = 0;

while (! infile.eof()){

infile >> stuMarks[size];


size ++;
}

infile.close();

Page 5
g. Write a pseudocode for a function that is supposed to take your “stuMarks” array
and find the number of students that are classified as At RiskStudents. The
function should return the number of At Risk Students. (4 marks)

Function number at risk


Define number of at risk students and initialize to 0

For each mark in stuMarks


If mark is less than 55
Increment number of at risk students
Otherwise
Do nothing

Move to next student

Return the number of at risk students

h. Implement the function that you have described above using C++ code. (8 marks)

int numAtRisk( double marks[] , int size){

const int MARKLIMIT = 55;

int atRisk = 0;

for (int i=0; i < size, i ++){

if( marks[i] < MARKLIMIT){


atRisk++;
}
}

return atRisk;
}

Page 6
i. Write code that will call your function that you have created in (g) and print the
number of students that are classified as At Risk. (4 marks)

int atRisk = numAtRisk(stuMarks, size);

cout << “number of at risk students : ” << atRisk <<endl;

End of Exam 

Page 7
//extra sheet

Page 8

You might also like