Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

https://www.cs.uky.edu/~kwjoiner/cs215/labs/lab9.pdf IN C++! Learning Objective:

ID: 3703421 • Letter: H

Question

https://www.cs.uky.edu/~kwjoiner/cs215/labs/lab9.pdf

IN C++!

Learning Objective: - Coding and unit testing of a simple class with a partial-array data member. - Submitting multiple files in Canvas Specifications: Get and use the main() program provided on the course website. This will be used to test your class. You can start your lab in Visual Studio, then - add new item to source files called lab9.cpp - Copy/paste from the text file on the website. - Add your comment box at the top Add two new source files to your VS Project: student.h and student.cpp Use these to code the interface and implementation of the following class. Create a class called student. It should have the following data members: - Name of student - A Partial Array of scores [maximum of 10 scores] - Initialize the name to empty string, and the number of scores to 0 in the Constructor. The class should have the following methods: - setName: given a new name, sets the data member to the new name given. - setScores: given an array of integers and number of scores, if the number of scores given is lower than 0 or greater than the maximum number, prints: student::setScores(): invalid numScores given! Otherwise, it copies the given array into the scores data member, and sets the number of scores to the given value. - addScore: given a new score, adds the score to the list by copying it into the next available slot, and incrementing number of scores. When the array is full, prints: student::addScores(): MAX SCORES exceeded! - getName: simply returns the data member. - getAvg: when the number of scores is 0, returns 0.0; otherwise, returns the average of the scores in the list. - getScores: given an array of integers (which is already pass-by-reference), fill the array with the current contents of the score data member and return the current number of scores data member. - print: on one line, prints the name, average (by invoking getAvg()), number of scores, and scores as shown in the example below. Submission: Using MS-Windows Explorer: - be sure to SAVE ALL before submitting. - find the folder where your .h and .cpp files are saved - highlight one of the files, the press ctrl and highlight the others o .h and .cpp files ONLY - Right-click on one of the highlighted files and select send to / compressed (zip) file - Rename .zip file created to lab9.zip - Submit the .zip file

Explanation / Answer

//Student.h

#pragma once

#include<iostream>

#include<string>

//define max size of partial array

#define MAX 10

using namespace std;

class Student

{

string name;

int num_score;

int scores[MAX];

public:

//default constructor

Student();

void setName(string str);

void setScore(int arr[],int n);

void addScore(int n);

string getName() const;

void getScore(int arr[],int &n);

float getAvg();

};

-----------------------------------------

//Student.cpp

#include"Student.h"

Student::Student()

{

num_score = 0;

name = "";

}

void Student::setName(string str)

{

name = str;

}

void Student::setScore(int arr[],int n)

{

if (n <= 0 || n >= MAX) //if number of score n is less than or equal to 0 or greater than 10 ,,print error message

{

cout << "invalid numScores given!" << endl;

return;

}

//else copy entire array to scores array

for (int i = 0; i < n; i++)

{

scores[i] = arr[i];

num_score++;

}

}

void Student::addScore(int score)

{

if (num_score >= MAX)

{

cout << "MAX SCORES exceeded!" << endl;

return;

}

scores[num_score++] = score;

}

string Student::getName() const

{

return name;

}

void Student::getScore(int arr[],int &n)

{

//copy scores array into arr and num_score to n

for (int i = 0; i < num_score; i++)

{

arr[i] = scores[i];

}

n = num_score;

}

float Student::getAvg()

{

float sum = 0, avg;

for (int i = 0; i < num_score; i++)

{

sum += scores[i];

}

avg = sum / num_score;

return avg;

}

-----------------------------------------

//main.cpp

#include"Student.h"

#include<time.h>

using namespace std;

int main()

{

//declare an object of type Student

Student st;

int n;

//array for scores

int arr[MAX];

//assign random score between 30-100

srand(time(NULL)); //intialize rand generation

n = rand() % (MAX - 1) + 1; //generate random number of score

for (int i = 0; i < n; i++)

{

arr[i] = rand() % (100 - 30) + 30; //random number between 30 to 100 for each score

}

//test setScore function,setName,addScore

st.setName("Smith Gary");

st.setScore(arr, n);

st.addScore(60);

//get avg

cout << "Average score : " << st.getAvg() << endl;

//get scores into arr2

int arr2[MAX];

int n2;

st.getScore(arr2, n2);

//display scores array after calling method getScore

cout << "Score elements are: ";

for (int i = 0; i < n2; i++)

{

cout << arr2[i] << " ";

}

cout << endl;

//try adding more scores till we hit error condition

st.addScore(90);

st.addScore(59);

st.addScore(69);

st.addScore(89);

st.addScore(79);

//print scores before avarage

cout << "Score elements are: ";

for (int i = 0; i < n2; i++)

{

cout << arr2[i] << " ";

}

cout << "Updated avarage: " << st.getAvg() << endl;

//now check invalid condition when trying to add invalid num_scores

int arr3[MAX + 1];

int n3 = 11;

Student st2;

for (int i = 0; i < n3; i++)

arr3[i] = rand() % (100 - 30) + 30;

st.setScore(arr3, n3);

st2.addScore(50);

st2.getScore(arr3, n3);

//display scores array after calling method getScore

cout << "Score elements are: ";

for (int i = 0; i < n3; i++)

{

cout << arr3[i] << " ";

}

}

/*output

Average score : 68.4286

Score elements are: 83 53 30 98 71 84 60

MAX SCORES exceeded!

MAX SCORES exceeded!

Updated avarage: 69.7

invalid numScores given!

Score elements are: 50

//output2

Average score : 67.4

Score elements are: 81 73 79 55 43 76 47 88 72 60

MAX SCORES exceeded!

MAX SCORES exceeded!

MAX SCORES exceeded!

MAX SCORES exceeded!

MAX SCORES exceeded!

Score elements are: 81 73 79 55 43 76 47 88 72 60 Updated avarage: 67.4

invalid numScores given!

Score elements are: 50

*/