Exercise 2 Building an application using your Student ADT (20 pts) In Assignment
ID: 3667510 • Letter: E
Question
Exercise 2 Building an application using your Student ADT (20 pts)
In Assignment 4, Exercise 5, part 3, you were asked to write an algorithm that uses the Student ADT.
We’ve added 5 new tasks to the list, but since your Student ADT is working perfectly, the extra work we added will not take too much time.
Here’s what your application program should do:
create an array of 10 students student references with initial values received from the console
2. print out each student to the console, one at a time
3. calculate the class average and print out your result
4. (new) Make one change to any student’s assignment, and one change to any student’s midterm, and one change to any student’s final exam.
5. (new) display the student records that changed.
6. (new) Recompute the class average, and display the result.
7. (new) Give every student 5 more marks on Assignment 5.
8. (new) Recompute the class average, and display the result.
Hints:
• You’ll notice a recalculation of a class average. Maybe your app needs a function to calculate the class average for any size class.
• The array for 10 students should be an array of references! We saw an example of this in the lecture notes! Yes, go look.
We will not use arrays of pointers in CMPT after this assignment, because we will start using lists.
But arrays of pointers are considered an important tool for some kinds of programming tasks. You may use the heap or the stack to store these references.
• Do not try to name your array "class" because "class" is a keyword in C++. It would be like trying to name an array "while" or "if".
• In this exercise, you will start by converting your pseudo-code from A4E5 part 3 to C++. You may also start from the model solution provided in the solutions to A4. What to hand in:
• Hand in your C++ file studentApp.cc containing code to perform the above steps.
• A text-document called a5e2output.txt that gives the input and output of your program working and demonstrating the correct behaviour of the application as above.
Grading • 5 marks. Your file studentApp.cc #includes Student.h and does not define any Student operation. 5 marks will be deducted for the slightest violation of this criterion.
• 5 marks. Your file studentApp.cc makes no attempt to access the fields of the Student record type directly, and uses only the Student ADT interface to work with the student data. 5 marks will be deducted for the slightest violation of this criterion.
• 8 marks. Your application does every one of the 8 steps listed above, using the Student ADT. • 2 marks.
Your output file a5e2output.txt shows your program working properly.
STUDENT ADT
assignment 4 Exercise 5
************************************************************************
Student
refToChar firstName
refToChar lastName
Integer studentNumber
refToChar NSID
refToFloat assignments
Float midterm
Float final
end Student
Algorithm createStudentRecord()
Pre: none
Post: student record created
Return: reference to newly created student record
refToStudent s <- allocate new Student
return s
---
Algorithm destroyStudentRecord(s)
Pre: s :: refToStudent
Post: Student record s and fields deallocated
Return: nothing
if s != null
then
deallocate s->firstName
deallocate s-> lastName
deallocate s-> NSID
deallocate s-> assignments
deallocate s
end
---
Algorithm displayStudentRecord(s)
Pre: s :: refToStudent
Post: fields in s displayed to console
Return: nothing
if s != NULL
then
print "Name: " s->firstName s->lastName
print "Student Number: " s->studentNumber
print "NSID: " s->NSID
print "Assignments: "
for i from 0 to 10 by 1 do
print i s->assignments[i]
end
print "Midterm: " s->midterm
print "Final: " s->final
end
---
Algorithm copyString(s)
This is a helper function, to ensure that we never make a
mistake copying a C-string. It's not part of the Student ADT Interface
Pre: s is a reference to a CString
Post: allocates new memory for a copy of s
Return: reference to the new copy
refToChar copy <- allocate new Char[strlen(s) + 1]
strcpy(copy, s)
return copy
---
Algorithm readStudentRecordFromConsole()
Pre: nothing
Post: creates new student record and populates fields from console
Return: refToStudent -- newly created student record
Char firstName[100]
Char lastName[100]
Char NSID[100]
Float midterm
Float final
refToStudent newStudent <- createStudentRecord() // 2016/02/09 error correction
if newStudent != NULL
then
read firstName
newStudent->firstName <- copyString(firstName)
read lastName
newStudent->lastName <- copyString(lastName)
read newStudent->studentNumber
read NSID
newStudent->NSID <- copyString(NSID)
newStudent->assignments <- allocate new Integer[10]
for i from 0 to 10 by 1 do
read newStudent->assignments[i]
end
read newStudent->midterm
read newStudent->final
done
return newStudent
---
Algorithm ChangeAssignmentGradeForStudent(s, a, g)
Pre: s :: refToStudent
a :: Integer -- assignment number
g :: Integer -- amount to add to grade
Post: adds g marks to assignment number a for student s
Return: nothing
if s != NULL
then
s->assignments[a] <- s->assignments[a] + g
done
---
Algorithm ChangeExamGradeForStudent(s, x, e)
Pre: s :: refToStudent
x :: Char -- 'M' if midterm, 'F' if final
e :: Integer -- amount to add to exam grade
Post: adds e marks to exam (Midterm or Final) for student s
Return: nothing
if s != NULL
then
if x == 'M'
then
s->midterm <- s->midterm + e
else if x == 'F'
then
s->final <- s->final + e
else
done
done
---
Algorithm calculateAverageGrade(s)
Pre: s :: refToStudent
Post: calculates student average final grade
Return: Float -- final average grade for student
if s != NULL
then
Float midterm <- s->midterm * 0.25
Float final <- s->final * 0.45
Float assignmentTotal <- 0
for i from 0 to 10 by 1 do
assignmentTotal <- assignmentTotal + s->assignments[i]
end
assignmentTotal <- assignmentTotal / 10
assignmentTotal <- assignmentTotal * 0.30
return midterm + final + assignmentTotal
done
---
Algorithm main()
Pre: nothing
Post: create 10 students, get info from console, print students, calculate class average
Return: nothing
refToRefToStudent students <- allocate new refToStudent[10]
Float classTotal <- 0
Float classAverage <- 0
for i from 0 to 10 by 1 do
students[i] <- readStudentRecordFromConsole()
done
for i from 0 to 10 by 1 do
displayStudentRecord(students[i])
done
for i from 0 to 10 by 1 do
classTotal <- calculateAverageGrade(students[i])
done
classAverage <- classTotal / 10
print "The class average is " classAverage
************************************************************************
Explanation / Answer
#include <iostream>
int main()
{
using namespace std;
unsigned short x = 65535; // largest 16-bit unsigned value possible
cout << "x was: " << x << endl;
x = x + 1; // 65536 is out of our range -- we get overflow because x can't hold 17 bits
cout << "x is now: " << x << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.