954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array help needed

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);
}

cproud21
Light Poster
42 posts since Sep 2008
Reputation Points: 4
Solved Threads: 0
 

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...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
#include<string>
shasha821110
Junior Poster
123 posts since Jan 2009
Reputation Points: 10
Solved Threads: 2
 

Hi,

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

rudasi
Light Poster
35 posts since Feb 2009
Reputation Points: 8
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You