Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 7 Using the previous array program (Sort and Search), rewrite the progra

ID: 3596360 • Letter: P

Question

Program 7 Using the previous array program (Sort and Search), rewrite the program using a pointer to display the original and sorted contents of the array. Ex: Use array++ where array is the name of the array

Previous program search and sort:

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
vector<string> name;
string n;
vector<int> year;
int y;
vector<double> tution;
double t;


ifstream fin("index.txt");

while(true)
{
if(fin.eof())
break;
  
fin >> n;
fin >> y;
fin >> t;
name.push_back(n);
year.push_back(y);
tution.push_back(t);

}
int size = name.size();

cout << "Original Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < size; ++i)
{
cout << name[i] << " " << year[i] << " " << tution[i] << " ";
}

cout << endl;


int min_index;

for (int i = 0; i < size-1; i++)
{

min_index = i;
for (int j = i+1; j < size; j++)
if (year[j] < year[min_index])
min_index = j;


swap(year[min_index], year[i]);
swap(name[min_index], name[i]);
swap(tution[min_index], tution[i]);
}

cout << "Sorted Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < size; ++i)
{
cout << name[i] << " " << year[i] << " " << tution[i] << " ";
}

int yr;
cout << " Enter year: ";
cin >> yr;

int l = 0;
int r = size -1;

while (l <= r)
{
int m = l + (r-l)/2;


if (year[m] == yr)
{
cout << "Record found: " << name[m] << " " << year[m] << " " << tution[m] << endl;
return 0;
}


if (year[m] < yr)
l = m + 1;


else
r = m - 1;
}


cout << "Record not found ";

return 0;
}

Explanation / Answer

// Since the index.txt file is not provided, the code cannot be run and checked.
// however the code is convered to pointer notation, you can run and check with the index.txt file

// C++ code using pointers

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<string> name;
string n;
vector<int> year;
int y;
vector<double> tution;
double t;

ifstream fin("index.txt");
while(true)
{
if(fin.eof())
break;
  
fin >> n;
fin >> y;
fin >> t;
name.push_back(n);
year.push_back(y);
tution.push_back(t);
}
int size = name.size();
cout << "Original Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < size; ++i)
{
cout << *(name+i) << " " << *(year+i) << " " << *(tution+i) << " ";
}
cout << endl;

int min_index;
for (int i = 0; i < size-1; i++)
{
min_index = i;
for (int j = i+1; j < size; j++)
if (*(year+j) < *(year+min_index))
min_index = j;

swap(*(year+min_index), *(year+i));
swap(*(name+min_index), *(name+i));
swap(*(tution+min_index), *(tution+i));
}
cout << "Sorted Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < size; ++i)
{
cout << *(name+i) << " " << *(year+i) << " " << *(tution+i) << " ";
}
int yr;
cout << " Enter year: ";
cin >> yr;
int l = 0;
int r = size -1;
while (l <= r)
{
int m = l + (r-l)/2;

if (*(year+m) == yr)
{
cout << "Record found: " << *(name+m) << " " << *(year+m) << " " << *(tution+m) << endl;
return 0;
}

if (*(year+m) < yr)
l = m + 1;

else
r = m - 1;
}

cout << "Record not found ";
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote