SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
These are the things I have to do to the code please help me out.
Change struct to class
Make all the members private
Add get size , get capacity and get array methods
Add remove , index of and operator+ methods
Add oprator< method, and together with other five ( > , >= , <= , == , != )
Complete and debug all the other methods in the sample code, such as insert
Write some testing code in the main function
#include <iostream>
using namespace std;
struct Vec
{
int _size;
int _cap;
int* arr;
Vec()
{
_size = 0;
_cap = 4;
arr = new int[_cap];
}
void insert( int n, int x )
{
for ( int i = _size-1; i >= n; i-- )
{
arr[i+1] = arr[i];
}
arr[n] = x;
_size++;
}
void reserve( int new_cap )
{
if ( new_cap > _cap )
{
_cap = new_cap;
int *tmp = new int[_cap];
for ( int i = 0; i < _size; i++ )
{
tmp[i] = arr[i];
}
delete[] arr;
arr = tmp;
}
}
void push_back( int x )
{
if ( _size == _cap )
{
_cap *= 2;
reserve(_cap);
}
arr[_size++] = x;
}
void pop() {
if (_size > 0) _size--;
}
int& at( int idx )
{
if ( idx >= _size || idx < 0 )
{
throw "[Vector] Index out of bound!";
}
return arr[idx];
}
int& operator[]( int idx )
{
if ( idx >= _size || idx < 0 )
{
throw "[Vector] Index out of bound!";
}
return arr[idx];
}
~Vec()
{
delete[] arr;
}
};
ostream& operator<<(ostream& os, Vec& v)
{
os << "[ ";
for ( int i = 0; i < v._size-1; i++ )
{
os << v.arr[i] << ", ";
}
os << v.arr[v._size-1] << " ]";
return os;
}
int main()
{
Vec v;
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(2);
v.push_back(8);
//v.insert(1, 7);
v[3] = 5;
cout << v[3] << endl;
//v.at(3) = 5;
cout << v << endl;
//cout << v._cap << endl;
//v.reserve(20);
//cout << v._cap << endl;
//cout << v << endl;
//cout << v._size << endl;
//cout << v._cap << endl;
return 0;
}
These are the things I have to do to the code please help me out- Chan.pdf

Contenu connexe

Similaire à These are the things I have to do to the code please help me out- Chan.pdf

C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
Neeraj Kaushik
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
MaruMengesha
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 

Similaire à These are the things I have to do to the code please help me out- Chan.pdf (20)

Java arrays
Java    arraysJava    arrays
Java arrays
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
Arrays
ArraysArrays
Arrays
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
 

Plus de DylanTZEAverys

Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdfUnderstanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
DylanTZEAverys
 
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdfUnderstanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
DylanTZEAverys
 
Topic - Process Improvement- College admission process improvement for.pdf
Topic - Process Improvement- College admission process improvement for.pdfTopic - Process Improvement- College admission process improvement for.pdf
Topic - Process Improvement- College admission process improvement for.pdf
DylanTZEAverys
 

Plus de DylanTZEAverys (20)

tusd tee ieguiments-cmodot exarpie Get mote belp -Rest the iegulendets.pdf
tusd tee ieguiments-cmodot exarpie Get mote belp -Rest the iegulendets.pdftusd tee ieguiments-cmodot exarpie Get mote belp -Rest the iegulendets.pdf
tusd tee ieguiments-cmodot exarpie Get mote belp -Rest the iegulendets.pdf
 
Use the data below to compute GDP by the expenditure approach- All fig.pdf
Use the data below to compute GDP by the expenditure approach- All fig.pdfUse the data below to compute GDP by the expenditure approach- All fig.pdf
Use the data below to compute GDP by the expenditure approach- All fig.pdf
 
urgent The deadweight logs of the tax ls shownt by anded 7- A+B+F th-.pdf
urgent The deadweight logs of the tax ls shownt by anded 7- A+B+F th-.pdfurgent The deadweight logs of the tax ls shownt by anded 7- A+B+F th-.pdf
urgent The deadweight logs of the tax ls shownt by anded 7- A+B+F th-.pdf
 
Use MV-P(1+RT) to find the maturity value (in $ -$ of the loan-.pdf
Use MV-P(1+RT) to find the maturity value (in $ -$ of the loan-.pdfUse MV-P(1+RT) to find the maturity value (in $ -$ of the loan-.pdf
Use MV-P(1+RT) to find the maturity value (in $ -$ of the loan-.pdf
 
Understand how atmospheric nitrogen (N2) is converted to ammonia and s.pdf
Understand how atmospheric nitrogen (N2) is converted to ammonia and s.pdfUnderstand how atmospheric nitrogen (N2) is converted to ammonia and s.pdf
Understand how atmospheric nitrogen (N2) is converted to ammonia and s.pdf
 
Understand what makes up an ecosystem- microbiome- guild and populatio.pdf
Understand what makes up an ecosystem- microbiome- guild and populatio.pdfUnderstand what makes up an ecosystem- microbiome- guild and populatio.pdf
Understand what makes up an ecosystem- microbiome- guild and populatio.pdf
 
Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdfUnderstanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis-.pdf
 
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdfUnderstanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
Understanding how the gum disease pathogen- Porphyromonas gingivalis- (1).pdf
 
Unknown D fizzes when dilute hydrochloric acid is applied- What type o.pdf
Unknown D fizzes when dilute hydrochloric acid is applied- What type o.pdfUnknown D fizzes when dilute hydrochloric acid is applied- What type o.pdf
Unknown D fizzes when dilute hydrochloric acid is applied- What type o.pdf
 
Type A hemophilia in dogs is sex linked recessive disorder- There is a.pdf
Type A hemophilia in dogs is sex linked recessive disorder- There is a.pdfType A hemophilia in dogs is sex linked recessive disorder- There is a.pdf
Type A hemophilia in dogs is sex linked recessive disorder- There is a.pdf
 
Two different models have been developed to describe the pace of evolu.pdf
Two different models have been developed to describe the pace of evolu.pdfTwo different models have been developed to describe the pace of evolu.pdf
Two different models have been developed to describe the pace of evolu.pdf
 
True or False- We can overcome a component auditor's lack of independe.pdf
True or False- We can overcome a component auditor's lack of independe.pdfTrue or False- We can overcome a component auditor's lack of independe.pdf
True or False- We can overcome a component auditor's lack of independe.pdf
 
True or False- If Mendel had compared traits controlled by genes on th.pdf
True or False- If Mendel had compared traits controlled by genes on th.pdfTrue or False- If Mendel had compared traits controlled by genes on th.pdf
True or False- If Mendel had compared traits controlled by genes on th.pdf
 
Tristan Walker has also started a foundation called Code2040- Code2040.pdf
Tristan Walker has also started a foundation called Code2040- Code2040.pdfTristan Walker has also started a foundation called Code2040- Code2040.pdf
Tristan Walker has also started a foundation called Code2040- Code2040.pdf
 
Trypanosomes are parasites which cause dsease in humans and animals In.pdf
Trypanosomes are parasites which cause dsease in humans and animals In.pdfTrypanosomes are parasites which cause dsease in humans and animals In.pdf
Trypanosomes are parasites which cause dsease in humans and animals In.pdf
 
True or False- -home-etaylor-Documents-foo-txt is an absolute file pat.pdf
True or False- -home-etaylor-Documents-foo-txt is an absolute file pat.pdfTrue or False- -home-etaylor-Documents-foo-txt is an absolute file pat.pdf
True or False- -home-etaylor-Documents-foo-txt is an absolute file pat.pdf
 
Total cholesterol in children aged 10-15 is assumed to follow a normal.pdf
Total cholesterol in children aged 10-15 is assumed to follow a normal.pdfTotal cholesterol in children aged 10-15 is assumed to follow a normal.pdf
Total cholesterol in children aged 10-15 is assumed to follow a normal.pdf
 
Tool-Technique Bioinformatics Nanotechnology Clinical Modeling-trials.pdf
Tool-Technique Bioinformatics Nanotechnology Clinical Modeling-trials.pdfTool-Technique Bioinformatics Nanotechnology Clinical Modeling-trials.pdf
Tool-Technique Bioinformatics Nanotechnology Clinical Modeling-trials.pdf
 
Topic - Process Improvement- College admission process improvement for.pdf
Topic - Process Improvement- College admission process improvement for.pdfTopic - Process Improvement- College admission process improvement for.pdf
Topic - Process Improvement- College admission process improvement for.pdf
 
Today- you have learned about Sexual reproduction- which is performed.pdf
Today- you have learned about Sexual reproduction- which is performed.pdfToday- you have learned about Sexual reproduction- which is performed.pdf
Today- you have learned about Sexual reproduction- which is performed.pdf
 

Dernier

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

These are the things I have to do to the code please help me out- Chan.pdf

  • 1. These are the things I have to do to the code please help me out. Change struct to class Make all the members private Add get size , get capacity and get array methods Add remove , index of and operator+ methods Add oprator< method, and together with other five ( > , >= , <= , == , != ) Complete and debug all the other methods in the sample code, such as insert Write some testing code in the main function #include <iostream> using namespace std; struct Vec { int _size; int _cap; int* arr; Vec() { _size = 0; _cap = 4; arr = new int[_cap]; } void insert( int n, int x ) { for ( int i = _size-1; i >= n; i-- ) { arr[i+1] = arr[i]; } arr[n] = x; _size++; } void reserve( int new_cap ) { if ( new_cap > _cap ) {
  • 2. _cap = new_cap; int *tmp = new int[_cap]; for ( int i = 0; i < _size; i++ ) { tmp[i] = arr[i]; } delete[] arr; arr = tmp; } } void push_back( int x ) { if ( _size == _cap ) { _cap *= 2; reserve(_cap); } arr[_size++] = x; } void pop() { if (_size > 0) _size--; } int& at( int idx ) { if ( idx >= _size || idx < 0 ) { throw "[Vector] Index out of bound!"; } return arr[idx]; } int& operator[]( int idx ) { if ( idx >= _size || idx < 0 ) { throw "[Vector] Index out of bound!"; } return arr[idx]; } ~Vec() {
  • 3. delete[] arr; } }; ostream& operator<<(ostream& os, Vec& v) { os << "[ "; for ( int i = 0; i < v._size-1; i++ ) { os << v.arr[i] << ", "; } os << v.arr[v._size-1] << " ]"; return os; } int main() { Vec v; v.push_back(3); v.push_back(1); v.push_back(4); v.push_back(2); v.push_back(8); //v.insert(1, 7); v[3] = 5; cout << v[3] << endl; //v.at(3) = 5; cout << v << endl; //cout << v._cap << endl; //v.reserve(20); //cout << v._cap << endl; //cout << v << endl; //cout << v._size << endl; //cout << v._cap << endl; return 0; }