You are on page 1of 2

CC2-1-Fundamentals of Programming/CC2-Computer Programming 1 Module 16: Vector Type

Activity
CC2-1-Fundamentals of Programming/ CC2-Computer Programming 1
Module 16 – Vector Type

Background
The lesson discussed vector data type. Vector solve some limitations of using array as data structure,
since the size of vector data type can grow and shrink depending on the number of elements you put
into it. Vector implementation in C++ was implemented as class, therefore we can use various methods
of vector class to manipulate vectors.

Task

Now your task is, to think of one simple programming problem that you think is solvable using vector.
State the problem as comment on the top of your source code, and the algorithm you use to solve the
problem.

Sample Output

Say, we want to calculate the average of grades the students got for their projects. The number of
scores may vary from student to student since some students might not able to submit their project.

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void getStudentData(vector<string> &name, vector<vector<float>> &scr);


void printStudentData(vector<string> &names, vector<vector<float>> &scr);

int main() {

vector<string> student;
vector<vector<float>> scores;

string quit = "no";

while (quit != "yes") {

getStudentData(student, scores);

cout << "Quit? ";


cin >> quit;
cout << "\n";
}

printStudentData(student, scores);

return 0;
}

void getStudentData(vector<string> &names, vector<vector<float>> &scr) {

string sname = "";


CC3 - Intermediate Programming/ Computer Programming 2 Module 1: Review of Computer Programming 1-1

cout << "Enter name of student: ";


cin >> sname;
names.push_back(sname);

cout << "Input the scores, you can input -1 if you're done entering the last "
<< "score.\n";

float score = 0;

vector<float> s;

while (score > -1) {


cout << "Enter score: ";
cin >> score;

if (score > -1)


s.push_back(score);
}

scr.push_back(s);
}

void printStudentData(vector<string> &names, vector<vector<float>> &scr) {

cout << left << setw(10) << "Name" << left << setw(40) << "Scores" << left;
cout << endl;

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


cout << left << setw(10) << names[i];

float sum = 0.0;


int j;

for (j = 0; j < scr[i].size(); j++) {


sum += scr[i][j];
cout << left << setw(4) << scr[i][j];
}

cout << right << setw(10) << "Average: " << fixed << setprecision(2)
<< sum / j;

cout << endl;


}
}

Submission

a) This activity must be accomplished individually.


b) You can use any C++ IDE installed in your devices or you can utilize online C++ editors/IDE in
making this output.
c) It is the source file (.cpp) of your program which you will submit to our Learning Management
System.
d) Try to solve the problems on your own without the aid of anyone or anything.

You might also like