I have the following array that prints out the amount of rainfall for each month, and then uses a bubble sort to sort it in ascending order... How can I display the name of the month along with the value that is already displayed?

#include <iostream>
using namespace std;

void bSort(int[], int);


int main()
{	
	string months[12] = {"Jan", "Feb", "Mar", "Apr", "May","Jun","Jul","Aug", "Sep","Oct", "Nov", "Dec"};
	int rain[12];
	double total = 0;
	double average;
	double min = 0;
	double max = 0;
	
	
	for (int i = 0; i <=11; i++)
	{
		cout << "Input the Amount of rainfall in " << months[i] << ": ";
		cin >> rain[i];
		while (rain[i] < 0)
		{
			cout << "Cannot have negative rainfall! Please re-enter:";
			cin >> rain[i];
		}
	}
	for(int i = 0; i <= 11; i++)
	{
		total = total + rain[i];
	}
	
	average = total / 12;
	
	min = rain[0];
	for(int i = 1; i < 12; i++)
	{
		if(rain[i] < min)
			min = rain[i];
	}
	
	max = rain[0];
	for(int i = 1; i < 12; i++)
	{
		if(rain[i] > max)
			max = rain[i];
	}
	
	cout << "Total Rainfall: " << total << endl;
	cout << "Average Rainfall: " << average << endl;
	cout << "The lowest monthly rainfall is " << min << endl;
	cout << "The highest monthly rainfall is " << max << endl;
	

		
		
		bSort(rain, 12);	
		for(int i = 0; i < 12; i++)
			{
			cout << rain[i] << endl;
			}
	
	return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;

void bSort(int intArray[], int size)
{
	int rain;
	bool swap;
	
	do
	{
		swap = false;
		for(int i = 0; i < (size-1); i++)
		{
			if(intArray[i] > intArray[i+1])
			{
				rain = intArray[i];
				intArray[i] = intArray[i+1];
				intArray[i+1] = rain;
				swap = true;
			}
		}
	}while(swap);
}

Probably the simplest (and dirty) method: pass month array into the bSort function and swap its elements at the same time as rain elements swapped...

Hi,

I agree with ArkM, passing the month array at the same time and swapping it would be a good option.

This article has been dead for over six months. Start a new discussion instead.