You are first tasked with developing a search algorithm to search for a student by their ID in an array of integers. The read function in main.cpp reads the student IDs from students.txt in an array. Note that the entire array need not be updated. The index up to which the array is updated is returned by the function and stored in a variable called num_students. A research team has decided that a Ternary search algorithm works best. The algorithm is as follows: 1. Let low = 0, high = size-1; let mid1 low + (high-low)/3 and mid2 high - (high-low)/3 2. If A[mid1] is the element, return mid1; If A[mid2] is the element, return mid2 3. If element < A[mid1], perform ternary search from low to mid1 4. If element > A[mid1] and element < A[mid2], perform ternary search from mid1 to mid2 5. If element > A[mid2], perform ternary search from mid2 to high Implement the search algorithm by defining the function ternarySearch declared in main.cpp. Complete the rest of the main function to accept the student's ID as input and search for the student. Output the index of the student if found, else output a message that the student was not found. Task 2: Add one student Implement an insertion algorithm to insert a new student into the sorted array by defining the function addStudent declared in main.cpp.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 8PP: (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays...
icon
Related questions
Question

1300030051
1300308467
1300606334
1200906550
1201809169
1206505724
1301801478
1302109358
1202406962
1202704464
1203005705
1203308145
1302603281
1203903827
1304209961
1304500491
1304702995
1305101942
1306404827
1205705436
1206002391
1206304604
1206603902
1206900153
1207200292
1207502382
1207807421
1208108716
1208409718
1208709895

You are first tasked with developing a search algorithm to search for a student by their ID in an array of
integers. The read function in main.cpp reads the student IDs from students.txt in an array. Note
that the entire array need not be updated. The index up to which the array is updated is returned by the
function and stored in a variable called num_students.
A research team has decided that a Ternary search algorithm works best. The algorithm is as follows:
1. Let low = 0, high = size-1; let mid1 + low + (high-low)/3 and mid2 + high - (high-low)/3
2. If A[mid1] is the element, return mid1; If A[mid2] is the element, return mid2
3. If element < A[mid1], perform ternary search from low to mid1
4. If element > A[mid1] and element < A[mid2], perform ternary search from mid1 to mid2
5. If element > A[mid2], perform ternary search from mid2 to high
Implement the search algorithm by defining the function ternarySearch declared in main.cpp.
Complete the rest of the main function to accept the student's ID as input and search for the student.
Output the index of the student if found, else output a message that the student was not found.
Task 2: Add one student
Implement an insertion algorithm to insert a new student into the sorted array by defining the function
addStudent declared in main.cpp.
Hint: You may reuse parts of the ternary search algorithm to find the location to insert the student.
Transcribed Image Text:You are first tasked with developing a search algorithm to search for a student by their ID in an array of integers. The read function in main.cpp reads the student IDs from students.txt in an array. Note that the entire array need not be updated. The index up to which the array is updated is returned by the function and stored in a variable called num_students. A research team has decided that a Ternary search algorithm works best. The algorithm is as follows: 1. Let low = 0, high = size-1; let mid1 + low + (high-low)/3 and mid2 + high - (high-low)/3 2. If A[mid1] is the element, return mid1; If A[mid2] is the element, return mid2 3. If element < A[mid1], perform ternary search from low to mid1 4. If element > A[mid1] and element < A[mid2], perform ternary search from mid1 to mid2 5. If element > A[mid2], perform ternary search from mid2 to high Implement the search algorithm by defining the function ternarySearch declared in main.cpp. Complete the rest of the main function to accept the student's ID as input and search for the student. Output the index of the student if found, else output a message that the student was not found. Task 2: Add one student Implement an insertion algorithm to insert a new student into the sorted array by defining the function addStudent declared in main.cpp. Hint: You may reuse parts of the ternary search algorithm to find the location to insert the student.
#include <iostream>
#include <fstream>
using namespace std;
#define MAXSTUDENTS 64
Fint read(string filename, int* students) {
ifstream fin(filename);
int index = 0;
while (!fin.eof() && index < MAXSTUDENTS) {
fin >> students[index++];
}
return index;
int ternarySearch(int* students, int element, int num_students);
void addStudent (int student, int* students);
// void addStudents(int* newstudents, int* students); // Uncomment to implement bonus
Fint main() {
int* students = new int[MAXSTUDENTS];
int num_students read("students.txt", students);
******/
* TODO: Implement your code here:
* 1. Take user input - student ID
* 2. Search for the student using ternarySearch
* 3. Take user input to add student to the list
* 4. Add the student using addStudent
**
/*本
delete[] students;
return e;
Transcribed Image Text:#include <iostream> #include <fstream> using namespace std; #define MAXSTUDENTS 64 Fint read(string filename, int* students) { ifstream fin(filename); int index = 0; while (!fin.eof() && index < MAXSTUDENTS) { fin >> students[index++]; } return index; int ternarySearch(int* students, int element, int num_students); void addStudent (int student, int* students); // void addStudents(int* newstudents, int* students); // Uncomment to implement bonus Fint main() { int* students = new int[MAXSTUDENTS]; int num_students read("students.txt", students); ******/ * TODO: Implement your code here: * 1. Take user input - student ID * 2. Search for the student using ternarySearch * 3. Take user input to add student to the list * 4. Add the student using addStudent ** /*本 delete[] students; return e;
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Binary numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning