Last active
November 19, 2021 19:55
This file contains hidden or 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
#include <iostream> | |
#include "vector.hpp" | |
#include <vector> | |
#include <ctime> | |
#include <cstdlib> | |
using namespace std; | |
class Student { | |
private: | |
int id; | |
string sname; | |
double score[2]; | |
public: | |
Student() | |
{ | |
id = 0; | |
sname = ""; | |
score[0] = 0; | |
score[1] = 0; | |
} | |
Student(int i, string s, double *sc) | |
{ | |
id = i; | |
sname = s; | |
score[0] = sc[0]; //replace with for loop | |
score[1] = sc[1]; | |
} | |
void setValues(int i, string s, double *sc) | |
{ | |
id = i; | |
sname = s; | |
score[0] = sc[0]; //replace with for loop | |
score[1] = sc[1]; | |
} | |
int getID() | |
{ | |
return id; | |
} | |
string getName() | |
{ | |
return sname; | |
} | |
double *getScore() //points to the address of the sc array | |
{ | |
return score; | |
} | |
}; | |
void printStudent(Student &s) | |
{ | |
double *sc; //points to the address of each member of the sc array | |
cout << "S ID " << s.getID()<< endl; | |
cout << "S name " << s.getName()<< endl; | |
sc = s.getScore(); | |
cout << "Score 0 " << sc[0] << endl; | |
cout << "Score 1" << sc[1]<< endl; | |
} | |
int main() | |
{ | |
srand((int)time(0)); | |
VectorQueue<int, 10> arrayQueue; | |
for (int i = 0; i < 10; i++) | |
{ | |
int n = (rand() % 100) + i; //assigns n to a random value | |
arrayQueue.enqueue(n); //enqueues the new value of n | |
} | |
for (int i = 0; i < 10; i++) | |
{ | |
int popnumber = arrayQueue.dequeue(); | |
cout << popnumber << endl; | |
} | |
VectorQueue<Student, 10> studentQueue; | |
Student s[10]; // creates 3 empty student objects | |
for(int i = 0; i < 3; i++){ | |
int id; | |
string name; | |
double score[2]; | |
cout << "Please enter id number for student " << i + 1 << ": " << endl; | |
cin >> id; | |
cout << "Please enter name " << i + 1 << ": " << endl; | |
cin >> name; | |
cout << "Please enter both scores, one at a time for student " << i + 1 << ": " << endl; | |
cin >> score[0] >> score[1]; | |
s[i].setValues(id, name, score); | |
studentQueue.enqueue(s[i]); | |
} | |
double sc[2] = {100.0, 99.9}; //creating score arrays | |
Student s1(100, "John", sc); //enqueue a student when queue is full | |
studentQueue.enqueue(s1); | |
for(int i = 0; i < 3; i++) | |
{ | |
Student popstudent = studentQueue.dequeue(); | |
printStudent(popstudent); | |
} | |
Student extraPop = studentQueue.dequeue(); | |
printStudent(extraPop); //dequeue a student when queue is empty | |
} |
This file contains hidden or 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
#ifndef VECTORQUEUE_HPP | |
#define VECTORQUEUE_HPP | |
#include <iostream> | |
#include <list> | |
#include <vector> | |
using namespace std; | |
template <class T, int size = 100> | |
class VectorQueue | |
{ | |
private: | |
vector<T> queue; | |
public: | |
VectorQueue(); | |
void enqueue(T &el); | |
T dequeue(); | |
bool isFull() const; //Why are these three variables constant? | |
bool isEmpty() const; | |
T front() const; | |
T back() const; | |
int vsize() const; | |
}; | |
#include "vector.hpp" | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
template <class T, int size> | |
VectorQueue<T, size>::VectorQueue() | |
{ | |
queue.reserve(size); | |
} | |
template <class T, int size> | |
bool VectorQueue<T, size>::isFull() const | |
{ | |
return (queue.back() == queue.size() - 1); | |
} | |
template <class T, int size> | |
bool VectorQueue<T, size>::isEmpty() const | |
{ | |
return queue.empty(); | |
} | |
template <class T, int size> | |
void VectorQueue<T, size>::enqueue(T &el) | |
{ | |
if (isFull()){ | |
cout << "Queue is full!" << endl; | |
} else | |
{ | |
queue.push_back(el); | |
} | |
} | |
template <class T, int size> | |
T VectorQueue<T, size>::dequeue() | |
{ | |
if (isEmpty()) | |
{ | |
cout << "Queue is empty!" << endl; | |
} else | |
{ | |
T el = queue.front(); | |
queue.erase(queue.begin()); | |
return el; | |
} | |
} | |
template <class T, int size> | |
T VectorQueue<T, size>::front() const | |
{ | |
return queue.front(); | |
} | |
template <class T, int size> | |
int VectorQueue<T, size>::vsize() const | |
{ | |
return queue.size(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment