Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
CS225: Data Structures and Programming Principles
Homework 3
1. Write a function Sum(v) that returns the sum of all elements in an STL vector v.
#include <iostream>
#include<vector>
using namespace std;
void sum(vector<int>v) {
int sum = 0;
vector<int>::iterator i;
for (i = v.begin(); i < v.end(); i++) {
sum = sum + *i;
}
cout << "sum of elements=" << sum << endl;
}
int main() {
int n;
cout << "enter n" << endl;
cin >> n;
vector<int>v(n);
for (int i = 0; i < n; i++) {
cout << "enter element:" << endl;
cin >> v[i];
}
sum(v);
}
2. What is the output screen after the below C++ program has been executed.
#include <iostream>
#include <list>
using namespace std;
void main() {
list <int> L1,L2;
list <int>::iterator L1_Iter;
L1.push_front(5);
L1.push_back(10);
L1.push_front(8);
L1.push_back(2);
L1.pop_front();
L1.push_back(1);
L2.push_back( 10 );
L2.push_back( 20 );
Instructor: Dr. Truong Quang Vinh
Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
cout << "The original list L1 is:";
for ( L1_Iter = L1.begin( ); L1_Iter != L1.end( ); L1_Iter++ )
cout << " " << *L1_Iter;
cout << endl;
L1.swap(L2);
cout << "After swapping with L2, list L1 is:";
for ( L1_Iter = L1.begin( ); L1_Iter != L1.end( ); L1_Iter++ )
cout << " " << *L1_Iter;
cout << endl;
getchar();
}
Solution:
The original list L1 is: 5 10 2 1
After swapping with L2, list L1 is: 10 20
3. Fill out the following table:
Operation Output S
insertFirst(5) p1(5) (5)
insertAfter(p1, 2) p2(2) (5, 2)
insertBefore(p2, 3) p3(3) (5,3,2)
insertFirst(9) p4(9) (9,5,3,2)
before(p3) 5 (9,5,3,2)
swapElements(p1, p2) p1(2),p2(5) (9,2,3,5)
remove(p2) (9,2,3)
first() 9 (9,2,3)
last() 3 (9,2,3)
isFirst(p2) error (9,2,3)
after(p3) error (9,2,3)
4. What is the output screen after the below C++ program has been executed.
#include <iostream>
#include <vector>
using namespace std;
Instructor: Dr. Truong Quang Vinh
Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
vector<int> vec_gen(int n) {
vector<int> V;
for (int i=0; i< n; i++)
V.push_back(i*i);
return V;
}
void main() {
int S,i;
vector <int> Vect;
Vect = vec_gen(6);
S = Vect.size();
cout << "The Vect is:" << endl;
for (i=0; i<S; i++)
cout << Vect.at(i) << '\n';
getchar();
}
Solution:
The Vect is:
0
1
4
9
16
25
5. Write a C++ function to make a list of string which contains “Ha Noi”, “Ho Chi Minh”, “Da Nang”,
“Hai Phong”, “Can Tho”. Then, the function shows all these string to the output screen as below
Five largest cities in Vietnam are:
Ha Noi, Ho Chi Minh, Da Nang, Hai Phong, and Can Tho.
Solution:
#include<iostream>
#include<list>
using namespace std;
int main() {
list<string>cities;
list <string>::iterator iter;
cities.push_front("Can Tho");
cities.push_front("Hai Phong");
cities.push_front("Da Nang");
cities.push_front( "Ho Chi Minh");
cities.push_front("Ha Noi");
cout << "five largest cities in Viet Nam are:" << endl;
for (iter = cities.begin(); iter != cities.end(); iter++)
cout << " " << *iter;
cout << endl;
Instructor: Dr. Truong Quang Vinh
Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
}
6.
a.Write C++ code for the implementation of a database by using array as described below.
Array Pointer0 Pointer1 Pointer2 Pointer3
Student Student Student
0 1 2
Class Student
Solution for question a:
#include <iostream>
#include<string>
using namespace std;
class student {
private:
string name;
string id;
public:
student() {
name = "no name";
id = "0000";
}
};
int main() {
student* A[10];
student* p;
A[0] = new student;
p = A[0];
}
b. Write a function to add a new student into the database at the position n
c. Write a function to remove a student from the database at the position n
Solution for questions b and c:
#include <iostream>
#include<string>
using namespace std;
class student {
private:
string name;
string id;
public:
void input() {
cout << "enter name:" << endl;
Instructor: Dr. Truong Quang Vinh
Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
cin >> name;
cout << "enter id:" << endl;
cin >> id;
}
void output() {
cout << "name:" << this->name << endl;
cout << "id:" << this->id << endl;
}
};
void add(student* A[],int n) {
student* p;
A[n] = new student;
p = A[n];
A[n]->input();
}
void display(student* A[] ) {
int n;
cout << "enter the position of student that you want to show" << endl;
cin >> n;
student* p;
p = A[n] ;
if (A[n] == nullptr) {
cout << "this position is empty" << endl;
}
else
A[n]->output();
}
void remove(student* A[],int pos) {
int n = 9;
if (pos < 0) {
pos = 0;
}
else if (pos >= n) {
pos = n - 1;
}
for (int i = pos; i < n - 1; i++) {
A[i] = A[i + 1];
}
--n;
}
int main() {
int n;
int ch;
int pos;
int menu = 0;
student* A[10] = { nullptr };
while (!menu) {
cout << "--------MENU--------:" << endl;
cout << "1. add a student" << endl;
cout << "2.display" << endl;
cout << "3.remove" << endl;
cout << "4. exit" << endl;
cin >> ch;
switch (ch) {
case 1: {cout << "enter position n(0 to 9):" << endl;
Instructor: Dr. Truong Quang Vinh
Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering
cin >> n;
add(A, n);
break; }
case 2: {display(A);
break; }
case 3: {cout << "enter the position that you want to remove:" << endl;
cin >> pos;
remove(A, pos);
break;
}
case 4: {menu = 1;
break; }
}
}
Instructor: Dr. Truong Quang Vinh