Why won\'t this program run. I got the answer from cramster. Please help. #inclu
ID: 3622879 • Letter: W
Question
Why won't this program run. I got the answer from cramster. Please help.#include <iostream>
#include <fstream>
using namespace std;
bool getdata(string a[],int n);
void printarray(string a[],int n,string mess);
void selectionsort(string a[],int n);
void bubblesort(string a[],int n);
int main()
{string a[20],b[20];
int i;
if(getdata(a,20))
{for(i=0;i<20;i++)
b[i]=a[i];
printarray(a,20,"names before selection sort");
selectionsort(a,20);
printarray(a,20," names after selection sort");
printarray(b,20," names before bubble sort");
bubblesort(b,20);
printarray(b,20," names after bubble sort");
}
system("pause");
return 0;
}
void selectionsort(string a[], int n)
{ int i,j,index,compare=0;
string min,temp;
for(i=0;i<n-1; i++)
{index=i;
min=a[i];
for(j=i+1;j<n;j++)
{compare++;
if(min.compare(a[j])>0)
{index=j;
min=a[j];
}
}
temp=a[i];
a[i]=a[index];
a[index]=temp;
}
cout<<" In Selection sort There were "<<compare<<" comparisons ";
}
bool getdata(string a[],int n)
{ifstream input;
int i;
input.open("names.dat"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return false;
}
for(i=0;i<n;i++)
input>>a[i];
input.close();
return true;
}
void printarray(string a[],int n,string mess)
{int i;
cout<<mess<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void bubblesort(string a[],int n)
{int i,j,compare=0;
string temp;
for (i=0; i<n; i++)
for (j=0; j<n-1; j++)
{compare++;
if (a[j+1].compare(a[j])<0)
{temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<" In bubble sort There were "<<compare<<" comparisons ";
}
Explanation / Answer
please rate - thanks
if your using Visual C++ need the string library
#include<string.h>
#include <iostream>
#include <fstream>
using namespace std;
bool getdata(string a[],int n);
void printarray(string a[],int n,string mess);
void selectionsort(string a[],int n);
void bubblesort(string a[],int n);
int main()
{string a[20],b[20];
int i;
if(getdata(a,20))
{for(i=0;i<20;i++)
b[i]=a[i];
printarray(a,20,"names before selection sort");
selectionsort(a,20);
printarray(a,20," names after selection sort");
printarray(b,20," names before bubble sort");
bubblesort(b,20);
printarray(b,20," names after bubble sort");
}
system("pause");
return 0;
}
void selectionsort(string a[], int n)
{ int i,j,index,compare=0;
string min,temp;
for(i=0;i<n-1; i++)
{index=i;
min=a[i];
for(j=i+1;j<n;j++)
{compare++;
if(min.compare(a[j])>0)
{index=j;
min=a[j];
}
}
temp=a[i];
a[i]=a[index];
a[index]=temp;
}
cout<<" In Selection sort There were "<<compare<<" comparisons ";
}
bool getdata(string a[],int n)
{ifstream input;
int i;
input.open("names.dat"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return false;
}
for(i=0;i<n;i++)
input>>a[i];
input.close();
return true;
}
void printarray(string a[],int n,string mess)
{int i;
cout<<mess<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void bubblesort(string a[],int n)
{int i,j,compare=0;
string temp;
for (i=0; i<n; i++)
for (j=0; j<n-1; j++)
{compare++;
if (a[j+1].compare(a[j])<0)
{temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<" In bubble sort There were "<<compare<<" comparisons ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.