Bubble array sort in C++ implementation - c++

I'm trying to get this code to sort names but it doesn't seem to work for me. What could be wrong? I have a class named Student and want to pass the names and sort them in ascending order. So what I am trying to do is pass the 4 object firstname onto the the sort_list function and sort them in ascending order and display them afterwards. However when I run the code it shows me the same order I had and the sort function didn't seem to have done anything. See if you guys can help me out here.
#include <iostream>
#include<string>
using namespace std;
//***************************************************************************
//STUDENT CLASS
//***************************************************************************
class Student
{
private:
string firstname;
string lastname;
string studentID;
string phoneNumber;
double gpa;
public:
Student();
Student(const string&, const string&, const string&, const string&, const double&);
string getfirstName() const;
string getlastName() const;
string getstudentId() const;
string getphoneNumber() const;
double getGPA() const;
void setfirstName(string&);
void setlastName(string&);
void setstudentId(string&);
void setphoneNumber(string&);
void setGAP(double&);
};
Student::Student()
{
firstname = " ";
lastname = " ";
studentID = " ";
phoneNumber = " ";
gpa = 0;
}
Student::Student(const string&a, const string&b, const string&c, const string&d, const double&e)
{
firstname = a;
lastname = b;
studentID = c;
phoneNumber = d;
gpa = e;
}
string Student::getfirstName()const
{
return firstname;
}
string Student::getlastName()const
{
return lastname;
}
string Student::getstudentId() const
{
return studentID;
}
string Student::getphoneNumber() const
{
return phoneNumber;
}
double Student::getGPA() const
{
return gpa;
}
void Student::setfirstName(string&u)
{
firstname = u;
}
void Student::setlastName(string&v)
{
lastname = v;
}
void Student::setstudentId(string&x)
{
studentID = x;
}
void Student::setphoneNumber(string&y)
{
phoneNumber = y;
}
void Student::setGAP(double&z)
{
gpa = z;
}
//***************************************************************************
//COURSE CLASS
//***************************************************************************
class Course :public Student
{
private:
string code;
int section;
int capacity;
int numStudents;
Student *list;
public:
Course();
Course(string, int, int);
~Course();
string getCourseCode();
int getSection();
int getCapacity();
int getNumStudents();
void setCourseCode(string);
void setSection(int);
void add(const Student&);
void display();
void display(const string, const int);
void remove(const string m, const int n);
void sort_list();
};
Course::Course()
{
code = "CMPT1020";
section = 1;
capacity = 35;
numStudents = 0;
list = new Student[35];
}
Course::Course(string a, int b, int c)
{
code = a;
section = b;
capacity = c;
numStudents = 0;
list = new Student[c];
}
Course::~Course()
{
delete[] list;
list = nullptr;
}
string Course::getCourseCode()
{
return code;
}
int Course::getSection()
{
return section;
}
int Course::getCapacity()
{
return capacity;
}
int Course::getNumStudents()
{
return numStudents;
}
void Course::setCourseCode(string a)
{
code = a;
}
void Course::setSection(int b)
{
section = b;
}
void Course::add(const Student& s)
{
if (numStudents == capacity)
{
cout << "Course is full" << endl;
return;
}
list[numStudents] = s;
numStudents++;
int i = numStudents - 2;
while (i >= 0 && (s.getGPA() > list[i].getGPA()))
{
list[i + 1] = list[i];
i--;
}
list[i + 1] = s;
}
void Course::display()
{
for (int i = 0; i < numStudents; i++)
{
cout<<list[i].getfirstName() <<" "<< list[i].getlastName() <<" "<< list[i].getstudentId() <<" "<< list[i].getphoneNumber() <<" "<< list[i].getGPA() << endl;
}
}
void Course::display(const string x, const int y)
{
if (y == 1)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getfirstName() == x)
{
cout << list[i].getfirstName() << " " << list[i].getlastName() << " " << list[i].getstudentId() << " " << list[i].getphoneNumber() << " " << list[i].getGPA() << endl;
}
}
}
if (y == 2)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getlastName() == x)
{
cout << list[i].getfirstName() << " " << list[i].getlastName() << " " << list[i].getstudentId() << " " << list[i].getphoneNumber() << " " << list[i].getGPA() << endl;
}
}
}
if (y == 3)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getstudentId() == x)
{
cout << list[i].getfirstName() << " " << list[i].getlastName() << " " << list[i].getstudentId() << " " << list[i].getphoneNumber() << " " << list[i].getGPA() << endl;
}
}
}
if (y == 4)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getphoneNumber() == x)
{
cout << list[i].getfirstName() << " " << list[i].getlastName() << " " << list[i].getstudentId() << " " << list[i].getphoneNumber() << " " << list[i].getGPA() << endl;
}
}
}
}
void Course::remove(const string a, const int b)
{
if (b == 1)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getfirstName() == a)
{
for (int j = i; j < numStudents; j++)
{
list[j] = list[j + 1];
}
numStudents--;
}
}
}
if (b == 2)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getlastName() == a)
{
for (int j = i; j < numStudents; j++)
{
list[j] = list[j + 1];
}
numStudents--;
}
}
}
if (b == 3)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getstudentId() == a)
{
for (int j = i; j < numStudents; j++)
{
list[j] = list[j + 1];
}
numStudents--;
}
}
}
if (b == 4)
{
for (int i = 0; i < numStudents; i++)
{
if (list[i].getphoneNumber() == a)
{
for (int j = i; j < numStudents; j++)
{
list[j] = list[j + 1];
}
numStudents--;
}
}
}
}
void Course::sort_list()
{
string temp;
for (int i = 0; i < numStudents; i++)
{
for (int j = 0; j < numStudents-1; j++)
{
if(list[j].getfirstName() >list[j+1].getfirstName())
{
temp = list[j].getfirstName();
list[j].getfirstName() = list[j+1].getfirstName();
list[j+1].getfirstName() = temp;
}
}
}
}
int main()
{
Student a("Kevin", "Chen", "300215915", "7788408028", 2);
Student b("Mickey", "Mouse", "12345678", "2222222222", 2.5);
Student c("Donald", "Duck", "24681012", "3333333333", 3.0);
Student d("Goofy", "Dog", "3579111315", "5555555555", 3.5);
Course x;
x.add(a);
x.add(b);
x.add(c);
x.add(d);
x.display();
cout << endl;
x.sort_list();
x.display();
/*cout << " " << endl;
x.remove("kevin", 1);
x.remove("Chen", 2);
x.remove("300215915", 3);
x.remove("7788408028", 4);
x.display();
cout << endl;
x.display("kevin", 1);
x.display("Mouse", 2);
x.display("24681012", 3);
x.display("5555555555", 4);*/
system("pause");
return 0;
}

It is true that your sorting code doesn't do anything.
Here is the core of it:
temp = list[j].getfirstName();
list[j].getfirstName() = list[j+1].getfirstName();
list[j+1].getfirstName() = temp;
getfirstName() returns the first name by value so all your assignments are doing is swapping temporary copies of the first names. There is no effect on the actual list elements.
I think you meant:
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
This also means temp must be declared as a Student, not a std::string.
Also, unless I'm misremembering how Bubble Sort works (which is certainly possible — given its uselessness in the real world, it's been a long time since I've written one), your outer loop needs as many iterations as it takes for the inner loop not to be doing any swaps any more. If that's somehow inherently capped at numStudents iterations, I can't see why, nor see any evidence of it on the Wikipedia article.
Assuming I am on the right lines with that, I'd have the function looking like this:
void Course::sort_list()
{
bool go_again = false;
do {
go_again = false;
for (int i = 0; i < numStudents-1; i++) {
if (list[i].getfirstName() > list[i+1].getfirstName()) {
Student temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
go_again = true;
}
}
} while(go_again);
}
(live demo)

Related

I don't know what is causing the segmentation fault in my code

I'm at my wit's end with this code and I am new to c++ so I don't even know where to begin to check for this error. What is causing the segmentation fault in my code and how do I fix it?
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Student
{
private:
string name;
int numClasses;
vector<string> classList;
public:
Student (string nameVal, int numClassesVal, string classListArr[]);
Student (string nameVal, int numClassesVal);
Student (string nameVal);
Student ();
void setName(string nameVal);
void setNumClasses(int numClassesVal);
void setClassList(string classListArr[]);
string getName();
int getNumClasses();
vector<string> getClassList();
void input();
void output();
void reset();
};
int main ()
{
Student user;
string answer;
int flag = 1;
while (flag == 1)
{
user.input();
user.output();
cout << "Do you want to enter the new data? (y/n)" << endl;
cin >> answer;
cin.ignore();
if (answer == "n")
{
flag == 0;
}
}
return 0;
}
Student::Student (string nameVal, int numClassesVal, string classListArr[])
: name(nameVal), numClasses(numClassesVal)
{
string test = classListArr[0];
for (int x = 0; x < 10; ++x)
{
if ( (x > 0) && (classListArr[x] == test))
break;
classListArr[x] = classList[x];
}
}
Student::Student (string nameVal, int numClassesVal)
{
name = nameVal;
numClasses = numClassesVal;
for (int x = 0; x < 10; ++x)
{
classList[x] = "";
}
}
Student::Student (string nameVal)
{
name = nameVal;
numClasses = 0;
for (int x = 0; x < 10; ++x)
{
classList[x] = "";
}
}
Student::Student ()
{
name = "";
numClasses = 0;
for (int x = 0; x < 10; ++x)
{
classList[x] = "";
}
}
void Student::setName(string nameVal) {name = nameVal;}
void Student::setNumClasses(int numClassesVal) {numClasses = numClassesVal;}
void Student::setClassList(string classListArr[])
{
string test = classListArr[0];
for (int x = 0; x < 10; ++x)
{
if ( (x > 0) && (classListArr[x] == test))
break;
classListArr[x] = classList[x];
}
}
string Student::getName() {return name;}
int Student::getNumClasses() {return numClasses;}
vector<string> Student::getClassList() {return classList;}
void Student::input()
{
string temp;
int classes;
cout << "Input Student's Name:" << endl;
getline(cin, temp);
name = temp;
cout << "Input Number of Classes:" << endl;
cin >> classes;
cin.ignore();
numClasses = classes;
for (int x = 0; x < 10; ++x)
{
if ( x == 9)
{
cout << "Input Last Class Name (Maximum of 10):";
getline(cin, temp);
classList[9] = temp;
}
else
{
cout << "Input Class Name:";
getline(cin, temp);
classList[x] = temp;
cout << "Do you have any more classes? (y/n):";
getline(cin, temp);
if ( temp == "n")
break;
}
}
}
void Student::output()
{
cout << "Student Name: " << name << "\nNumber of Classes: " << numClasses << endl;
cout << "Names of Classes: " << endl;
for (int x = 0; x < 10; ++x)
{
if ( numClasses == 0 )
break;
if ( x == 0 )
cout << "Names of Classes: \n";
if ( classList[x] == "" )
break;
cout << "(" << x << ") " << classList[x] << "\n";
}
}
void Student::reset()
{
numClasses = 0;
for (int x = 0; x < 10; ++x)
{
classList[x] = "";
}
}
I tried changing the main function from before where I had a goto statement instead of a flag, and that didn't fix it. I think it's the cin.getline() but I am not sure.
You have undefined behavior in this code (and the other constructors):
Student::Student ()
{
name = "";
numClasses = 0;
for (int x = 0; x < 10; ++x)
{
classList[x] = "";
}
}
You never allocated any elements in the classList vector, so the reference to classList[x] is out of bounds. You should use .push_back() to add new elements to the vector:
Student::Student ()
{
name = "";
numClasses = 0;
for (int x = 0; x < 10; ++x)
{
classList.push_back("");
}
}
Or before the loop you could do classList.reserve(10); to make room for 10 elements.
See C++ Tutorial: A Beginner's Guide to std::vector

why increment variable changing the value of the array when they have different names

Can someone please help me. I am struggling to find in my code why the last value in column B always gets incremented by one. I have written some code since its an assignment due today. I also cant figure out why the last value in column B is not equal to 196 because in the reset function it sets all the values in the array to 196 . Any suggestion would be appreciated. Thank you in advance
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
I kind of cleaned up the code a bit. It seems you found your answer in the comments, so I just did some indentation. Try and eliminate whitespaces in your code (mind you, the one I am putting here is not perfect either, but you get the point). Clean and easy to read code doesn't only make it better for you, but as you get higher up in the industry and other people begin reading and working on your code, having clean and easy to read code really helps :)
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}
Note: Some more whitespaces could even come out but I generally like to have spaces after certain statements (personal preference).

sortId: identifier not found

I'm trying to sort the students by first name, grade, and id.
When I run it, it gives me the error "identifier not found."
and I'm kind of confused because I have the void sortsID in the struct before the main.
This is in my main which includes my myDate.h. I only included some of the code that I thought was important in this question
main.cpp
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
int main() {
studentData myClass[10];
myClass[0].name = "blah blah";
//adding names to class
//random id numbers, bday, grade for each student
for (int i = 0; i < 10; i++) {
int randomId = (rand() % (9999 - 1000 + 1)) + 1000;
int randomGrade = (rand() % (100 - 50 + 1)) + 50;
int randomMonth = rand() % (12 - 1) + 1;
int randomDay = rand() % (31 - 1) + 1;
int randomYear = rand() % (1994 - 1990) + 1990;
myDate randomBday(randomMonth, randomDay, randomYear);
myClass[i].id = randomId;
myClass[i].grade = randomGrade;
myClass[i].birthday = randomBday;
}
studentData *idSort[10];
case 2:
cout << "Sorting by ID" << endl;
sortID(myClass, 10);
cout << "Displaying original list";
cout << "Student Info:\n";
cout << "=================================================\n";
cout << "NAME ID# GRADE BDAY\n";
for (int i = 0; i < 10; i++) {
idSort[i] = &(myClass[i]);
cout << left << setw(18) << idSort[i]->name << " ";
cout << left << setw(12) << idSort[i]->id << " ";
cout << setw(11) << setprecision(4) << idSort[i]->grade << " ";
idSort[i]->birthday.display();
cout << " " << endl;
}
break;
system("PAUSE"); };
void sortID(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number,
swap the records
if (s[i].id > s[i + 1].id)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
void sortGrade(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number, swap the records
if (s[i].grade > s[i + 1].grade)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
void sort_on_name(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].name is later in alphabet than s[i+1].name, swap the two records
if (strcmp(s[i].name, s[i + 1].name) > 0)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
`
You declared function sortID as a non-static member function of class studentData
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
But you call it as a non-member function
sortID(myClass, 10);
Of course the compiler does not know how this name is declared because you declared name
studentData::sortID
Moreover the function defined after main with name sortID differs from the member function because its first parameter has type studentData s[] while the early declared function has the first parameter of type studentData
void sortID(studentData s[], int n) {
studentData temp; // Local variable used to swap records
for (int i = 0; i<n; i++)
{
for (int i = 0; i<n; i++)
{
// If s[i].student_number is greater than s[i+1].student_number,
swap the records
if (s[i].id > s[i + 1].id)
{
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
}
}
}
Thank you guys for all your help!
I just used the #include < algorithmn >
So I just added this between the studentData and main()
struct compare_student_by_id {
bool operator() (const studentData & lhs, const studentData & rhs) {
return lhs.id < rhs.id;
}};
and included
std::sort(myClass, myClass + 10, compare_student_by_id());
in the main.

c++ out of range at memory location error

Here is the code I have for a programing assignment I have. Im getting this error when I run the program
"Unhandled exception at at 0x772BC41F in STRUCT2.EXE: Microsoft C++ exception: std::out_of_range at memory location 0x0043ED04." If I understand this right, the error means my array has exceeded the allotted memory space. is that correct? and if this is correct, what am I doing wrong? My input file has less than 30 elements in it.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <sstream>
#include <cctype>
using namespace std;
struct Element
{
string Name;
char eN1;
char eN2;
float Weight;
};
struct Formula
{
char Element1;
char ElementA;
int Atom;
};
void ELEMENTS(Element ElmAry[30]);
float Str2Float(string Weight);
void FORMULAS(Formula FormAry[30]);
float CalculateMoleculeWeight(Element ElmAry[30], Formula FormAry[30]);
int main()
{
ifstream inputFile1;
ifstream inputFile2;
ofstream outputFile;
inputFile1.open("Element.txt");
inputFile2.open("Formula.txt");
outputFile.open("Molecular Weight.txt");
Element ElmAry[30];
Formula FormAry[30];
char inputCh;
int i = 0;
string String1;
string mFor;
string ABRV;
int ElmDigit = 0;
float StringWeight = 0;
string Name;
string Weight;
int LENGTH = 0;
float MOLEWT;
if(!inputFile1)
{
cout << "Couldn't find the Element.txt file." << endl;
return 0;
}
if(!inputFile2)
{
cout << "Couldn't find the Formula.txt file." << endl;
return 0;
}
ELEMENTS(ElmAry);
while(inputFile1)
{
Name = String1.substr(0,2);
ElmAry[i].Name = Name;
Weight = String1.substr(3,10);
String1.clear();
StringWeight = Str2Float(Weight);
ElmAry[i].Weight = StringWeight;
i++;
}
i--;
FORMULAS(FormAry);
while (inputFile2)
{
getline(inputFile2,String1);
LENGTH = String1.length();
int j = 0;
int n = 0;
while( j < LENGTH)
{
int pos = 0;
pos = String1.find(')');
while(n < LENGTH)
{
inputCh = String1.at(n);
if(isalpha(inputCh) && isupper(inputCh))
{
FormAry[j].Element1 = String1.at(n);
n++;
inputCh = String1.at(n);
}
if(isalpha(inputCh) && islower(inputCh))
{
FormAry[j].ElementA = String1.at(n);
n++;
inputCh = String1.at(n);
}
if(ispunct(inputCh))
{
n++;
inputCh = String1.at(n);
ElmDigit = (inputCh-'0');
}
if(isdigit(inputCh))
{
FormAry[j].Atom = ElmDigit;
n++;
}
inputCh = String1.at(n);
j++;
if(iscntrl(inputCh))
{
n++;
inputCh = String1.at(n);
j++;
}
n++;
}
}
}
MOLEWT = CalculateMoleculeWeight(ElmAry, FormAry);
cout << "\t\t MOLECULAR WEIGHT CHART \t\t\n" << endl;
cout << "\n| FORMULA |\t " << "\t| ATOM.WT |" << endl;
cout << "_______________________________";
outputFile << "\t\t MOLECULAR WEIGHT CHART \t\t\n" << endl;
outputFile << "\n| FORMULA |\t " << "\t| ATOM.WT |" << endl;
outputFile << "_______________________________";
for (int a = 0; a < 30; a++)
{
cout << MOLEWT << endl;
outputFile << MOLEWT << endl;
}
inputFile1.close();
inputFile2.close();
outputFile.close();
cin.get();
cin.get();
return 0;
}
void ELEMENTS(Element ElmAry[30])
{
for(int i = 0; i < 30; i++)
{
ElmAry[i].Weight = 0;
}
}
void FORMULAS(Formula FormAry[30])
{
for(int x = 0; x < 30; x++)
{
for(int x = 0; x < 9; x++)
{
FormAry[x].Atom = 1;
}
}
}
float Str2Float (string x)
{
stringstream ss(x);
float StringWeight;
ss >> StringWeight;
return StringWeight;
}
float CalculateMoleculeWeight(Element ElmAry[30], Formula FormAry[30])
{
int i;
int j=0;
float MoleWT = 0;
float MoleSum = 0;
char e1;
char e2;
char f1;
char f2;
for(i = 0; i < 30; i++)
{
f1 = FormAry[j].Element1;
f2 = FormAry[j].ElementA;
e1 = ElmAry[i].eN1;
e2 = ElmAry[i].eN1;
if
(e1 == f1 && e2 == f2)
{
MoleWT = ElmAry[i].Weight * FormAry[j].Atom;
MoleSum = MoleSum + MoleWT;
j++;
}
}
return MoleSum;
}
when I get to
while(inputFile1)
{
Name = String1.substr(0,2);
ElmAry[i].Name = Name;
Weight = String1.substr(3,10);//invalid string position
String1.clear();
StringWeight = Str2Float(Weight);
ElmAry[i].Weight = StringWeight;
i++;
}
i--;
Weight = String1.substr(3,10); gives me an invalid string position
std::out_of_range is an exception you get when you attempt to access memory outside the space you've allocated (in an STL container). In this particular case, you are accessing areas of a std::string that has not been allocated:
Weight = String1.substr(3,10); // empty string or string.length() < 4 throws here
std::string::substr takes index parameters that must be within the bounds of the array encapsulated by std::string. If the string is only 2 characters long, and you attempt to get characters starting at the 4th position, you will see the std::out_of_range exception. You should check the length prior to doing this kind of operation.
Additionally, you are declaring your arrays:
Element ElmAry[30];
Formula FormAry[30];
But you are looping through an entire file (which potentially has more than 30 elements). Thus, when i >= 30, you are out of bounds (and behavior will be undefined).
You can fix that by using std::vector, which will allow the array to be dynamically sized, or another collection (e.g. std::list, std::deque).

Implementing a Selection Sort on an Array Based List

My question is what am I doing wrong. The insert function doesn't work correctly. I am unable to retrieve the list itself. And because of this my selection sort won't execute. Any help would be appreciated.
Header:
/** #file ListA.h */
#include <string>
using namespace std;
const int MAX_LIST = 100;
typedef string ListItemType;
class List
{
public:
List();
bool isEmpty() const;
int getLength() const;
void insert(int index, const ListItemType& newItem, bool& success);
void retrieve(int index, ListItemType& dataItem, bool & success) const;
void remove(int index, bool& success);
int selectionSortArray(int numdata[], int xnums);
List selectionSort(List selectList);
private:
ListItemType items[100];
int size;
int translate(int index) const;
};
CPP:
/** #file ListA.cpp */
#include "ArrayList.h" // header file
#include <iostream>
#include <fstream>
List::List() : size()
{
}
bool List::isEmpty() const
{
return size == 0;
}
int List::getLength() const
{
return size;
}
void List::insert(int index, const ListItemType& newItem, bool& success)
{
success = (index >= 1) &&
(index <= size + 1) &&
(size < MAX_LIST);
if (success)
{
for (int pos = size; pos >= index; --pos)
items[translate(pos + 1)] = items[translate(pos)];
items[translate(index)] = newItem;
++size;
}
}
void List::remove(int index, bool& success)
{
success = (index >= 1) && (index <= size);
if (success)
{
for (int fromPosition = index + 1;
fromPosition <= size;
++fromPosition)
items[translate(fromPosition - 1)] = items[translate(fromPosition)];
--size; // decrease the size of the list by one
} // end if
} // end remove
void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
success = (index >= 1) && (index <= size);
if (success)
dataItem = items[translate(index)];
}
int List::translate(int index) const
{
return index - 1;
}
//List List::selectionSort(List selectList)
//{
//return selectList;
int List::selectionSortArray(int numdata[], int xnums)
{
int tmp;
for (int i = 0; i < xnums -1; i++)
for (int j = i+1; j < xnums; j++)
if (numdata[i] > numdata[j])
{
tmp = numdata[i];
numdata[i] = numdata[j];
numdata[j] = tmp;
}
//for( int i = 0; i < 5; i++)
//cout << numdata[i] << " ";
return *numdata;
}
int main()
{
ListItemType insertType = "listItem1";
ListItemType retrieveType = "listitem2";
int numberofitems;
cout << "Please enter the number of data items:" << endl;
cin >> numberofitems;
cout << endl;
cout << "Please enter the data items, one per line:" << endl;
int listofitems[numberofitems];
List myArrayList;
cout << myArrayList.getLength() << endl;
if (myArrayList.isEmpty()) // tests before
{
cout << "This list is empty \n" << endl;
}
else
{
cout << "List is not empty! \n"<< endl;
}
bool mainsucc = true;
for (int i = 0; i<numberofitems; i++)
{
cout << "Enter number " << i+1 << " : " << endl;
cin >> listofitems[i];
}
for (int i =0; i <numberofitems; i++){
myArrayList.insert(listofitems[i], insertType, mainsucc);}
cout << "Size of the list is : " << myArrayList.getLength() << endl;
int listRetrieveSize = myArrayList.getLength();
int listRetrieve[listRetrieveSize];
for (int j=0; j<listRetrieveSize; j++)
{
myArrayList.retrieve(listofitems[j], retrieveType, mainsucc);
}
if (myArrayList.isEmpty()) // tests after
{
cout << "This list is empty \n" << endl;
}
else
{
cout << "List is not empty! \n"<< endl;
}
numberofitems= myArrayList.selectionSortArray(listofitems, numberofitems);
for (int i=0;i>numberofitems;i++)
{
cout<<listofitems[i];
}
return 1;
}
Your selection sort is definitely wrong:
int List::selectionSortArray(int numdata[], int xnums)
{
int tmp;
for (int i = 0; i < xnums -1; i++) {
//you should find the smallest integer inside this for loop
for (int j = i+1; j < xnums; j++) {
if (numdata[i] > numdata[j])
{
//why do you swap inside this for loop?
tmp = numdata[i];
numdata[i] = numdata[j];
numdata[j] = tmp;
}
}//missing closing }
//smap numdata[i] with currently smallest int
}//missing closing }
return *numdata;
}
Your insert function deals with strings, but you are doing selection sort on integers.
typedef string ListItemType;
defines string as ListItemType.
I'm pretty sure that what you have implemented is the Bubble sort algorithm, instead of the selection sort algorithm.