Question: In a C program, you have a data structure with satellite data defined as follows: struct my_data { float height; char *name; double computed value;
In a C program, you have a data structure with satellite data defined as follows:
struct my_data {
float height;
char *name;
double computed value;
};
Because this is satellite data, you must access it through some other data structure. (That is, you don't have an array of just these satellite data.) Your most convenient access is through iterator functions: you can call first() to get a pointer to the first my_data structure and next() to get a pointer to the next structure. Both functions return a pointer to a my_data structure or else NULL if no more are available. Due to other processes happening at the same time, you are not guaranteed that two iterations will return the data in the same order, but each time you iterate through the data you are guaranteed to get each item exactly once.
The height and name have already been filled in. You want to run an O(n log n) time and O(n) space algorithm A on that satellite data to compute computed value. The algorithm A only needs the height, it ignores the name.
Sadly, A is very difficult to implement, but you have access to an implementation, impressive_A(), of A that expects an array of floats and returns an array of doubles. You may assume that impressive_A() is O(n).
Describe an algorithm (pseudo-code and high-level description, not C) that uses first and next, the above structure definition, and the function impressive_A() to fill in the computed value members. Analyze the time and space complexity of your algorithm, including the fact that your algorithm must call impressive_A().
In summary, you must copy the height data to an array, run impressive_A(), and then copy the doubles back to the satellite data. (Hint: You will need an auxiliary data structure to remember the correspondence between the satellite data you read on your first iteration and the array indices so that you can write the computed_values back on a second iteration. Analyze the time and space complexity of your solution in terms of O(impressive_A()). If you need to "invent" functions whose implementation you don't know, give the best time bounds you can for those invented functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
