Help me to do on File in / out for C++ assignment, can you someone teach me how
ID: 3835486 • Letter: H
Question
Help me to do on File in / out for C++ assignment, can you someone teach me how to do this assignment on Xcode (Mac laptop).
*Please all step and print the output that the coding is working.
This is assignment question:
Create a file with 10 records: Last Name, First Name, City, State, zip.
read the file records into an array
Sort the array by lastname
Write out the sorted array to a new file.
This is my 10 record txt file:
Lee Mike SanFrancisco California 94112
Tu Catherine SanMateo California 94010
Power Dan Albany NewYork 12084
Rosales Ryan Houston Texas 77021
Chen Anna MiamiBeach Florida 33140
You Carrie Kailua Hawaii 96725
Huang Stanley RedwoodCity California 94061
Chow Jacky Annapolis Maryland 21411
Catherine Alicia Topeka Kansas 66610
Li Panda CarsonCity Nevada 89706
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void buubleSort(string l[], string f[], string c[], string s[], int z[], int n) {
string tempf, templ, tempc, temps;
int tempz;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(l[j-1] > l[j]){
//swap the elements!
templ = l[j-1];
l[j-1] = l[j];
l[j] = templ;
tempf = f[j-1];
f[j-1] = f[j];
f[j] = tempf;
tempc = c[j-1];
c[j-1] = c[j];
c[j] = tempc;
temps = s[j-1];
s[j-1] = s[j];
s[j] = temps;
tempz = z[j-1];
z[j-1] = z[j];
z[j] = tempz;
}
}
}
}
int main() {
ifstream inputFile;
inputFile.open("data.txt");
ofstream outputFile;
outputFile.open ("sortfile.txt");
string lastName, firstName, city, state;
int zipcode;
string l[10], f[10], c[10], s[10];
int z[10];
int i=0;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> lastName>> firstName>> city>> state>>zipcode;
l[i]=lastName;
f[i]=firstName;
c[i]=city;
s[i]=state;
z[i]=zipcode;
i++;
}
}
buubleSort(l,f,c,s,z,10);
for(int i=0;i<10;i++){
outputFile<<l[i]<<" "<<f[i]<<" "<<c[i]<<" "<<s[i]<<z[i]<<endl;
}
cout<<"File has been written"<<endl;
inputFile.close();
outputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
File has been written
data.txt
Lee Mike SanFrancisco California 94112
Tu Catherine SanMateo California 94010
Power Dan Albany NewYork 12084
Rosales Ryan Houston Texas 77021
Chen Anna MiamiBeach Florida 33140
You Carrie Kailua Hawaii 96725
Huang Stanley RedwoodCity California 94061
Chow Jacky Annapolis Maryland 21411
Catherine Alicia Topeka Kansas 66610
Li Panda CarsonCity Nevada 89706
sortfile.txt
Catherine Alicia Topeka Kansas66610
Chen Anna MiamiBeach Florida33140
Chow Jacky Annapolis Maryland21411
Huang Stanley RedwoodCity California94061
Lee Mike SanFrancisco California94112
Li Panda CarsonCity Nevada89706
Power Dan Albany NewYork12084
Rosales Ryan Houston Texas77021
Tu Catherine SanMateo California94010
You Carrie Kailua Hawaii96725
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.