Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program that takes runners names and times from a text file and p

ID: 3640309 • Letter: I

Question

I am writing a program that takes runners names and times from a text file and putting them into parallel arrays. After retrieving the data I then bubble sort the arrays, and display the results in the correct lanes. The lanes should be ordered from fastest time to slowest in lane assignments 4, 5, 3, 6, 2, 7, 1, and 8. I have two text files containing one with 20 names and times and the other with 6 names and times. My program works fine when i have more then 8 runners and displays them properly in their lanes. My problem is when I use my text file with less then 8 runners and times any unused lane should be printed with "open". Instead of open I am getting random items from memory. So my question is what I need to do to fix the program so it will show "open" in the empty lanes when I have less then 8 runners, but still show all the runners when I have more then 8.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
ifstream input;
char name[20][30];
double time[20];
const int lanes[] = {-1, 6, 4, 2, 0, 1, 3, 5, 7};
int i, x;
char tempname[30];
double temptime;

input.open("lakeside.txt");
int count = 0;

while (!input.eof())
{
input.getline(name[count], 29);
input >> time[count] >> ws;
count++;
}
input.close();

for(i=0; i<20-1; i++)
for(x=1; x<20-i; x++)
if(time[x]<time[x-1])
{
temptime = time[x];
strcpy(tempname, name[x]);
time[x] = time[x-1];
strcpy(name[x], name[x-1]);
time[x-1] = temptime;
strcpy(name[x-1], tempname);
}
for(i=1; i<9; i++)
{
int spot = lanes[i];
if(spot < count)
{
cout << "Lane #" << i << " " << name[spot] << " " << time[spot];
cout << endl;
}
else
cout << "Lane #" << i << " " << "Open" << endl;
}

return 0;
}


Explanation / Answer

please rate - thanks

I believe this should solve your problem, if not let me know


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
ifstream input;
char name[20][30];
double time[20];
const int lanes[] = {-1, 6, 4, 2, 0, 1, 3, 5, 7};
int i, x;
char tempname[30];
double temptime;

input.open("lakeside.txt");
int count = 0;

while (!input.eof())
{
input.getline(name[count], 29);
input >> time[count] >> ws;
count++;
}
input.close();

for(i=0; i<count-1; i++)
for(x=1; x<count-i; x++)
if(time[x]<time[x-1])
{
temptime = time[x];
strcpy(tempname, name[x]);
time[x] = time[x-1];
strcpy(name[x], name[x-1]);
time[x-1] = temptime;
strcpy(name[x-1], tempname);
}
for(i=1; i<9; i++)
{
int spot = lanes[i];
if(spot < count)
{
cout << "Lane #" << i << " " << name[spot] << " " << time[spot];
cout << endl;
}
else
cout << "Lane #" << i << " " << "Open" << endl;
}
return 0;
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote