100% found this document useful (1 vote)
34 views

Oop Lab 12

This document is a lab manual for a CS212 Object Oriented Programming class. It provides instructions for students to complete 3 tasks involving standard template library (STL) functions in C++. The tasks are: 1) Create a function to remove consecutive duplicates from an array, 2) Create a template display function to output all items in a vector, and 3) Create a function to remove multiples of a given number from an array. Students are instructed to copy their code and screenshots for each task into the lab manual and submit it online.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views

Oop Lab 12

This document is a lab manual for a CS212 Object Oriented Programming class. It provides instructions for students to complete 3 tasks involving standard template library (STL) functions in C++. The tasks are: 1) Create a function to remove consecutive duplicates from an array, 2) Create a template display function to output all items in a vector, and 3) Create a function to remove multiples of a given number from an array. Students are instructed to copy their code and screenshots for each task into the lab manual and submit it online.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-14CD
Fall 2023

Lab12: STL

Date: 4th December, 2023

Time: 9:00 a.m. - 12:00 p.m.

Instructor: Mehreen
Tahir

Lab Engineer: Mehwish Kiran

Name: Seemab Ramzan

CMS ID: 404989

Section: BEE-14-C
Introduction: STLs are a software reuse. Data structures and algorithms commonly used by C++
programmers and are a part of C++ Standard Library.

Objective: Understanding and using STL

Tasks:

1. Create a template function remove_duplicates()that takes a list and remove consecutive


duplicates from the list.

Code
#include <iostream>
using namespace std;

void remove_duplicates(int arr[], int& size)


{
if (size <= 1)
{
return;
}

int index = 0;
for (int i = 1; i < size; ++i) {
if (arr[i] != arr[index]) {
arr[++index] = arr[i];
}
}
size = index + 1;
}
int main()
{
int arr[] = { 7,7, 33, 33,44, 44, 44,56, 56 };
int size = sizeof(arr) / sizeof(arr[0]);

cout << "Original List: ";


for (int i = 0; i < size; ++i)
{
cout << arr[i] << " ";
}

remove_duplicates(arr, size);
cout << endl;
cout << "List after removing consecutive duplicates: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
return 0;
}

CS 212: Object Oriented Programming Page 2


Screenshot

2. Create template function display that displays all the items of the vector.

Code
#include <iostream>
#include <vector>
using namespace std;

template <typename T>


void display(const std::vector<T>& vec)
{
for (const auto& item : vec)
{
cout << item << " ";
}
cout << endl;
}

int main()
{
vector<int> intVector = { 7, 8, 9, 10, 11 };
vector<double> doubleVector = { 1.1, 2.2, 3.3, 4.4, 5.5 };
vector<string> stringVector = { "Strawberry", "Pear", "Pineapple", "Grape" };

cout << "Int Vector: ";


display(intVector);

cout << "Double Vector: ";


display(doubleVector);

cout << "String Vector: ";


display(stringVector);

return 0;
}

CS 212: Object Oriented Programming Page 3


Screenshot

3. Create a template function remove_multiples () that takes a list and a number (int) as an
argument and removes all the multiples of the number from list

Code
#include <iostream>
using namespace std;

template <typename T, size_t N>


size_t remove_multiples(T(&arr)[N], const T& number)
{
size_t newSize = 0;
for (size_t i = 0; i < N; ++i)
{
if (arr[i] % number != 0)
{
arr[newSize++] = arr[i];
}
}
return newSize;
}

int main() {
int intArray[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100 };
int numberToRemove = 2;

cout << "Original Int Array: ";


for (int i : intArray) {
cout << i << " ";
}

size_t newSize = remove_multiples(intArray, numberToRemove);


cout << endl;
cout << "Array after removing multiples of " << numberToRemove << ": ";
for (size_t i = 0; i < newSize; ++i) {
cout << intArray[i] << " ";
}
return 0;
}

CS 212: Object Oriented Programming Page 4


Screenshot

IMPORTANT
• You are encouraged to use good programming conventions by entering appropriate
comments, using indentations, and using descriptive variable names in your programs.

Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS and the name of the task must be in this format YourFullName_reg#.

CS 212: Object Oriented Programming Page 5

You might also like