Question

In: Computer Science

Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...

Array based application

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// Function prototypes
void selectionSort(string [], int);
void displayArray(string [], int);
void readNames(string[], int);

int main()
{
   const int NUM_NAMES = 20;
   string names[NUM_NAMES];

   // Read the names from the file.
   readNames(names, NUM_NAMES);

   // Display the unsorted array.
   cout << "Here are the unsorted names:\n";
   cout << "--------------------------\n";
   displayArray(names, NUM_NAMES);

   // Sort the array.
   selectionSort(names, NUM_NAMES);

   // Display the sorted array.
   cout << "\nHere are the names sorted:\n";
   cout << "--------------------------\n";
   displayArray(names, NUM_NAMES);

   return 0;
}

// ********************************************************
// The selectionSort function performs an ascending order *
// selection sort on an array of strings. The size *
// parameter is the number of elements in the array. *
// ********************************************************
void selectionSort(string values[], int size)
{
   int startScan;
   int minIndex;
   string minValue;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
       minIndex = startScan;
       minValue = values[minIndex];

       for(int index = startScan + 1; index < size; index++)
       {
           if (values[index] < minValue)
           {
               minValue = values[index];
               minIndex = index;
           }
       }

       values[minIndex] = values[startScan];
       values[startScan] = minValue;
   }
}

// ********************************************************
// The displayArray function displays the contents of *
// the array. *
// ********************************************************
void displayArray(string values[], int size)
{
   for (int i = 0; i < size; i++)
       cout << values[i] << endl;
}

// ********************************************************
// The readNames function reads the contents of the *
// "names.dat" file into the array. *
// ********************************************************
void readNames(string values[], int size)
{
int index = 0;   // Array index

// Open the file.
ifstream inFile;
inFile.open("names.dat");

// Test that the file was opened.
if (!inFile)
{
cout << "Error opening names.dat\n";
exit(0);
}

   // Read the names from the file into the array.
while (index < size)
{
// Get a line from the file.
       getline(inFile, values[index]);

// Increment index.
index++;
}

// Close the file.
inFile.close();

Program 1:

Use the array based application above to build an equivalent STL Vector application. Keep in mind that a vector object can be passed to functions like any other object, it is not an array.

Use the following as the input data for the names.dat file.

Collins, Bill
Smith, Bart
Allen, Jim,
Griffin, Jim
Stamey, Marty
Rose, Geri,
Taylor, Terri
Johnson, Jill,
Allison, Jeff
Looney, Joe
Wolfe, Bill,
James, Jean
Weaver, Jim
Pore, Bob,
Rutherford, Greg
Javens, Renee,
Harrison, Rose
Setzer, Cathy,
Pike, Gordon
Holland, Beth

Solutions

Expert Solution

Hi

I have updated the code vector and it is working fine.

#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
// Function prototypes
void selectionSort(vector<string> &, int);
void displayArray(vector<string> &, int);
void readNames(vector<string> &);
int main()
{
vector<string> names;
// Read the names from the file.
readNames(names);
// Display the unsorted array.
cout << "Here are the unsorted names:\n";
cout << "--------------------------\n";
displayArray(names, names.size());
// Sort the array.
selectionSort(names, names.size());
// Display the sorted array.
cout << "\nHere are the names sorted:\n";
cout << "--------------------------\n";
displayArray(names, names.size());
return 0;
}
// ********************************************************
// The selectionSort function performs an ascending order *
// selection sort on an array of strings. The size *
// parameter is the number of elements in the array. *
// ********************************************************
void selectionSort(vector<string>& values, int size)
{
int startScan;
int minIndex;
string minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = values[minIndex];
for(int index = startScan + 1; index < size; index++)
{
if (values[index] < minValue)
{
minValue = values[index];
minIndex = index;
}
}
values[minIndex] = values[startScan];
values[startScan] = minValue;
}
}
// ********************************************************
// The displayArray function displays the contents of *
// the array. *
// ********************************************************
void displayArray(vector<string>& values, int size)
{
for (int i = 0; i < size; i++)
cout << values[i] << endl;
}
// ********************************************************
// The readNames function reads the contents of the *
// "names.dat" file into the array. *
// ********************************************************
void readNames(vector<string>& values)
{
// Open the file.
ifstream inFile;
inFile.open("names.dat");
string name;
// Test that the file was opened.
if (!inFile)
{
cout << "Error opening names.dat\n";
exit(0);
}
// Read the names from the file into the array.
while (!inFile.eof())
{
// Get a line from the file.
getline(inFile,name);
values.push_back(name);
// Increment index.

  
}
// Close the file.
inFile.close();

}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Here are the unsorted names:                                                                                                                                                                                                                                             

--------------------------                                                                                                                                                                                                                                               

Collins, Bill   

Smith, Bart   

Allen, Jim,   

Griffin, Jim

Stamey, Marty   

Rose, Geri,

Taylor, Terri

Johnson, Jill,   

Allison, Jeff   

Looney, Joe

Wolfe, Bill,

James, Jean

Weaver, Jim

Pore, Bob,

Rutherford, Greg

Javens, Renee,

Harrison, Rose

Setzer, Cathy,   

Pike, Gordon

Holland, Beth   

Here are the names sorted:                                                                                                                                                                                                                                               

--------------------------                                                                                                                                                                                                                                               

Allen, Jim,   

Allison, Jeff   

Collins, Bill

Griffin, Jim                                                                                                                                                                                                                                                             

Harrison, Rose                                                                                                                                                                                                                                                           

Holland, Beth                                                                                                                                                                                                                                                            

James, Jean                                                                                                                                                                                                                                                              

Javens, Renee,                                                                                                                                                                                                                                                           

Johnson, Jill,                                                                                                                                                                                                                                                           

Looney, Joe                                                                                                                                                                                                                                                              

Pike, Gordon                                                                                                                                                                                                                                                             

Pore, Bob,                                                                                                                                                                                                                                                               

Rose, Geri,                                                                                                                                                                                                                                                              

Rutherford, Greg                                                                                                                                                                                                                                                         

Setzer, Cathy,                                                                                                                                                                                                                                                           

Smith, Bart                                                                                                                                                                                                                                                              

Stamey, Marty                                                                                                                                                                                                                                                            

Taylor, Terri                                                                                                                                                                                                                                                            

Weaver, Jim                                                                                                                                                                                                                                                              

Wolfe, Bill,


Related Solutions

#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt");...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; } else...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT