Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C
#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;
}


What I have tried:

Error:
 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)'
Posted
Updated 20-Sep-18 23:11pm
v2

Hope this will help you.

#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(array, 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 = 0.0f;
	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_RATE[j] < MAX_RATE[index_of_smallest])
			{
				index_of_smallest = j;
			}
		}
		swap_values(MAX_RATE[index_of_smallest], MAX_RATE[i]);
	}
}

double findMedian(double MAX_RATE[], int numRates)
{
	// sort the array
	sort_array(MAX_RATE, MAX_RATES + numRates);

	// check for even case
	if (numRates % 2 != 0)
		return (double)MAX_RATE[numRates / 2];

	return (double)(MAX_RATE[(numRates - 1) / 2] + MAX_RATE[numRates / 2]) / 2.0;
}
 
Share this answer
 
Comments
Dave Kreskowiak 21-Sep-18 7:53am    
Doing someones work for them does not help them.

You never explained what was wrong and what needed to be fixed. The other answers were much better than yours.
Look at the error message:
61:55: error: expected primary-expression before ']' token

Line 61, column 55 - it expects something before the closing square bracket.
I'm guessing that it's this line:
cout << "Median rate is " << findMedian(MAX_RATES[], numRates) << endl;

And the
C#
MAX_RATES[]
needs an index value between the "[" and "]" array index indicators..

Do the same thing with the other messages - they all give you a line and column number, so use your IDE or editor to find out exactly which line and column they are referring to. That should give you a good idea what you typed wrong!
 
Share this answer
 
Quote:
Can someone please help me with this code? I don't know why it isn't working?

Learn to help yourself ! The compiler gives you tons of hints, learn to use them.
Quote:
In function 'int main()':
61:55: error: expected primary-expression before ']' token

In function main() line 61 position 55
C++
cout << "Median rate is " << findMedian(MAX_RATES[], numRates) << endl;
                                                  ^ this char

The compiler don't like it.
- Check what you are doing. '[]' is used with arrays.
- What is MAX_RATES ?
C++
const int MAX_RATES = 20;

MAX_RATES is a global constant, not an array, the compiler don't understand what you try to do.
Nota: It is always a bad idea to use the same name for many things with a mixing of local and global defines, at least as a beginner.

Quote:
114:28: error: invalid types 'const int[int]' for array subscript
114:59: error: invalid types 'const int[int]' for array subscript

C++
if (MAX_RATES[j] < MAX_RATES[index_of_smallest])

Here again, you use the integer constant like an array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900