PROGRAM SIX Using the previous array program Sort and Search

PROGRAM SIX

Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sorted content of the array.

Ex: *(array + x) where array is the name of the array and x is the element of the array.

- For all loop manipulating arrays, use the array address as the starting point and the last element as the ending point.

- For all loops manipulating arrays, do not use subscripts to assign or compare array elements.

- Please output all currency values using two decimal places.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

void bubbleSort(string name[],double scores[],int year[],int i)

{

string tempS;

double tempD;

int tempI;

for(int j=i-1;j>0;j--)

{

for(int k=0;k

{

if(year[k]>year[k+1])

{

tempI=year[k];

year[k]=year[k+1];

year[k+1]=tempI;

tempS=name[k];

name[k]=name[k+1];

name[k+1]=tempS;

tempD=scores[k];

scores[k]=scores[k+1];

scores[k+1]=tempD;

}

}

}

}

void display(string name[],double scores[],int year[],int i)

{

for(int j=0;j

{

cout<

}

}

int linearSearch(string name[],double scores[],int year[],int i,int y)

{

for(int j=0;j

if(year[j]==y)

return i;

return 0;

}

int main()

{

string name[20];

double scores[20];

int year[20];

string fileName=\"C:\\\\input.dat\";

//cout<<\"Enter the input file \ \";

//cin>>fileName;

ifstream in(fileName.c_str());

if(!in)

{

cout<<\"Error: file could not be opened\"<

return 0;

}

int i=0;

while(in)

{

getline(in,name[i],\' \');

if(!in)

break;

in>>scores[i];

if(!in)

break;

in>>year[i];

in.ignore();

i++;

}

in.close();

cout<<\"Unsorted record :\"<

display(name,scores,year,i);

bubbleSort(name,scores,year,i);

cout<<\"\ Sorted record :\"<

display(name,scores,year,i);

int y;

cout<<\"Enter the player score to perform search: \"<

cin>>y;

i=linearSearch(name,scores,year,i,y);

if(i)

cout<<\"record found: \"<

else

cout<<\"record not found \ \";

return 0;

}

Solution

Here is the pointer oriented code equivalent for your code:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void bubbleSort(string name[],double scores[],int year[],int i)
{
string tempS;
double tempD;
int tempI;
for(int j=i-1;j>0;j--)
{
for(int k=0;k<i;k++)
{
if(*(year+k)>*(year+k+1))
{
tempI=*(year+k);
*(year+k)=*(year+k+1);
*(year+k+1)=tempI;
tempS=*(name+k);
*(name+k)=*(name+k+1);
*(name+k+1)=tempS;
tempD=*(scores+k);
*(scores+k)=*(scores+k+1);
*(scores+k+1)=tempD;
}
}
}
}
void display(string name[],double scores[],int year[],int i)
{
for(int j=0;j<i;j++)
{
cout<<*(name+i)<<\"\\t\"<<*(scores+i)<<endl;
}
}
int linearSearch(string name[],double scores[],int year[],int i,int y)
{
for(int j=0;j<i;j++)
if(*(year+j)==y)
return i;
return 0;
}
int main()
{
string name[20];
double scores[20];
int year[20];
string fileName=\"C:\\\\input.dat\";
//cout<<\"Enter the input file \ \";
//cin>>fileName;
ifstream in(fileName.c_str());
if(!in)
{
cout<<\"Error: file could not be opened\";
return 0;
}
int i=0;
while(in)
{
getline(in,*(name+i),\' \');
if(!in)
break;
in>>*(scores+i);
if(!in)
break;
in>>*(year+i);
in.ignore();
i++;
}
in.close();
cout<<\"Unsorted record :\";
display(name,scores,year,i);
bubbleSort(name,scores,year,i);
cout<<\"\ Sorted record :\";
display(name,scores,year,i);
int y;
cout<<\"Enter the player score to perform search: \";
cin>>y;
i=linearSearch(name,scores,year,i,y);
if(i)
cout<<\"record found: \";
else
cout<<\"record not found \ \";
return 0;
}

PROGRAM SIX Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sort
PROGRAM SIX Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sort
PROGRAM SIX Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sort
PROGRAM SIX Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sort
PROGRAM SIX Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sort

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site