Question: #include using namespace std; #define MAX_RATES 20 //validate input as positive void getCPIValues(float &old_cpi, float &new_cpi) { do { cout > old_cpi >> new_cpi; if

#include

using namespace std;

#define MAX_RATES 20

//validate input as positive

void getCPIValues(float &old_cpi, float &new_cpi) {

do { cout > old_cpi >> new_cpi;

if (!(old_cpi > 0 && new_cpi > 0))

{

cout

}

} while (!(old_cpi > 0 && new_cpi > 0));

}

void swap_values(double* a, double* b) {

double* t = a; a = b; b = t;

}

//bubble sort

void sort_array(double cpi[MAX_RATES], int n) {

for (int i = 0; i cpi[j + 1]) swap_values(&cpi[j], &cpi[j + 1]); } }

}

int main() {

char ch;

double cpi[MAX_RATES];

int i = 0;

float old_cpi, new_cpi, inf, total = 0;

do { getCPIValues(old_cpi, new_cpi);

// cout

inf = ((new_cpi - old_cpi) / old_cpi) * 100;

total += inf;

if (i

cpi[i++] = inf;

else

break;

cout

cout

cin >> ch;

} while (ch == 'y' || ch == 'Y');

cout

sort_array(cpi, i);

if (i % 2 !=0)

cout

else

cout

return 0;

}

#include using namespace std; #define MAX_RATES 20 //validate input as positive void

This is the error message I get for the code above, please debug the median rate!

This is the question:

Expected Output:

Enter the old and new consumer price indices: 238.343 238.250 Inflation rate is -0.0390204 Try again? (y or Y): y Enter the old and new consumer price indices: 238.250 237.852 Inflation rate is -0.167049 Try again? (y or Y): y Enter the old and new consumer price indices: 237.852 238.123 Inflation rate is 0.113935 Try again? (y or Y): n Average rate is -0.0307116 Median rate is -0.0390204

Directions

Take a breath and do each part, one at a time. Start at the top and work your way down.

- Add a function to get the CPI values from the user and validate that they are greater than 0.

1. Declare and implement a void function called getCPIValues that takes two float reference parameters for the old_cpi and new_cpi.

2. Move the code that reads in the old_cpi and new_cpi into this function.

3. Add a do-while loop that validates the input, making sure that the old_cpi and new_cpi are valid values.

+ if there is an input error, print "Error: CPI values must be greater than 0." and try to get data again.

4. Replace the code that was moved with a call to this new function.

- Add an array to accumulate the computed inflation rates

1. Declare a constant called MAX_RATES and set it to 20.

2. Declare an array of double values having size MAX_RATES that will be used to accumulate the computed inflation rates.

3. Add code to main that inserts the computed inflation rate into the next position in the array.

4. Be careful to make sure the program does not overflow the array.

- Add a function that sorts the values in an array of doubles.

1. Declare and implement a function called swap_values that takes two double parameters like the one we defined in class. It will be used by the sort_array function to swap inflation rates in the array.

2. Declare and implement a function that uses either a selection sort or bubble sort to put the array values into ascending order (i.e. smallest to largest). In order to sort an array, you must be able to move the values around using swap_values.

+ Function parameters: an array of doubles and an int with the number of elements in the arra

+ Your sort function must use the swap_values function defined above to exchange the values in the array during the sort.

+ You can use either a selection sort or a bubble sort in the sort_array function but it must use the swap_values function.

- Add a function called findMedianRate that calculates the median inflation rate using sort above.

1. Declare and implement a function called findMedianRate that takes two parameters and returns a double which will be the median rate.

+ parameters: an array of doubles and an int with the number of elements in the array (e.g. numRates).

2. Sort the array using your sort_array function defined above. Once the array is sorted, use the following logic to calculate the median value:

+ if the number of rates is odd, then the median rate has as many values preceeding it as following it or in other words, it's the one in the middle.

+ if the number of rates is even, then the median rate is calculated as the average of the two rates in the middle.

Debugging Information for IR3 Test4 O Intiation race IS -0.000 Enter tre 014 and new consumer price inuices: 9c9 Median rate is 0.223225 a.out Inflation Rate.cpp Download .zip PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red indicates things in your output that shouldn't be there. The character refers to newlines, so the green character refers a newline you are missing in your output and the red refers to a newline you need to remove from your output. 2 View Submission In IDI 1 Enter the old and new consumer price indices: Inflation rate is 0.640626 2 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.1841424 3 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than od 4 Enter the old and new consumer price indices: Inflation rate is 0.3786484 5 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.2232254 6 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than er 7 Enter the old and new consumer price indices: Inflation rate is -0.506584 8 Try again? (y or Y): Average rate is 0.1103564 Median rate is 0.223786482254 10 9 Debugging Information for IR3 Test4 O Intiation race IS -0.000 Enter tre 014 and new consumer price inuices: 9c9 Median rate is 0.223225 a.out Inflation Rate.cpp Download .zip PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red indicates things in your output that shouldn't be there. The character refers to newlines, so the green character refers a newline you are missing in your output and the red refers to a newline you need to remove from your output. 2 View Submission In IDI 1 Enter the old and new consumer price indices: Inflation rate is 0.640626 2 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.1841424 3 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than od 4 Enter the old and new consumer price indices: Inflation rate is 0.3786484 5 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.2232254 6 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than er 7 Enter the old and new consumer price indices: Inflation rate is -0.506584 8 Try again? (y or Y): Average rate is 0.1103564 Median rate is 0.223786482254 10 9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!