What you get: A start on a main.cpp file and a file with sets of three fields per record: country name as a series of non-blank characters, population and area in km^2 as integers. You can expect no more than 250 records.
What you must do: You must follow the grading/style rubric and guide from the website. . All non-integer numbers must be stored and displayed rounded to two decimal digits of precision. Your program must not fail, based on unexpected data values as long as the data is of the type specified above.
What are examples of unexpected data values? How could you resolve them? How could you deal with missing data? For this assignment, your program must read the input file only once during each execution. Sort all provided records by population density, and by country name when densities are the same using an insertion sort as specified in the algorithm below.
You must traverse (read) the struct array no more than one time to accomplish these tasks and display in the order given:
Part 2 NOTES: A struct is similar to a class, but typically without methods and all data is public. Note that CountryStats is a global definition, but the variable countries is local. A global definition is outside of any classes or functions and can be used anywhere. The arrays above have statically allocated memory (allocated at compile time), fixed in the given size.
You must use a char array for each country name, ints for the population and area and double for density. You must use an array of structs to store the records.
struct CountryStats { // must be a global definition
char countryName[MAXNAMELENGTH];
int population;
int area; // km^2
double density;
};
CountryStats countries [MAXRECORDS]; // must be a local declaration
You must use char arrays and the functions found in string.h (including iostream is usually sufficient). In many ways, arrays of char are similar to strings.
A function prototype having a parameter such as (char *s1) is the same as (char s1 []). The * is a pointer, a pointer to a char which is the same as an address, which is how arrays are defined. Note that all char arrays used as strings must have ' ' as their last character so that output routines work properly. The >> operator does this for you.
The == operator does not work for zero terminated cstrings; so, to compare text values, use strcmp function (uses ASCII values) to see if two strings (char arrays) are equal (strcmp returns zero), less than (returns a value < 0), or greater than (returns a value > 0) each other. For example:
if (strcmp (name1, name2) < 0) {
cout << name1 << " is alphabetically less than " << name2;
}
Other things you can do, assuming you have the declaration: CountryStats temp; You can do things like
infile >> temp. countryName ; // read whole thing as one would with string
strcpy(countries [i]. countryName, temp. countryName); // acts like an assignment into the struct array
countries [i] = temp; // assigning a struct to a struct of the same type copies correctly
You must do this entire program in one .cpp file using only main and other functions (do not use classes). (Java students: this means to pretend main and other functions used are wrapped in a class, similar to the class containing main in your Java programs.
Other functions in this program would be like additional methods of that class.) All variables that would have been in the class will be local to main and must be passed to other functions as parameters. Functions still have local vars as needed.
You must code the read and insertion sort of your program as specified here: For the insertion sort, your code must read one line of data into a temporary location of type CountryStats and then insert it into its correct position in the array. Continue until all are records are read and sorted. The reading and sorting of each record happens at essentially the same time. You must start with an empty array then insert each country into its proper place right after you read it from the file. Thus the invariant is that your array of structs is always in sorted order, after 0 records have been read, after 3 records have been read and after all records have been read. (Reading into the array is not done in a separate loop as it may be in bubble sort sample code.)
The following algorithm (pseudocode) shows how to insert one item:
Read country name, population and area into a temporary location
Determine and store the proper density for the newly read data
Loop from the high index end of the filled part of the array down toward the (low index) beginning {
if (new density < current density) {
copy/move current data down toward beginning
}
else if (new/current density are equal and new name < current name) {
copy/move current data down toward beginning
}
else {
found right place, leave this search loop
}
}
now that you have made room, insert (copy) new name into correct sorted position
As always expected when programming, comment clearly and thoroughly. Clearly state any assumptions you make in the beginning comment block of the appropriate place, e.g., function definition. For example, you can assume the data is properly formatted as described, and document this when you input the data. Pre and post conditions are recommended. Functions must be separated using doxygen comment separators.
Non object‐oriented programs are function driven (as opposed to message driven). Functions perform actions instead of objects invoking methods.. You must use lab1densitymain.cpp (see website) as the name and basis for the main program you submit. For extra credit: measure and explain differences between elapsed time reading/storing data and array traversal/calculations.