1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
jenyasd209 [6]
3 years ago
11

For this lab you will be creating a auto-expanding dynamic-array. This array will explicitly hold std library strings. As with a

ll labs you can create any PRIVATE data members/ methods you want, but the Public interface should remain the same. While tests will be provided, you will need to add your own test cases to ensure all corner cases are accounted for and avoided. This class will be used for future labs so it is important that it is tested thouroughly.
Engineering
1 answer:
Inessa05 [86]3 years ago
8 0

Answer:

Explanation:

Program is divided into 3 files. stringVector.h , stringVectory.cpp (implementation file) and main.cpp (testing file)

stringVector.h

#include<iostream>

using namespace std;

class stringVector {

private:

string * data;

unsigned length;

unsigned allocated_length;

public:

stringVector();

virtual ~stringVector();

unsigned size();

unsigned capacity();

void reserve(unsigned new_size);

bool empty();

void append(string new_data);

void swap(unsigned pos1, unsigned pos2);

stringVector &operator = (stringVector const &rhs);

string& operator[](unsigned position);

void sort();

};

stringVector.cpp

#include<iostream>

#include<stdexcept>

#include"stringVector.h"

stringVector::stringVector()

{

data = NULL;

length = 0;

allocated_length = 0;

}

stringVector::~stringVector()

{

delete [] data;

}

unsigned stringVector::size()

{

return length;

}

unsigned stringVector::capacity()

{

return allocated_length;

}

void stringVector::reserve(unsigned new_size)

{

string *temp = new string[new_size]; // Create a new array

 

/*Copy the contents of the array*/

for(int i =0; i < new_size; i++){

if(i < length){

temp[i] = data[i];

}

else

break;

}

 

delete []data ;// Delete previous array

data = temp;

allocated_length = new_size;

if(length > new_size){

length = new_size;

}

}

bool stringVector::empty(){

return (length == 0) ? true : false;

}

void stringVector::append(string new_data)

{

string *temp = NULL;

if(length == allocated_length){

if(allocated_length == 0){

data = new string[10];

allocated_length = 10;    

}

else{

temp = new string[2 * allocated_length]; // Create a new array with double the size

for(int i = 0; i < length ; i++){

temp[i] = data[i];

}

allocated_length = 2 * allocated_length;

 

if(data != NULL)

delete []data;

data = temp;

}

}

data[length] = new_data;

length++;

}

void stringVector::swap(unsigned pos1, unsigned pos2)

{

string str;

if((pos1 >= length) || (pos2 >= length)){

cout << "Index Out of bounds" << endl;

return;

}

str = data[pos1];

data[pos1] = data[pos2];

data[pos2] = str;

}

stringVector& stringVector::operator = (stringVector const &rhs)

{

delete [] data;

length = rhs.length;

allocated_length = rhs.allocated_length;

this->data = new string[allocated_length];

for(int i=0 ; i < length; i++)

this->data[i] = rhs.data[i];

}

string& stringVector::operator[](unsigned position)

{

if(position > length){

throw std::out_of_range("Position out of range");

}

return data[position - 1];

}

void stringVector::sort()

{

string str;

for(int i= (length - 1) ; i > 0; i--){

for(int j = 0; j < i; j++){

if(data[j].compare(data[j+1]) > 0){

//Swap

str = data[j];

data[j] = data[j+1];

data[j+1] = str;

}

}

}

}

main.cpp

#include<iostream>

#include<stdexcept>

#include"stringVector.h"

void printVector(stringVector &);

int main()

{

stringVector vector;

string str1("California");

string str2("Alabama");

string str3("Oklahoma");

string str4("Texas");

vector.append(str1);

vector.append(str2);

vector.append(str3);

vector.append(str4);

cout << vector.size() << " - " << vector.capacity() << endl;

printVector(vector);

cout << endl;

vector.reserve(3);

cout << vector.size() << " - " << vector.capacity() << endl;

printVector(vector);

cout << endl;

vector.append(str4);

cout << vector.size() << " - " << vector.capacity() << endl;

printVector(vector);

cout << endl << "Copied vector : " << endl;

stringVector vector2 = vector;

printVector(vector2);

cout << endl;

vector.sort();

cout << "Vector 1 after sorting" << endl;

printVector(vector);

return 0;

}

void printVector(stringVector &vec)

{

cout << "Vector : ";

for(int i = 1 ; i <= vec.size(); i++){

cout << vec[i] << " ";

}

cout << endl;

}

OUTPUT:

[[email protected] cppProg3]$ ./main

4 - 10

Vector: California Alabama Oklahoma Texas

3 -3

Vector: California Alabama Oklahoma

4 - 6

Vector: California Alabama Oklahoma Texas

Copied Vector:

Vector: California Alabama Oklahoma Texas

Vector 1 after sorting

Vector: Alabama California Oklahoma Texas

You might be interested in
Which of the following explains the purpose of a convertible screwdriver?
aliya0001 [1]

Answer:

The correct option is;

To be able to switch between different heads quickly and easily

Explanation:

A screwdriver is a tool or device that has a grip handle which surrounds an extension shaft that has a forged tip that fits into the complementary groove  on a screw head such that by turning the handle while the forged head of the screwdriver sits in a screw head, the screw is turned

A convertible screw driver is built with the capability to easily change the tip or other attributes of the screwdriver quickly.

7 0
3 years ago
For Figure Below, if the elevation of the benchmark A is 25.00 m above MSL:
nlexa [21]

Answer:

For Figure Below, if the elevation of the benchmark A is 25.00 m above MSL:

1. Using the Rise and Fall Method, find the reduced level for all points. (Construct the Table)

2. Using HPC Method, find the reduced level for all points. ( Construct the Table).

3. Show all required Arithmetic checks for your work. For Item 1 and 2.

4. What is the difference in height between points H and D?

5. What is the gradient of the line connecting A and J, knowing that horizontal distance = 200

m.

8 0
4 years ago
A divided multilane highway in a recreational area has four lanes (two lanes in each direction) and is on rolling terrain. The h
sertanlavr [38]

Answer:

Explanation:

Before: PT= 0.10, PB= 0.03 (given) ET = 2.5 ER = 2.0 (Table 6.5)

fHV= 0.847 (Eq. 6.5) PHF = 0.95, fp= 0.90, N=2, V = 2200 (given)

vp= V/[PHF⋅fHVTB⋅fp⋅N] = 1518.9 (Eq. 6.3)

BFFS = 50+5, BFFS =55 (given) fLW= 6.6

TLC=6+3=9 fLC= 0.65

fM= 0.0

fA= 1.0

FFS = BFFS −fLW−fLC–fM–fA= 46.75 (Eq. 6.7)

Use FFS=45 D= vp/S = 33.75pc/mi/ln Eq (6.6)

After: fA= 3.0

FFS = BFFS −fLW−fLC–fM–fA= 44.75 (Eq. 6.7)

Use FFS=45 Vnew= 2600 Vp= Va/[PHF⋅fHB⋅fp⋅N] = 1795 (Eq. 6.3) D= vp/S = 39.89pc/mi/ln

8 0
3 years ago
If the reading of mercury manometer was 728 mmHg, what is the reading for another liquid such as water in mH20 units?​
vodka [1.7K]

Answer:

mH275 units

Explanation:

  • that was true
4 0
3 years ago
How do you know when an equation is (In)​
pav-90 [236]
If a vertical line crosses the relation on the graph only once in all locations, the relation is a function. However, if a vertical line crosses the relation more than once, the relation is not a function. Using the vertical line test, all lines except for vertical lines are functions
3 0
3 years ago
Other questions:
  • Electric heater wires are installed in a solid wall having a thickness of 8 cm and k=2.5 W/m.°C. The right face is exposed to an
    5·2 answers
  • Using Python, have your program do the following, using loops (no recursion)
    5·1 answer
  • The specific volume of a system consisting of refrigerant-134a at 1.0 Mpa is 0.01 m /kg. The quality of the R-134a is: (a) 12.6%
    15·1 answer
  • please help will give brainliest Explain why having personal protection equipment (PPE), such as masks, gloves, and suits, is a
    14·1 answer
  • At high altitude air temperature is low and ice formation on wings can cause serious issues for air crafts.A solution to this pr
    5·1 answer
  • A silicon diode has a saturation current of 6 nA at 25 degrees Celcius. What is the saturation current at 100 degrees Celsius?
    15·1 answer
  • Help please!
    13·1 answer
  • WHICH OF THIS PLS FAST!!!
    5·1 answer
  • X − 3y − 8z = −10 2x + 5y + 6z = 13 3x + 2y − 2z = 3
    12·1 answer
  • How do you know what year a mustang is?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!