Question

Read me first
You are given an incomplete code. Your first assignment is to read this code carefully, and understand what it does.
Accompanying this code is a text file named countries.txt. This file contains all countries in the world along with their area (as found on Wikipedia ). The first entry in this file is an integer (the number of countries it contains). Following that is a sequence of lines containing the country name (with no spaces—e.g. ‘United States’ becomes ‘United‐States’—to simplify parsing) and an integer representing the country area.
The main structures and functions in this program are:
// a simple structure used to hold a country ‐ includes a name (as char*)
// and its area is Km^2 (as int) struct country {
char* name;
int area;
};

// reads a number of countries from the given 'filename' (must be in working
// directory)
// populates the given array pointer (i.e. 'countries') // the number of countries read is returned as int int read(char* filename, country* &countries);

// prints the list of countries
void print(int numOfCountries, country* countries);

// shuffles the current list of countries
void shuffle(int numOfCountries, country* countries);

// sorts the given list of countries by area ‐ using bubble sort ‐ ascending void bubble_sort(int numOfCountries, country* countries);
// sorts the given list of countries by area ‐ using quick sort ‐ ascending void quick_sort(int numOfCountries, country* countries);
// searches the given list of countries to find the one with the given name – using // linear search
// returns the index of the found country (‐1 if not found)
// must be non‐recursive (for efficiency)
int linear_search(int numOfCountries, country* countries, char* name_key);
// searches the given list of countries to find the one with the given area – using // binary search
// (assume the array is sorted by area, in ascending order) // returns the index of the found country (‐1 if not found)
// must be non‐recursive (for efficiency)
int binary_search(int numOfCountries, country* countries, int area_key);
// runs the bubble_sort and the quick_sort with different parameters to produce
// timing results which can be then used to produce a graph void benchmark(int numOfCountries, country* countries);
The underlined functions need to be implemented by you (see below for more instructions).

HW2 Bubble sort and Quick sort and Linear search and Binary search.
Implement the following functions in the given template (search for ‘TODO’ in the code):
• Bubble sort (search for an empty function bubble_sort)
• Quick sort (search for an empty function quick_sort)
• Linear search (search for an empty function linear_search)
• Binary search (search for an empty function binary_search)
Sample executions:

Welcome to this interactive workshop! For instructions, type 'help'.
help
Valid commands are:
- 'read': read from a file (to be specified)
- 'print' print out current data
- 'shuffle' shuffles the data
- 'bubble' perform bubble sort on current data
- 'quick' perform quick sort on current data
- 'linear': perform linear search on current data (key to be specified)
- 'binary': perform binary search on current data (key to be specified)
- 'exit' exits this interactive workshop
read
Please enter file name (must be in working directory): countries.txt
5 countries read!
print
0 - Russia   -    17098242
1 - Canada -      9889000
2 - China   -      9388211   
3 - United-States-of-America -      9147420   
4 - Brazil -      8515767

shuffle
Shuffled!

print
    0 - China -      9388211         
    1 - Russia -    17098242
    2 - Canada -      9889000                                 
    3 - United-States-of-America   -      9147420         
    4 - Brazil -      8515767

quick
Sorted with Quick sort!

print
    0 - Russia   -    17098242
    1 - Canada -      9889000                              
    2    -   United-States-of-America -      9147420   
    3 - China -      9388211
    4 - Brazil -      8515767

exit
Good bye!

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

void main(int argc, char* argv[])
{
country* countries = new country[0]; // declare countries array (initially empty)
int n = 0; // contains the number of entries in countries (initially 0)

cout << "Welcome to this interactive workshop! For instructions, type 'help'." << endl;

char* command = new char[100];
while (true)
{
cin >> command;

if (strcmp(command, "help") == 0)
{
cout << "Valid commands are:" << endl;
cout << "- 'read': read from a file (to be specified)" << endl;
cout << "- 'print' print out current data" << endl;
cout << "- 'bubble' perform bubble sort on current data" << endl;
cout << "- 'quick' perform quick sort on current data" << endl;...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Graph Theory Problems in Python
Homework Solution
$73.00
Computer Science
Graph Theory
Python Programming
Collections
Cities
Spirit Airlines
Breadth First Search
Dijkstra's Algorithm
Text Files
Implementations
Benchmarking Bellman-Ford's Algorithm
Homework Solution
$38.00
Bellman Ford
Algorithm
Graph
Single Source
Shortest Path
Critical Operation
Big-O
Analysis
Documentation
Test Plan
Complexity
Benchmark
Approach
Improvement
Dijkstra
Relaxation
Edge
Node
Lessons Learned
Greedy Strategy Programming Problem
Homework Solution
$10.00
Computer Science
Programming
Greedy Strategy
Algorithms
Company
Workers
Properties
Sets
Shifts
Intervals
Variables
Statements
Sequences
Complexity
Finish Time
Input
Output
Get help from a qualified tutor
Live Chats