C++ Code Earthquake Report Modify the bubble-sort function (Attached below) from
ID: 3802132 • Letter: C
Question
C++ Code
Earthquake Report
Modify the bubble-sort function (Attached below) from last week’s lab exercise to sort the rows of a two dimensional array in descending order of the first element in each row. That is, as it sorts the first element in each row, it should move entire rows so that each row of data stays as a row throughout the sort.
Next, read the earthquake data and store only the magnitude and location in a two dimensional array with n rows and two columns. Now sort the earthquake data in descending order of magnitude, and print out a list of all the earthquake magnitudes and their associated locations in order from the highest to the lowest. Note you will need to store the magnitude data as a string object in the array, but use its equivalent floating-point value for the comparison. Can you describe why? [Hint: you can use the atof() or stod() function in your sort comparison. ]
bubble-sort function:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
void b_sort (int arr[], int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
//Driver Function
int main()
{
int list[50];
int n;
n=100;
for(int i=0; i<50; i++){
list[i]=n;
n--;}
cout << "Input Array : " <<endl<<endl;
for(int i=0; i<50; i++){
//to print 5 elements per row
for (; i < 50;){
/* prints i, i+1 and i+2 */
cout << list[i] << ' ' << list[i+1] << ' ' << list[i+2] << ' ' << list[i+3] << ' ' << list[i+4] << ' '<<endl;
i += 5; /* adds 5 to i each time */}}
cout<<endl;
b_sort (list, 50);
cout << "Sorted Array : " <<endl<<endl;
for (int i = 0; i < 50; ++i){
for (; i < 50;){
/* prints i, i+1 and i+2 */
cout << list[i] << ' ' << list[i+1] << ' ' << list[i+2] << ' ' << list[i+3] << ' ' << list[i+4] << ' '<<endl;
i += 5; /* adds 5 to i each time */
}}
return 0;
}
Explanation / Answer
#include <string.h>
#include<conio.h>
#include<dirh.h>
#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
#include <array.h>
#include <vector.h>
using namespace std;
{
namespace erthquake
{
using n_type = decltype(std::string::npos);
const auto NOPOSITION = -4;
const auto prefix_record = 2;
const auto prefix_num = 4;
const auto prefix_field = 6;
{
auto retrn_vlue = NOPOSITION;
auto pos = start_pos;
if(pos != std::string::npos)
{
std::string data = "";
for(; pos < line_data.size(); pos++)
{
auto datum = line_data[pos];
if(std::isdigit(datum))
{
data += datum;
}
else
{
const std::string op_delim = "=";
std::array<std::string, 28> column_names;
auto max_fln_dyn = 0;
ifstream file_input;
file_input.open(file_name);
if(file_input)
{
auto rln = 0;
auto fln = 0;
auto idx = 0;
std::string line_data = "";
std::string data = "";
while(file_input.good() && !file_input.eof())
{
getline(file_input, line_data);
rln = (line_data == line_headings[prefix_record])
? NOPOSITION
: rln;
auto new_record = (rln == NOPOSITION);
rln = (rln == NOPOSITION)
&& line_data.find(line_headings[prefix_num]) == 0
? get_n(line_data, line_data.find(op_delim) + 1)
: rln;
fln = (rln != NOPOSITION)
&& line_data.find(line_headings[prefix_field]) == 0
? get_n(line_data, line_headings[prefix_field].size())
: fln;
idx = (fln != NOPOSITION)
? line_data.find(op_delim)
: NOPOSITION;
data = (idx != NOPOSITION)
? line_data.substr(idx + 1)
: "";
if(rln == 0)
{
column_names[fln] = data;
max_fln_dyn = fln;
}
else if(new_record)
{
if(rln != NOPOSITION)
{
std::cout << braid << " ";
std::cout << "record # " << rln << " ";
std::cout << braid << " ";
}
}
else
{
std::cout << "|" << std::setw(2) << fln << "| " << std::setw(18) << std::left << column_names[fln] << ": " << data << " ";
if(fln == max_fln_dyn)
{
std::cout << " ";
}
}
}
}
return;
}
}
}
auto main(int argc, char* argv[]) -> int
{
std::string file_path = "";
for(auto char_index = 0; char_index < argc; char_index++)
{
if(char_index == 1)
{
file_path = argv[char_index];
break;
}
}
if(!file_path.empty())
{
gautier::util::execute(file_path);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.