Using the previous array program (Sort and Search), rewrite the program processi
ID: 3537816 • Letter: U
Question
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
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void selectionSort(string name[],double scores[],int year[],int i)
{
string tempS;
double tempD;
int tempI;
int pos;
for(int j=0;j<i-1;j++)
{
pos=j;
for(int k=j+1;k<i;k++)
{
if(year[pos]>year[k])
pos=k;
}
if(pos!=j)
{
tempI=year[pos];
year[pos]=year[j];
year[j]=tempI;
tempS=name[pos];
name[pos]=name[j];
name[j]=tempS;
tempD=scores[pos];
scores[pos]=scores[j];
scores[j]=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 binarySearch(string name[],double scores[],int year[],int i,int y)
{
int first=0,last=i-1;
int middle;
while(first<=last)
{
middle=(first+last)/2;
if(year[middle]==y)
return middle;
else if(year[middle]<y)
first=middle+1;
else
last=middle-1;
}
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);
selectionSort(name,scores,year,i);
cout<<" Sorted record :"<<endl;
display(name,scores,year,i);
int y;
cout<<"Enter the year to perform search: "<<endl;
cin>>y;
i=binarySearch(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 selectionSort(string* name,double* scores,int* year[],int i)
{
string tempS;
double tempD;
int tempI;
int pos;
for(int j=0;j<i-1;j++)
{
pos=j;
for(int k=j+1;k<i;k++)
{
if(year[pos]>year[k])
pos=k;
}
if(pos!=j)
{
tempI=*year[pos];
*year[pos]=*year[j];
*year[j]=tempI;
tempS=*name[pos];
*name[pos]=*name[j];
*name[j]=tempS;
tempD=*scores[pos];
*scores[pos]=*scores[j];
*scores[j]=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 binarySearch(string *name,double *scores,int *year,int i,int y)
{
int first=0,last=i-1;
int middle;
while(first<=last)
{
middle=(first+last)/2;
if(*year[middle]==y)
return middle;
else if(*year[middle]<y)
first=middle+1;
else
last=middle-1;
}
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);
selectionSort(name,scores,year,i);
cout<<" Sorted record :"<<endl;
display(name,scores,year,i);
int y;
cout<<"Enter the year to perform search: "<<endl;
cin>>y;
i=binarySearch(name,scores,year,i,y);
if(i)
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.