Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Expected Output:

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.184142 3 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than 0 4 Enter the old and new consumer price indices: Inflation rate is 0.378648 5 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.223225 6 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than 0 7 Enter the old and new consumer price indices: Inflation rate is -0.50658 8 Try again? (y or Y): Enter the old and new consumer price indices: Error: CPI values must be greater than 0 9 Enter the old and new consumer price indices: Inflation rate is 0.284456 10 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.489679 11 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.348243 12 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.368315 13 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.0926804 14 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.319909 15 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.190802 16 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.13949 17 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.384759 18 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.14665 19 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.1744 20 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.276358 21 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.195154 22 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.276591 23 Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.231967 24 Try again? (y or Y): Average rate is 0.0496408 25 Median rate is 0.13354

Here's my code it does not have the correct output it goes into an infinite loop please help me fix that.

#include using namespace std;

// Add a function to get the CPI values from the user and validate that they are greater than 0. void getCPIValues(float &old_cpi, float &new_cpi){ do{ cout << "Enter the old and new consumer price indices: ";

cin >> old_cpi >> new_cpi;

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

{

cout << "Error: CPI values must be greater than 0 ";

}

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

}

//Function that takes two double parameters and swap //their values void swap_values(double *ay, double *yx) { double temp = *ay; *ay = *yx; *yx = temp; }

//Function to sort the array //Bubble Sort void sort_array(double arr[], int number) { int i, j; for (i = 0; i < number-1; i++) // Last i elements are already in place for (j = 0; j < number-i-1; j++) if (arr[j] > arr[j+1]) // Calling swap_values function to // swap elements of array swap_values(&arr[j], &arr[j+1]); }

// Declare and implement a function called swap_values that takes two double parameters like the one we defined in class. // 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). // 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.

// double findMedianRate(double arr[], int number){ sort_array(arr, number); if(number % 2!=0){ return (double)arr[number/2]; } else{ return (double)( arr[(number-1)/2] + arr[number/2] ) / 2.0; } }

// if the number of rates is even, then the median rate is calculated as the average of the two rates in the middle. double average_median_rate(double rates[], int number) { double sum = 0; for (int i=0; i

//main function int main(){ // Declare a constant called MAX_RATES and set it to 20. const int MAX_RATES = 20; // array of type double to store inflation rates double double_inflationRates[MAX_RATES]; // Add code to main that inserts the computed inflation rate into the next position in the array. int inserts = 0; char ch='Y'; double inflationRate = 0.00; float old_cpi, new_cpi; // while user chooses 'y' or 'Y' while(ch=='Y' or ch=='y'){ // input CPI values getCPIValues(old_cpi, new_cpi); // calculate inflation // inflation = ((currentCPI - oldCPI)/oldCPI) * 100 inflationRate = (double)( ( new_cpi - old_cpi ) / old_cpi ) * 100; // to avoid overflow of array if(inserts < MAX_RATES){ // insert inflation in the array double_inflationRates[inserts++] = inflationRate; }

// print the inflation rate cout<<"Inflation rate is "<>ch; } // print average of all the inflation rates cout<<"Average rate is "<

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+2. What are the criteria for determining its quality?

Answered: 1 week ago