943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 907
  • C++ RSS
Feb 17th, 2009
0

Array help needed

Expand Post »
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);
}
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Feb 17th, 2009
0

Re: Array help needed

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Feb 17th, 2009
0

Re: Array help needed

c++ Syntax (Toggle Plain Text)
  1. #include<string>
Reputation Points: 10
Solved Threads: 2
Junior Poster
shasha821110 is offline Offline
123 posts
since Jan 2009
Feb 17th, 2009
0

Re: Array help needed

Hi,

I agree with ArkM, passing the month array at the same time and swapping it would be a good option.
Reputation Points: 8
Solved Threads: 2
Light Poster
rudasi is offline Offline
35 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error in accessing a function in header file
Next Thread in C++ Forum Timeline: Urgent: Need Help Writing Simple Program for length and width





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC