0% found this document useful (0 votes)
126 views5 pages

F 79 Ce 6589 B 3 B 377 B 9 Ac 7

This C++ program demonstrates linear search on a list of faculty members' names and IDs. It reads in a text file containing faculty data, sorts the data by last name using selection sort, and prints out the sorted list. The program defines a Faculty class to store faculty member objects with first name, last name, and ID attributes. It implements functions for sorting, reading in the data file, and printing the sorted output.

Uploaded by

api-690889230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views5 pages

F 79 Ce 6589 B 3 B 377 B 9 Ac 7

This C++ program demonstrates linear search on a list of faculty members' names and IDs. It reads in a text file containing faculty data, sorts the data by last name using selection sort, and prints out the sorted list. The program defines a Faculty class to store faculty member objects with first name, last name, and ID attributes. It implements functions for sorting, reading in the data file, and printing the sorted output.

Uploaded by

api-690889230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Client File

/*
Isaac Bernal
Fundamentals of Programming 2
Professor Lara
4/14/22
This program demostrates the use of linear search on a list of faculty members' first name, last
name, and I.D.
*/

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Faculty.h"
using namespace std;

//Function Prototypes
void welcome();
void SelectionSort(Faculty [], int);
int getData(Faculty[], const int );
void printData(Faculty[], int);

int main() {
const int SIZE = 4;
Faculty instructorArray[SIZE];//Class Array
ifstream inputfile;//to access files
int lcount = 0;

welcome();

lcount = getData(instructorArray, SIZE);

SelectionSort(instructorArray, lcount);

printData(instructorArray, lcount);

return 0;
}

//Function Definitions
//***********************************************************
void welcome()
{
cout << "\t\t_-_Welcome To Faculty Member Sort_-_\n\n\tSorting By Last Name in Descending
Order...\n";
}

//***********************************************************
//To Receive the Faculty class array and sorts in descending order by last name
// Returns nothing
void SelectionSort(Faculty fArray[], int numbersSize)
{
int i;
int j;
int indexSmallest;
Faculty temp; // Temporary variable for swap

for (i = 0; i < numbersSize - 1; ++i)


{
// Find index of smallest remaining element
indexSmallest = i;
for (j = i + 1; j < numbersSize; ++j)
{
if ( fArray[j].getLastName() > fArray[indexSmallest].getLastName())
{
indexSmallest = j;
}
}

// Swap numbers[i] and numbers[indexSmallest]


temp = fArray[i];
fArray[i] = fArray[indexSmallest];
fArray[indexSmallest] = temp;
}
}

//***********************************************************
//Reads the text file and store in array
//returns the counter of total members of the array
int getData(Faculty arrayN[], const int SIZE)
{
istringstream inSS;//each members data is in a single line
ifstream inputfile;// to access txt file
inputfile.open("list.txt");
string lineString;

string fName , lName;//stores its own data from file


short iD;
int counter = 0;
while (counter < SIZE)
{
getline(inputfile, lineString);//stores each line of data in a string
inSS.clear();
inSS.str(lineString);//stores buffer in string
inSS >> fName >> lName >> iD;//assign to each variable

arrayN[counter].setFirstName(fName);//set to class to be stored


arrayN[counter].setLastName(lName);
inputfile.clear();

arrayN[counter].setID(iD);

counter = counter +1;


}

inputfile.close();

return counter;
}

//***********************************************************
void printData(Faculty fArray[], int SIZE)
{
cout << "\n\nThe list is: \n\n";
for (int counter = 0; counter < SIZE; ++counter)
{
cout << (counter + 1) << ". " << fArray[counter].getLastName() << endl;

cout << (counter + 1) << ". " << fArray[counter].getFirstName() << "\n";

cout << (counter + 1) << ". " << fArray[counter].getID() << "\n\n";
}
}
//* --- This file is the IMPLEMENTATION FILE ---

#include <string>
using namespace std;
#include "Faculty.h"

//Class Function Definitions


void Faculty::setFirstName(string f)
{
firstName = f;
}

void Faculty::setLastName(string l)
{
lastName = l;
}

void Faculty::setID(short i)
{
facultyID = i;
}

string Faculty::getFirstName()
{
return firstName;
}

string Faculty::getLastName()
{
return lastName;
}

short Faculty::getID()
{
return facultyID;
}
/*
--- This file is the SPECIFICATION FILE ---
*/

#include <string>
using namespace std;

#ifndef FACULTY_H
#define FACULTY_H

class Faculty
{
private:
string lastName;
string firstName;
short facultyID;
public:
//Prototypes
void setFirstName(string f);
void setLastName(string l);
void setID(short i);
string getFirstName();
string getLastName();
short getID();
};

#endif

You might also like