Issue 6: Implement Push_back() & Resize()
Task Description:
In 2nd last issue (issue 4), we learnt how we can insert an element into a vector using the push_back() method. Now, we will learn how to implement this push_back method along with resize method. In this issue, each member will work on the same file so there will be merge conflicts. Now about pushback and resize:
Push_back:
When your pushback is called it should take an int value as argument and insert that value into the vector while also updating len to len+1, also if vector is already full (len==cap) then resize shall be called before inserting the value.
Resize:
When resize is called, the vector shall double its capacity to create more space. If cap is 0, it should become 1, if it is non-zero, then it shall be doubled.
Target File:
For this issue, you will be working on the following file:
dsa/pushback_and_resize.cpp
File Structure:
Your file will look like this:
#include<iostream>
using namespace std;
// Vector class
class vector {
private:
// Dont rename these variables
int* ptr;
int cap;
int len;
public:
vector(int cap=0) : ptr(nullptr), cap(cap), len(0) {}
// Replace yourName with your mentioned name in Issue Description:
void pushback_yourName(int value) {
// Your Code Here
}
// Replace yourName with your mentioned name in Issue Description:
void resize_yourName() {
// Your Code Here
}
// Dont Touch Anything Else in this Vector Class.
int size()const { return len; }
int capacity()const { return cap; }
int& operator[](int index) { return ptr[index]; }
~vector() { delete[] ptr; }
};
// Uncomment your respective lines in main function
// Main Function
int main() {
// Vector Objects of size 0
vector v1,v2,v3,v4,v5,v6,v7;
// Initial Sizes
cout<<"Initial Sizes (Empty Vectors) :\n";
// cout<<"Awais vector size: "<<v1.size()<<'\n';
// cout<<"Kaido vector size: "<<v2.size()<<'\n';
// cout<<"Talha vector size: "<<v3.size()<<'\n';
// cout<<"Velanora vector size: "<<v4.size()<<'\n';
// cout<<"Waleeja vector size: "<<v5.size()<<'\n';
// cout<<"Easha vector size: "<<v6.size()<<'\n';
// cout<<"Areeba vector size: "<<v7.size()<<'\n';
// Calling pushbacks
cout<<"\nPushing Values :\n";
// v1.pushback_awais (250011);
// v2.pushback_kaido (253015);
// v3.pushback_talha (250027);
// v4.pushback_velanora (250014);
// v5.pushback_waleeja (250005);
// v6.pushback_easha (250064);
// v7.pushback_areeba (250056);
// Resized After push backs
cout<<"\nSize Increased :\n";
// cout<<"Awais vector size: "<<v1.size()<<'\n';
// cout<<"Kaido vector size: "<<v2.size()<<'\n';
// cout<<"Talha vector size: "<<v3.size()<<'\n';
// cout<<"Velanora vector size: "<<v4.size()<<'\n';
// cout<<"Waleeja vector size: "<<v5.size()<<'\n';
// cout<<"Easha vector size: "<<v6.size()<<'\n';
// cout<<"Areeba vector size: "<<v7.size()<<'\n';
// Printing Values;
cout<<"\nPrinting Values :\n";
// cout<<"Awais : "<<v1[0]<<'\n';
// cout<<"Kaido : "<<v2[0]<<'\n';
// cout<<"Talha : "<<v3[0]<<'\n';
// cout<<"velanora : "<<v4[0]<<'\n';
// cout<<"waleeja : "<<v5[0]<<'\n';
// cout<<"easha : "<<v6[0]<<'\n';
// cout<<"areeba : "<<v7[0]<<'\n';
return 0;
}
Guidelines:
When you start writing your pushback_yourName function, first and foremost replace "yourName" with your respective name as mentioned in here:
- pushback_awais
- pushback_kaido
- pushback_talha
- pushback_velanora
- pushback_waleeja
- pushback_easha
- pushback_areeba
Do the same for for resize_yourName as well.
Important Note:
- Do Not Submit Ai Generated Codes!
- Make sure there are no memory leaks in your resize function.
- Make sure you uncomment your respective lines in main function before pushing to github.
- Do not rename the pushback_And_resize.cpp file to anything else.
- Before submitting your PR, make sure you delete the executable file and leave only cpp file.
- Do not read other's functions in their PR.
Issue 6: Implement Push_back() & Resize()
Task Description:
In 2nd last issue (issue 4), we learnt how we can insert an element into a vector using the push_back() method. Now, we will learn how to implement this push_back method along with resize method. In this issue, each member will work on the same file so there will be merge conflicts. Now about pushback and resize:
Push_back:
When your pushback is called it should take an int value as argument and insert that value into the vector while also updating len to len+1, also if vector is already full (len==cap) then resize shall be called before inserting the value.
Resize:
When resize is called, the vector shall double its capacity to create more space. If cap is 0, it should become 1, if it is non-zero, then it shall be doubled.
Target File:
For this issue, you will be working on the following file:
dsa/pushback_and_resize.cppFile Structure:
Your file will look like this:
Guidelines:
When you start writing your pushback_yourName function, first and foremost replace "yourName" with your respective name as mentioned in here:
- pushback_awais
- pushback_kaido
- pushback_talha
- pushback_velanora
- pushback_waleeja
- pushback_easha
- pushback_areeba
Do the same for for resize_yourName as well.Important Note: