SlideShare a Scribd company logo
1 of 17
Use a simple vector you created before to create two other more
complex vectors with
A) Memory allocation that doubles size of memory when end
reached, and 1/2's memory when the size reaches 1/4.
B) Implemented with a singularly linked list.
Main.cpp
#include
#include "SimpleVector.h"
//System Libraries
#include //Input/Output Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e,
etc...
//Function Prototypes
void fillVec(SimpleVector &);
void addVec(SimpleVector &);
void delVec(SimpleVector &);
void prntVec(SimpleVector &,int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables
int size;
//Read in the size
cout<<"What size vector to test?"<
cin>>size;
SimpleVector sv(size);
//Initialize or input i.e. set variable values
fillVec(sv);
//Display the outputs
prntVec(sv,10);
//Add and subtract from the vector
addVec(sv);
//Display the outputs
prntVec(sv,10);
//Add and subtract from the vector
delVec(sv);
//Display the outputs
prntVec(sv,10);
//Exit stage right or left!
return 0;
}
void addVec(SimpleVector &sv){
int add=sv.size()*0.1;
for(int i=1;i<=add;i++){
sv.push_front(i+add-1);
sv.push_back(i-add);
}
}
void delVec(SimpleVector &sv){
int del=sv.size()*0.2;
for(int i=1;i<=del;i++){
sv.pop_front();
sv.pop_back();
}
}
void fillVec(SimpleVector &sv){
for(int i=0;i
sv[i]=i%10;
}
}
void prntVec(SimpleVector &sv,int n){
cout<
for(int i=0;i
cout<
if(i%n==(n-1))cout<
}
cout<
}
SimpleVector.h
// SimpleVector class template
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include
#include // Needed for bad_alloc exception
#include // Needed for the exit function
using namespace std;
template
class SimpleVector
{
private:
T *aptr; // To point to the allocated array
int arraySize; // Number of elements in the array
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0;}
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
//Adding and subtracting from the Vector
void push_front(T);
void push_back(T);
T pop_front();
T pop_back();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
// Overloaded [] operator declaration
T &operator[](const int &);
};
// Constructor for SimpleVector class. Sets the size of the *
// array and allocates memory for it. *
template
SimpleVector::SimpleVector(int s)
{
arraySize = s;
// Allocate memory for the array.
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array.
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
// Copy Constructor for SimpleVector class. *
template
SimpleVector::SimpleVector(const SimpleVector &obj)
{
// Copy the array size.
arraySize = obj.arraySize;
// Allocate memory for the array.
aptr = new T [arraySize];
if (aptr == 0)
memError();
// Copy the elements of obj's array.
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
// Add 1 or Delete 1 front or back for SimpleVector class. *
template
void SimpleVector::push_front(T val){
// Allocate memory for the array.
T *newarr = 0;
try
{
newarr = new T [arraySize + 1];
}
catch (bad_alloc)
{
memError();
}
*(newarr) = val;//Add value to the front of the new array
// Copy previous array contents.
arraySize++;//Increment array size
for (int count = 1; count < arraySize; count++)
*(newarr + count) = *(aptr + count - 1);
delete aptr;//Delete previous array
aptr = newarr;
}
template
void SimpleVector::push_back(T val){
// Allocate memory for the array.
T *newarr = 0;
try
{
newarr = new T [arraySize + 1];
}
catch (bad_alloc)
{
memError();
}
// Copy previous array contents.
for (int count = 0; count < arraySize; count++)
*(newarr + count) = *(aptr + count);
*(newarr + arraySize) = val;//Add value at back of the array
arraySize++;//Increment array size
delete aptr;//Delete previous array
aptr = newarr;
}
template
T SimpleVector::pop_front(){
T dummy = 0;
if(arraySize != 0)//If array is not empty then only pop
{
dummy = *aptr;
if(arraySize == 1){
delete aptr;
aptr = 0;
}
else {
// Allocate memory for the array.
T *newarr = 0;
try {
newarr = new T[arraySize - 1];
}
catch (bad_alloc) {
memError();
}
// Copy previous array contents.
for (int count = 1; count < arraySize; count++)
*(newarr + count - 1) = *(aptr + count);
delete aptr;//Delete previous array
aptr = newarr;
}
arraySize--;//Decrease array size
}
return dummy;//Return popped value
}
template
T SimpleVector::pop_back(){
T dummy = 0;
if(arraySize != 0)//If array is not empty then only pop
{
dummy = *(aptr + arraySize - 1);
if(arraySize == 1){
delete aptr;
aptr = 0;
}
else {
// Allocate memory for the array.
T *newarr = 0;
try {
newarr = new T[arraySize - 1];
}
catch (bad_alloc) {
memError();
}
// Copy previous array contents.
for (int count = 0; count < arraySize - 1; count++)
*(newarr + count) = *(aptr + count);
delete aptr;//Delete previous array
aptr = newarr;
}
arraySize--;//Decrease array size
}
return dummy;//Return popped value
}
//**************************************
// Destructor for SimpleVector class. *
//**************************************
template
SimpleVector::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
// memError function. Displays an error message and
// terminates the program when memory allocation fails.
template
void SimpleVector::memError()
{
cout << "ERROR:Cannot allocate memory.n";
exit(EXIT_FAILURE);
}
// subError function. Displays an error message and *
// terminates the program when a subscript is out of range. *
template
void SimpleVector::subError()
{
cout << "ERROR: Subscript out of range.n";
exit(EXIT_FAILURE);
}
// getElementAt function. The argument is a subscript. *
// This function returns the value stored at the sub- *
// cript in the array. *
template
T SimpleVector::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element *
// in the array indexed by the subscript. *
template
T &SimpleVector::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
#endif
Use a simple vector you created before to create two other more

More Related Content

Similar to Use a simple vector you created before to create two other more

Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfmohdjakirfb
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structureSaad Gabr
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfarakalamkah11
 
Tutorial 6 queues & arrays & results recording
Tutorial 6   queues & arrays & results recording Tutorial 6   queues & arrays & results recording
Tutorial 6 queues & arrays & results recording Mohd Batati
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 ReviewSperasoft
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdfashokadyes
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfarihantstoneart
 

Similar to Use a simple vector you created before to create two other more (20)

Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Chp4(ref dynamic)
Chp4(ref dynamic)Chp4(ref dynamic)
Chp4(ref dynamic)
 
Namespaces
NamespacesNamespaces
Namespaces
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Js hacks
Js hacksJs hacks
Js hacks
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Tutorial 6 queues & arrays & results recording
Tutorial 6   queues & arrays & results recording Tutorial 6   queues & arrays & results recording
Tutorial 6 queues & arrays & results recording
 
Unit 4
Unit 4Unit 4
Unit 4
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 Review
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdf
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 

More from steviesellars

Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docx
Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docxAssignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docx
Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docxsteviesellars
 
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docx
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docxAssignment 3 Case HistoryWhen conducting an evaluation, it is als.docx
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docxsteviesellars
 
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docxAssignment 3 Apple Computer Leadership ReportThe Apple Computer C.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docxsteviesellars
 
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docxAssignment 3 Apple Computer Leadership ReportThe Apple Computer.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docxsteviesellars
 
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docx
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docxAssignment 2—Criminal Justice ArgumentsSeveral players interact w.docx
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docxsteviesellars
 
Assignment 2 Case of Anna OOne of the very first cases that caugh.docx
Assignment 2 Case of Anna OOne of the very first cases that caugh.docxAssignment 2 Case of Anna OOne of the very first cases that caugh.docx
Assignment 2 Case of Anna OOne of the very first cases that caugh.docxsteviesellars
 
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docx
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docxAssignment 2 Discussion—Comparison of EditorialsIn this assignmen.docx
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docxsteviesellars
 
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docx
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docxAssignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docx
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docxsteviesellars
 
ASSIGNMENT 2You will be working on a weekly generational analy.docx
ASSIGNMENT 2You will be working on a weekly generational analy.docxASSIGNMENT 2You will be working on a weekly generational analy.docx
ASSIGNMENT 2You will be working on a weekly generational analy.docxsteviesellars
 
Assignment 2Research by finding an article or case study discussi.docx
Assignment 2Research by finding an article or case study discussi.docxAssignment 2Research by finding an article or case study discussi.docx
Assignment 2Research by finding an article or case study discussi.docxsteviesellars
 
Assignment 2Accounting for stock investmentsThe beginning bal.docx
Assignment 2Accounting for stock investmentsThe beginning bal.docxAssignment 2Accounting for stock investmentsThe beginning bal.docx
Assignment 2Accounting for stock investmentsThe beginning bal.docxsteviesellars
 
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docx
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docxAssignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docx
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docxsteviesellars
 
Assignment 2 Training on Diversity TrendsAs a new member of a top.docx
Assignment 2 Training on Diversity TrendsAs a new member of a top.docxAssignment 2 Training on Diversity TrendsAs a new member of a top.docx
Assignment 2 Training on Diversity TrendsAs a new member of a top.docxsteviesellars
 
Assignment 2 Time TravelAssume you have been given th.docx
Assignment 2 Time TravelAssume you have been given th.docxAssignment 2 Time TravelAssume you have been given th.docx
Assignment 2 Time TravelAssume you have been given th.docxsteviesellars
 
Assignment 2 Time TravelAssume you have been given the ability to.docx
Assignment 2 Time TravelAssume you have been given the ability to.docxAssignment 2 Time TravelAssume you have been given the ability to.docx
Assignment 2 Time TravelAssume you have been given the ability to.docxsteviesellars
 
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docx
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docxAssignment 2 The Application PaperBy Wednesday, June 3, 2015,.docx
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docxsteviesellars
 
Assignment 2 Terrorist Organization Research a foreign terror.docx
Assignment 2 Terrorist Organization Research a foreign terror.docxAssignment 2 Terrorist Organization Research a foreign terror.docx
Assignment 2 Terrorist Organization Research a foreign terror.docxsteviesellars
 
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docx
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docxAssignment 2 The Application PaperBy Wednesday, June 10, 2015.docx
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docxsteviesellars
 
Assignment 2 State of America’s ChildrenIn today’s global world.docx
Assignment 2 State of America’s ChildrenIn today’s global world.docxAssignment 2 State of America’s ChildrenIn today’s global world.docx
Assignment 2 State of America’s ChildrenIn today’s global world.docxsteviesellars
 
Assignment 2 Social Movements ExploredIndividuals use social move.docx
Assignment 2 Social Movements ExploredIndividuals use social move.docxAssignment 2 Social Movements ExploredIndividuals use social move.docx
Assignment 2 Social Movements ExploredIndividuals use social move.docxsteviesellars
 

More from steviesellars (20)

Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docx
Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docxAssignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docx
Assignment 3 Capstone Research ProjectDue Week 10 and worth 440 p.docx
 
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docx
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docxAssignment 3 Case HistoryWhen conducting an evaluation, it is als.docx
Assignment 3 Case HistoryWhen conducting an evaluation, it is als.docx
 
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docxAssignment 3 Apple Computer Leadership ReportThe Apple Computer C.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer C.docx
 
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docxAssignment 3 Apple Computer Leadership ReportThe Apple Computer.docx
Assignment 3 Apple Computer Leadership ReportThe Apple Computer.docx
 
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docx
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docxAssignment 2—Criminal Justice ArgumentsSeveral players interact w.docx
Assignment 2—Criminal Justice ArgumentsSeveral players interact w.docx
 
Assignment 2 Case of Anna OOne of the very first cases that caugh.docx
Assignment 2 Case of Anna OOne of the very first cases that caugh.docxAssignment 2 Case of Anna OOne of the very first cases that caugh.docx
Assignment 2 Case of Anna OOne of the very first cases that caugh.docx
 
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docx
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docxAssignment 2 Discussion—Comparison of EditorialsIn this assignmen.docx
Assignment 2 Discussion—Comparison of EditorialsIn this assignmen.docx
 
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docx
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docxAssignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docx
Assignment 3 Ajax Minerals and PerrierDue Week 7 and worth 175 po.docx
 
ASSIGNMENT 2You will be working on a weekly generational analy.docx
ASSIGNMENT 2You will be working on a weekly generational analy.docxASSIGNMENT 2You will be working on a weekly generational analy.docx
ASSIGNMENT 2You will be working on a weekly generational analy.docx
 
Assignment 2Research by finding an article or case study discussi.docx
Assignment 2Research by finding an article or case study discussi.docxAssignment 2Research by finding an article or case study discussi.docx
Assignment 2Research by finding an article or case study discussi.docx
 
Assignment 2Accounting for stock investmentsThe beginning bal.docx
Assignment 2Accounting for stock investmentsThe beginning bal.docxAssignment 2Accounting for stock investmentsThe beginning bal.docx
Assignment 2Accounting for stock investmentsThe beginning bal.docx
 
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docx
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docxAssignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docx
Assignment 2 Traumatic Brain Injury Jefferson’s CaseWhen new par.docx
 
Assignment 2 Training on Diversity TrendsAs a new member of a top.docx
Assignment 2 Training on Diversity TrendsAs a new member of a top.docxAssignment 2 Training on Diversity TrendsAs a new member of a top.docx
Assignment 2 Training on Diversity TrendsAs a new member of a top.docx
 
Assignment 2 Time TravelAssume you have been given th.docx
Assignment 2 Time TravelAssume you have been given th.docxAssignment 2 Time TravelAssume you have been given th.docx
Assignment 2 Time TravelAssume you have been given th.docx
 
Assignment 2 Time TravelAssume you have been given the ability to.docx
Assignment 2 Time TravelAssume you have been given the ability to.docxAssignment 2 Time TravelAssume you have been given the ability to.docx
Assignment 2 Time TravelAssume you have been given the ability to.docx
 
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docx
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docxAssignment 2 The Application PaperBy Wednesday, June 3, 2015,.docx
Assignment 2 The Application PaperBy Wednesday, June 3, 2015,.docx
 
Assignment 2 Terrorist Organization Research a foreign terror.docx
Assignment 2 Terrorist Organization Research a foreign terror.docxAssignment 2 Terrorist Organization Research a foreign terror.docx
Assignment 2 Terrorist Organization Research a foreign terror.docx
 
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docx
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docxAssignment 2 The Application PaperBy Wednesday, June 10, 2015.docx
Assignment 2 The Application PaperBy Wednesday, June 10, 2015.docx
 
Assignment 2 State of America’s ChildrenIn today’s global world.docx
Assignment 2 State of America’s ChildrenIn today’s global world.docxAssignment 2 State of America’s ChildrenIn today’s global world.docx
Assignment 2 State of America’s ChildrenIn today’s global world.docx
 
Assignment 2 Social Movements ExploredIndividuals use social move.docx
Assignment 2 Social Movements ExploredIndividuals use social move.docxAssignment 2 Social Movements ExploredIndividuals use social move.docx
Assignment 2 Social Movements ExploredIndividuals use social move.docx
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
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.pptxAmita Gupta
 
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.pptxMaritesTamaniVerdade
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
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Ữ Â...Nguyen Thanh Tu Collection
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
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).pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
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...Poonam Aher Patil
 
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.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
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.pdfQucHHunhnh
 
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 Delhikauryashika82
 
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.pptxAreebaZafar22
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).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
 
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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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Ữ Â...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
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
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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...
 
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
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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
 
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
 
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
 

Use a simple vector you created before to create two other more

  • 1. Use a simple vector you created before to create two other more complex vectors with A) Memory allocation that doubles size of memory when end reached, and 1/2's memory when the size reaches 1/4. B) Implemented with a singularly linked list. Main.cpp #include #include "SimpleVector.h" //System Libraries #include //Input/Output Library using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes void fillVec(SimpleVector &); void addVec(SimpleVector &);
  • 2. void delVec(SimpleVector &); void prntVec(SimpleVector &,int); //Execution Begins Here! int main(int argc, char** argv) { //Declare Variables int size; //Read in the size cout<<"What size vector to test?"< cin>>size; SimpleVector sv(size); //Initialize or input i.e. set variable values fillVec(sv); //Display the outputs prntVec(sv,10); //Add and subtract from the vector addVec(sv); //Display the outputs prntVec(sv,10);
  • 3. //Add and subtract from the vector delVec(sv); //Display the outputs prntVec(sv,10); //Exit stage right or left! return 0; } void addVec(SimpleVector &sv){ int add=sv.size()*0.1; for(int i=1;i<=add;i++){ sv.push_front(i+add-1); sv.push_back(i-add); } } void delVec(SimpleVector &sv){ int del=sv.size()*0.2; for(int i=1;i<=del;i++){ sv.pop_front();
  • 4. sv.pop_back(); } } void fillVec(SimpleVector &sv){ for(int i=0;i sv[i]=i%10; } } void prntVec(SimpleVector &sv,int n){ cout< for(int i=0;i cout< if(i%n==(n-1))cout< } cout< } SimpleVector.h // SimpleVector class template
  • 5. #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H #include #include // Needed for bad_alloc exception #include // Needed for the exit function using namespace std; template class SimpleVector { private: T *aptr; // To point to the allocated array int arraySize; // Number of elements in the array void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public: // Default constructor SimpleVector() { aptr = 0; arraySize = 0;}
  • 6. // Constructor declaration SimpleVector(int); // Copy constructor declaration SimpleVector(const SimpleVector &); // Destructor declaration ~SimpleVector(); //Adding and subtracting from the Vector void push_front(T); void push_back(T); T pop_front(); T pop_back(); // Accessor to return the array size int size() const { return arraySize; } // Accessor to return a specific element T getElementAt(int position); // Overloaded [] operator declaration T &operator[](const int &);
  • 7. }; // Constructor for SimpleVector class. Sets the size of the * // array and allocates memory for it. * template SimpleVector::SimpleVector(int s) { arraySize = s; // Allocate memory for the array. try { aptr = new T [s]; } catch (bad_alloc) { memError(); } // Initialize the array. for (int count = 0; count < arraySize; count++)
  • 8. *(aptr + count) = 0; } // Copy Constructor for SimpleVector class. * template SimpleVector::SimpleVector(const SimpleVector &obj) { // Copy the array size. arraySize = obj.arraySize; // Allocate memory for the array. aptr = new T [arraySize]; if (aptr == 0) memError(); // Copy the elements of obj's array. for(int count = 0; count < arraySize; count++) *(aptr + count) = *(obj.aptr + count); } // Add 1 or Delete 1 front or back for SimpleVector class. *
  • 9. template void SimpleVector::push_front(T val){ // Allocate memory for the array. T *newarr = 0; try { newarr = new T [arraySize + 1]; } catch (bad_alloc) { memError(); } *(newarr) = val;//Add value to the front of the new array // Copy previous array contents. arraySize++;//Increment array size for (int count = 1; count < arraySize; count++) *(newarr + count) = *(aptr + count - 1); delete aptr;//Delete previous array
  • 10. aptr = newarr; } template void SimpleVector::push_back(T val){ // Allocate memory for the array. T *newarr = 0; try { newarr = new T [arraySize + 1]; } catch (bad_alloc) { memError(); } // Copy previous array contents. for (int count = 0; count < arraySize; count++) *(newarr + count) = *(aptr + count); *(newarr + arraySize) = val;//Add value at back of the array
  • 11. arraySize++;//Increment array size delete aptr;//Delete previous array aptr = newarr; } template T SimpleVector::pop_front(){ T dummy = 0; if(arraySize != 0)//If array is not empty then only pop { dummy = *aptr; if(arraySize == 1){ delete aptr; aptr = 0; } else { // Allocate memory for the array. T *newarr = 0; try {
  • 12. newarr = new T[arraySize - 1]; } catch (bad_alloc) { memError(); } // Copy previous array contents. for (int count = 1; count < arraySize; count++) *(newarr + count - 1) = *(aptr + count); delete aptr;//Delete previous array aptr = newarr; } arraySize--;//Decrease array size } return dummy;//Return popped value } template T SimpleVector::pop_back(){ T dummy = 0;
  • 13. if(arraySize != 0)//If array is not empty then only pop { dummy = *(aptr + arraySize - 1); if(arraySize == 1){ delete aptr; aptr = 0; } else { // Allocate memory for the array. T *newarr = 0; try { newarr = new T[arraySize - 1]; } catch (bad_alloc) { memError(); } // Copy previous array contents. for (int count = 0; count < arraySize - 1; count++)
  • 14. *(newarr + count) = *(aptr + count); delete aptr;//Delete previous array aptr = newarr; } arraySize--;//Decrease array size } return dummy;//Return popped value } //************************************** // Destructor for SimpleVector class. * //************************************** template SimpleVector::~SimpleVector() { if (arraySize > 0) delete [] aptr; } // memError function. Displays an error message and
  • 15. // terminates the program when memory allocation fails. template void SimpleVector::memError() { cout << "ERROR:Cannot allocate memory.n"; exit(EXIT_FAILURE); } // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * template void SimpleVector::subError() { cout << "ERROR: Subscript out of range.n"; exit(EXIT_FAILURE); } // getElementAt function. The argument is a subscript. * // This function returns the value stored at the sub- * // cript in the array. *
  • 16. template T SimpleVector::getElementAt(int sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; } // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * template T &SimpleVector::operator[](const int &sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; } #endif