Created
February 24, 2013 23:28
-
-
Save shade34321/5026229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// vectorApp.cpp | |
// Week_07_Vector_Program | |
// | |
// Created by Shade Alabsa on 2/24/13. | |
// Copyright (c) 2013 Shade Alabsa. All rights reserved. | |
// | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
#include <algorithm> | |
#include <vector> | |
#include "vectorApp.h" | |
using namespace std; | |
vectorApp::vectorApp(){ | |
readNames(); | |
} | |
// ******************************************************** | |
// sort uses the STL sort function to sort the vectors * | |
// ******************************************************** | |
void vectorApp::sort(){ | |
std::sort(names.begin(), names.end()); | |
} | |
// ******************************************************** | |
// displayNames goes through the vector and * | |
// prints out the names in the vector * | |
// ******************************************************** | |
void vectorApp::displayNames() | |
{ | |
for (int i = 0; i < names.size(); i++) | |
cout << names.at(i) << endl; | |
} | |
// ******************************************************** | |
// The readNames function reads the contents of the * | |
// "names.dat" file into the vector. * | |
// ******************************************************** | |
void vectorApp::readNames() | |
{ | |
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); | |
} | |
string temp; | |
// Read the names from the file into the array. | |
while (index < names.size()) | |
{ | |
// Get a line from the file. | |
getline(inFile, temp); | |
names.push_back(temp); | |
// Increment index. | |
index++; | |
} | |
// Close the file. | |
inFile.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// Week_07_Vector_Program | |
// | |
// Created by Shade Alabsa on 2/24/13. | |
// Copyright (c) 2013 Shade Alabsa. All rights reserved. | |
// | |
#include <iostream> | |
#include "vectorApp.h" | |
using namespace std; | |
int main() | |
{ | |
vectorApp va; | |
// Display the unsorted array. | |
cout << "Here are the unsorted names:\n"; | |
cout << "--------------------------\n"; | |
va.displayNames(); | |
// Sort the array. | |
va.sort(); | |
// Display the sorted array. | |
cout << "\nHere are the names sorted:\n"; | |
cout << "--------------------------\n"; | |
va.displayNames(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Chapter 8, Programming Challenge 11: Using Files-String Selection Sort Modification | |
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// vector.h | |
// Week_07_Vector_Program | |
// | |
// Created by Shade Alabsa on 2/24/13. | |
// Copyright (c) 2013 Shade Alabsa. All rights reserved. | |
// | |
#include <vector> | |
using namespace std; | |
#ifndef vectorApp_h | |
#define vectorApp_h | |
class vectorApp{ | |
private: | |
vector<string> names; | |
public: | |
vectorApp(); | |
void sort(); | |
void displayNames(); | |
void readNames(); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment