Using your old array program (Sort and Search), rewrite the program using pointers to manipulate and display the original and sorted contents of the array each array.
Note: Use array++ where array is the name of the array.
original program
#include
#include
#include
using namespace std;
int linearSearch(int[], int);
void printData(string[], int[], double[]);
void bubbleSort(int[], string[], double[]);
int main()
{
const int a = 10;
string name[a];
int year[a];
double tution[a];
int b = 0, r;
ifstream inFile;
inFile.open("index.txt");
if (!inFile)
{
cout << "File not found" << endl;
return 1;
}
while (inFile >> name[b] >> year[b] >> tution[b])
{
b++;
}
cout << "Original Dta" << endl << endl;
printData(name, year, tution);
bubbleSort(year, name, tution);
cout << "\n\nSorted Data" << endl << endl;
printData(name, year, tution);
cout << "\n\nEnter Year:";
cin >> r;
int pos = linearSearch(year, r);
if (pos >= 0)
{
cout << "Record found" << endl;
cout.width(15); cout << std::left << name[pos];
cout << tution[pos] << endl;
}
else
cout << "Record not fund" << endl;
return 1;
}
int linearSearch(int year[], int target)
{
int n = 10;
for (int b = 0; b < n; b++)
{
if (year[b] == target)
return b;
}
return -1;
}
void printData(string names[], int year[], double tution[])
{
int a = 10;
cout.width(20); cout << std::left << "Name";
cout.width(20); cout << std::left << "Year";
cout << "Tution" << endl << endl;
for (int b = 0; b < a; b++)
{
cout.width(20); cout << std::left << names[b];
cout.width(20); cout << std::left << year[b];
cout << tution[b] << endl;
}
}
void bubbleSort(int year[], string names[], double tution[])
{
int Year, b, a = 10;
string Name;
double Tution;
for (b = 1; b < a; ++b)
{
for(int c=0;c<(a-b);++b)
if (year[c] > year[c + 1])
{
Name = names[c];
names[c] = names[c + 1];
names[c + 1] = Name;
Year = year[c];
year[c] = year[c + 1];
year[c + 1] = Year;
Tution = tution[c];
tution[c] = tution[c + 1];
tution[c + 1] = Tution;
}
}
}