






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1. Declare a struct of type student that consists of a name (string), id (int) and gpa (double). 2. Declare a dynamic array of type student where its size entered by the user. 3. Call a function called info that accepts the array and its size as input which fills the array by reading the students information. 4. Call a function Count that accepts the array and its size as input and print the average and the information of students who obtained gpa greater than class average.
Typology: Exercises
Uploaded on 10/12/2019
1 / 10
This page cannot be seen from the preview
Don't miss anything!







#include
struct student { string name; int id; double gpa; }; void info(student *arr, int size) { for (int i = 0; i < size; i++) { cout << "Enter info of student (id, name, gpa)" << i + 1 << endl; cin >> (arr[i]).id >> (arr[i]).name >> (arr[i]).gpa;
} } void Count(student *arr, int size) { double sum = 0; double avg = 0; for (int i = 0; i < size; i++) { sum = sum + (arr[i]).gpa;
} avg = sum / size; cout << "average gpa = " << avg << endl; for (int i = 0; i < size; i++) { if ((arr[i]).gpa > avg) cout << " student info: id: " << (arr[i]).id << " name :" << (arr[i]).name << " gpa : " << (arr[i]).gpa;
}
int main() {
int size; cout << "Enter the size of the array of students"; cin >> size; student* ptr = new student[size]; info(ptr, size); Count(ptr, size); delete[] ptr; system("pause"); return 0;
}
#include
public: LinkedList() { head = NULL; }
void addAfter(int x, string n, float s, string after) { node* ptr = search(after); if (ptr) { node* newNode = new node; newNode->id = x; newNode->name = n; newNode->salary = s; newNode->next = ptr->next; ptr->next = newNode; } else cout << "can't add\n"; } void addBefore(int x, string n, float s, string before) { node* ptr = search(before); if (ptr) { if (ptr == head) { addAtBeginning(x, n, s); } else { node* newNode = new node; newNode->id = x; newNode->name = n; newNode->salary = s; node* prev = head; while (prev->next != ptr) prev = prev->next; newNode->next = ptr; prev->next = newNode; } } } void deleteFromEnd() { if (isEmpty()) { return; } node* ptr = NULL;
node* del = head; while (del->next != NULL) { ptr = del; del = del->next; } if (ptr == NULL) head = NULL; else ptr->next = NULL; delete del; } void deleteFromBeginning() { if (isEmpty()) return; node* ptr = head; head = head->next; delete ptr; } void deleteSpecificNode(string n) { node* ptr = search(n); if (ptr) { if (ptr == head) { deleteFromBeginning(); } else if (ptr->next == NULL) deleteFromEnd(); else { node* prev = head; while (prev->next != ptr) prev = prev->next; prev->next = ptr->next; delete ptr; } } } void print() { if (isEmpty())
cout << "3. Search for an employee\n"; cout << "4. Delete an employee from the end of the list\n"; cout << "5. Add employee (with order)\n"; cout << "6. Delete a specific employee (with order)\n"; cout << "7. Delete the list\n"; cout << "8. Exit\n"; cin >> ch; switch (ch) { case 1: cout << "Enter id,name,salary for the employee\n"; cin >> id >> n >> s; emps.addAtBeginning(id, n, s); system("pause"); break; case 2: cout << "List of employees\n"; emps.print(); system("pause"); break; case 3: cout << "Enter employee name\n"; cin >>n; if (emps.search(n) != NULL) cout << "Emplyee exists" << endl; else cout << "Employee not exist" <<endl; system("pause"); break; case 4: emps.deleteFromEnd(); break; case 5: cout << "Enter id,name,salary for the employee\n"; cin >> id >> n >> s; cout << "choose where to add new employee\n"; cout<<"a. Insert item at the beginning of the list\n"; cout<<"b. Insert item at the end of the list\n";
cout<< "c. Insert an item before a specific node\n"; cout<< "d. Insert an item after a specific node\n"; cin >> p; switch (p) { case 'a': emps.addAtBeginning(id, n, s); break; case 'b': emps.addAtEnd(id, n, s); break; case 'c': cout << "Enter employee name to add employee before it: "; cin >> n2; emps.addBefore(id, n, s, n2); break; case 'd': cout << "Enter employee name to add employee after it: "; cin >> n2; emps.addAfter(id, n, s, n2); break; default: break; } system("pause"); break; case 6: cout << "Enter employee name to delete: "; cin >> n2; emps.deleteSpecificNode(n2); system("pause");break; case 7: emps.deleteList(); break; default: break;
} } while (ch != 8);
system("pause");
Write a complete C++ program that creates an order single linked list which contains the information of employees (id, name and salary) using classes. The program should provide the following functions: