Может ли кто-нибудь помочь мне с этим кодом? Я не знаю, почему это не работает?
#include <iostream> using namespace std; /* * InflationRate - calculates the inflation rate given the old and new consumer price index * @param old_cpi: is the consumer price index that it was a year ago * @param new_cpi: is the consumer price index that it is currently * @returns the computed inflation rate or 0 if inputs are invalid. */ void getCPIValues(float& old_cpi, float& new_cpi); double InflationRate(float old_cpi, float new_cpi); void swap_values(double& a, double& b); void sort_array (double MAX_RATE[], int numRates); double findMedian(double MAX_RATES[], int numRates); const int MAX_RATES = 20; int main() //C++ programs start by executing the function main { // #1: declare two float variables for the old consumer price index (cpi) and the new cpi float old_cpi, new_cpi, infrt, sum = 0, avg; char again; int crt = 0; double array[MAX_RATES]; int numRates; do { // #2: Read in two float values for the cpi and store them in the variables getCPIValues(old_cpi, new_cpi); // #3: call the function InflationRate with the two cpis // #4: print the results infrt = InflationRate(old_cpi, new_cpi); if (infrt != 0) { sum += infrt; crt++; } cout << "Inflation rate is " << infrt << endl; for (int i = 0; i < array[MAX_RATES]; i++) cin >> numRates; //int numRates = sizeof(array)/sizeof(MAX_RATES[]); // BONUS #1: Put the logic in #2-4 in a loop that asks the user to enter 'y' if there's more data cout << "Try again? (y or Y): "; cin >> again; } while((again == 'Y') || (again == 'y')); // BONUS #2: Keep a running total of the valid inflation rates and the number of computed rates to calculate the average rate. // Print the results after the loop // calculate average avg = sum/crt; cout << "Average rate is " << avg << endl; cout << "Median rate is " << findMedian(MAX_RATES[], numRates) << endl; return 0; } // function ask the user to input the old and new consumer price indices // check if the numbers 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)); } // double InflationRate(float old_cpi, float new_cpi) // precondition: both prices must be greater than 0.0 // postcondition: the inflation rate is returned or 0 for invalid inputs double InflationRate(float old_cpi, float new_cpi) { if ((old_cpi <= 0) || (new_cpi <= 0) || old_cpi == new_cpi) { return 0; } else // Implement InflationRate to calculate the percentage increase or decrease // Use (new_cpi - old_cpi) / old_cpi * 100 return ((new_cpi - old_cpi) / old_cpi * 100); } void swap_values(double& a, double& b) { double temp; a = b; b = temp; } void sort_array (double MAX_RATE[], int numRates) { int index_of_smallest; for (int i = 0; i < numRates -1; i++) { index_of_smallest = i; for (int j = i+1; j < numRates; j++) { if (MAX_RATES[j] < MAX_RATES[index_of_smallest]) { index_of_smallest = j; } } swap_values(&MAX_RATES[index_of_smallest], &MAX_RATES[i]) } } double findMedian(double MAX_RATES[], int numRates) { // sort the array sort_array(MAX_RATES, MAX_RATES+numRates); // check for even case if (numRates % 2 != 0) return (double)MAX_RATES[numRates/2]; return (double)(MAX_RATES[(numRates-1)/2] + MAX_RATES[numRates/2])/2.0; }
Что я уже пробовал:
Ошибка:
In function 'int main()': 61:55: error: expected primary-expression before ']' token In function 'void sort_array(double*, int)': 114:28: error: invalid types 'const int[int]' for array subscript 114:59: error: invalid types 'const int[int]' for array subscript 119:49: error: invalid types 'const int[int]' for array subscript 119:64: error: invalid types 'const int[int]' for array subscript In function 'double findMedian(double*, int)': 126:35: error: invalid conversion from 'double*' to 'int' [-fpermissive] 105:6: note: initializing argument 2 of 'void sort_array(double*, int)'