Using the previous array program (Sort and Search), rewrite the program using a
ID: 3537802 • Letter: U
Question
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
#include "stdafx.h"
#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<j;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<<left<<setw(20)<<name[j]<<right<<setw(10)<<scores[j]<<setw(10)<<year[j]<<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"<<endl;
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 :"<<endl;
display(name,scores,year,i);
bubbleSort(name,scores,year,i);
cout<<" Sorted record :"<<endl;
display(name,scores,year,i);
int y;
cout<<"Enter the player score to perform search: "<<endl;
cin>>y;
i=linearSearch(name,scores,year,i,y);
if(i)
cout<<"record found: "<<endl;
else
cout<<"record not found ";
return 0;
}
Explanation / Answer
#include "stdafx.h"
#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<j;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<<left<<setw(20)<<*(name+j)<<right<<setw(10)<<*(scores+j)<<setw(10)<<*(year+j)<<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 -1;
}
int main()
{
string name[20];
double scores[20];
int year[20];
string fileName="input.txt";
//cout<<"Enter the input file ";
//cin>>fileName;
ifstream in(fileName.c_str());
if(!in)
{
cout<<"Error: file could not be opened"<<endl;
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 :"<<endl;
display(name,scores,year,i);
bubbleSort(name,scores,year,i);
cout<<" Sorted record :"<<endl;
display(name,scores,year,i);
int y;
cout<<"Enter the player year to perform search: "<<endl;
cin>>y;
i=linearSearch(name,scores,year,i,y);
if(i>=0)
cout<<"record found: "<<endl;
else
cout<<"record not found ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.