Array help needed

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Array help needed

 
0
  #1
Feb 17th, 2009
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);
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Array help needed

 
0
  #2
Feb 17th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 123
Reputation: shasha821110 is an unknown quantity at this point 
Solved Threads: 2
shasha821110 shasha821110 is offline Offline
Junior Poster

Re: Array help needed

 
0
  #3
Feb 17th, 2009
  1. #include<string>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: rudasi is an unknown quantity at this point 
Solved Threads: 2
rudasi's Avatar
rudasi rudasi is offline Offline
Light Poster

Re: Array help needed

 
0
  #4
Feb 17th, 2009
Hi,

I agree with ArkM, passing the month array at the same time and swapping it would be a good option.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC