| | |
Array help needed
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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);
}•
•
Join Date: Jan 2009
Posts: 123
Reputation:
Solved Threads: 2
c++ Syntax (Toggle Plain Text)
#include<string>
![]() |
Similar Threads
- Help needed filling array with unique random numbers (C++)
- C++ Array help needed (C++)
- Problem in Reading Array Variable (C)
- Scanning an inputted array of words, and separating each word into a new array? (C)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: error in accessing a function in header file
- Next Thread: Urgent: Need Help Writing Simple Program for length and width
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






