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
A sleeve made of SAE 4150 annealed steel has a nominal inside diameter of 3.0 inches and an outside diameter of 4.0 inches. It i
irga5000 [103]

Answer:

Class of fit:

Interference (Medium Drive Force Fits constitute a special type of Interference Fits and these are the tightest fits where accuracy is important).

Here minimum shaft diameter will be greater than the maximum hole diameter.

Medium Drive Force Fits are FN 2 Fits.

As per standard ANSI B4.1 :

Desired Tolerance: FN 2

Tolerance TZone: H7S6

Max Shaft Diameter: 3.0029

Min Shaft Diameter: 3.0022

Max Hole Diameter:3.0012

Min Hole Diameter: 3.0000

Max Interference: 0.0029

Min Interference: 0.0010

Stress in the shaft and sleeve can be considered as the compressive stress which can be determined using load/interference area.

Design is acceptable If compressive stress induced due to selected dimensions and load is less than compressive strength of the material.

Explanation:

4 0
3 years ago
Read 2 more answers
Write an expression that evaluates to true if the value of variable lastName is greater than the string Dexter.
gregori [183]

Answer:

lastName.compareTo("Dexter")>0

Explanation:

The expression that evaluates to true if the value of variable lastName is greater than the string Dexter is; lastName.compareTo("Dexter")>0.

4 0
3 years ago
Given an integer k, a set C of n cities c1, . . . , cn, and the distances between these cities dij = d(ci , cj ), for 1 ⤠i &lt
Snezhnost [94]

Answer:

See explaination

Explanation:

2-Approximation Algorithm

Step 1: Choose any one city from the given set of cities C arbitrarily and put it in to a set H which is initially empty.

Step 2: For every city c in set C that is currently not present in set H compute min_distc = Minimum[ d(c, c1), d(c, c2), d(c, c3), ..... . . . . d(c, ci) ]

where c1, c2, ... ci are the cities in set H

and d(x, y) is the euclidean distance between city x and city y

Step 3: H = H ∪ {cx} where cx is the city have maximum value of min_dist over all possible cities c, computed in Step-2.

Step 4: Step-2 and Step-3 are iterated for k-1 times so that k cities are included int set H.

The set H is the required set of cities.

Example

Assume:-

C = {0, 1, 2, 3}

d(0,1) = 10, d(0,2) = 7, d(0,3) = 6, d(1,2) = 8, d(1,3) = 5, d(2,3) = 12

k = 3

Solution:-

Initially H = { }

Step-1: H = {0}

Step-2: Cities c \not\in H are {1, 2, 3}

min_dist1 = min{dist(0,1)} = min{10} = 10

min_dist2 = min{dist(0,2)} = min{7} = 7

min_dist3 = min{dist(0,3)} = min{6} = 6

Step-3: Max{10, 7, 6} = 10

Step-4: cx = 1

Step-5: H = H ∪ cx = {0} \cup {1} = {0, 1}

Step-6: Cities c \not\in H are {2, 3}

min_dist2 = min{dist(0,2), dist(1,2)} = min{7, 8} = 7

min_dist3 = min{dist(0,3), dist(1,3)} = min{6, 5} = 5

Step-7: Max{7, 5} = 7

Step-8: cx = 2

Step-9: H = H \cup cx = {0, 1} \cup {2} = {0, 1, 2}

Result: The set H is {0, 1, 2}.

6 0
3 years ago
Name two parts that are sometimes made of hard metal and pressed into the head​
gayaneshka [121]

Answer:copper , stainless steal

Wrought iron

Explanation:

6 0
3 years ago
"Transportation is the way of expanding business activies" justify this statement with long answer​
Leokris [45]

Answer:

Transportation methods ensure deliveries to and from your facility flow smoothly and arrive at their designated destinations on time. Because of the importance of transportation to your business's success, it's vital to include this factor in your supply chain management strategy.

So,transportation is the way of expanding business activities.

4 0
3 years ago
Other questions:
  • Determine the carburizing time necessary to achieve a carbon concentration of 0.30 wt% at a position 4 mm into an iron–carbon al
    11·1 answer
  • What does product integration refer to in an advanced manufacturing setting?
    9·2 answers
  • A 5 mm dia copper cable is insulated with a material of conductivity of 0.16 W/mK and is exposed to air at 30°C with a convecti
    11·1 answer
  • The products of combustion from burner are routed to an industrial application through a thin-walled metallic duct of diameter D
    6·1 answer
  • 1)A wheel is used to turn a valve stem on a water valve. If the wheel radius is 1 foot and the stem, (axle), radius is .5 inches
    10·1 answer
  • What is a height gage?
    14·1 answer
  • Problem the pressure at a given point is 50 mmhg absolute
    12·1 answer
  • The water depths upstream and downstream of a hydraulic jump is 0.3 m and 1.2 m. Determine flow rate
    9·1 answer
  • Two long pipes convey water between two reservoirs whose water surfaces are at different elevations. One pipe has a diameter twi
    13·1 answer
  • Describe two fundamental reasons why flexural strength should depend on porosity
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!