0% found this document useful (0 votes)
24 views5 pages

Assignment 3 C++

The document discusses exercises from a C++ assignment. It includes code snippets and explanations for exercises on template functions, binary search, and implementing a vector class. The vector class implementation shows methods for push, get, size, and print.

Uploaded by

Dima Azzam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Assignment 3 C++

The document discusses exercises from a C++ assignment. It includes code snippets and explanations for exercises on template functions, binary search, and implementing a vector class. The vector class implementation shows methods for push, get, size, and print.

Uploaded by

Dima Azzam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 3 C++

Dima Fadi Azzam 24/12/2023


Id: 6302

Exercise 1:
The errors are underlined
#include <iostream>
template < class T>
using namespace std; //this is missing
void print(T left, T right)
{
cout <<”Printing arguments: “ << left <<” ** “ << right;
}

int main()
{
cout << endl;
print(3, 5.8) ; //this gives an error since the function print
needs two parameters OF THE SAME TYPE (3 IS SEEN AS AN INT)
cout<< endl ;
return 0 ;
}
The corrected code is:
#include <iostream>

using namespace std;


template <class T>
void print(T left, T right) {
cout <<"Printing arguments: " << left <<" ** " << right; }
int main() {
cout << endl;
print(3.0, 5.8);
cout<< endl;
}
Exercise 2:
The output of the provided program is:
22**33**44**55**66**
c**d**g**
Exercise 3:
#include <iostream>

template <typename T>


int binarySearch(T *a, int size, T key, int low, int high) {
int l = low, h = high;
while (l <= h) {
int mid = (l + h) / 2;
if (key > a[mid]) {
l = mid + 1;
} else if (key < a[mid]) {
h = mid - 1;
} else {
return mid;
}
}
return -1;
}
int main() {
int A[] = {1, 2, 3, 4, 5, 6};
int pos = binarySearch(A, 6, 1, 0, 5);
std::cout << pos;
return 0;
}
Exercise 4:

#include "myVector.h"
#include <iostream>
template <class T>
MyVector<T>::MyVector(){
capacity=1;
arr=new T[1];
currentElement=0;
}
template <class T>
void MyVector<T>::push(T data){
if(currentElement+1==capacity){
int size=capacity;
T* temp=arr;
capacity*=2;
arr=new T[capacity];
for(int i=0;i<size;i++)
arr[i]=temp[i];
delete[] temp;
}
arr[currentElement]=data;
currentElement++;
}
template <class T>
T MyVector<T>::get(int index){
if(index>currentElement)
return -1000000000000; //we dont have such index
else
return *(arr+index);
}
template <class T>
int MyVector<T>::size(){
return currentElement;
}
template <class T>
void MyVector<T>::push(T data,int index){
if(index<0)
return;
if(currentElement+1==capacity){
int size=capacity;
T* temp=arr;
capacity*=2;
arr=new T[capacity];
for(int i=0;i<size;i++)
arr[i]=temp[i];
delete[] temp;
}
for (int i = currentElement; i > index; i--) {
arr[i] = arr[i - 1];
}
*(arr+index)=data;
currentElement++;
}
template <class T>
void MyVector<T>::print(){
for(int i=0;i<currentElement;i++)
std::cout<<*(arr+i)<<" ";
std::cout<<std::endl;
}
int main(){
MyVector<int> v;
v.push(2);
v.push(3);
v.push(10,0);
v.print();
MyVector<char> v1;
v1.push('c');
std::cout<<v1.getCapacity();
v1.push('d');
std::cout<<v1.size()<<","<<v1.getCapacity();
}

You might also like