Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <stdio.h>
- using namespace std;
- void swap(int *x, int *y)
- {
- int temp = *y;
- *y = *x;
- *x = temp;
- }
- void swaps(string *x, string *y)
- {
- string temp = *y;
- *y = *x;
- *x = temp;
- }
- //dir = direction, true = highest first
- void bubble(int a[], string *b[], int len, bool dir)
- {
- int count = len;
- while(count)
- {
- for(int i = 0; i<count-1;i++)
- {
- if(a[i] < a[i+1] && dir)
- swap(a[i],a[i+1]);
- swaps(b[i],b[i+1]);
- if(a[i] > a[i+1] && !dir)
- swap(a[i],a[i+1]);
- swaps(b[i],b[i+1]);
- }
- count--;
- }
- }
- void printArray(const int a[], string* b[], int len)
- {
- for(int i = 0; i<len;i++)
- {
- cout << a[i] << " inches in " << *(b[i]) << endl;
- }
- }
- int main()
- {
- const int SIZE = 12;
- int rainfall[SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0}, counter = 0;
- string month[SIZE] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
- string *monthptr[SIZE];
- for(int i = 0; i<SIZE;i++)
- {
- monthptr[i] = &month[i];
- }
- int total = 0, high = 0, low = 9999;
- float avg = 0;
- bool done = false;
- while(!done)
- {
- cout << "Enter rainfall for "<<month[counter]<<": ";
- cin >> rainfall[counter];
- if(rainfall[counter]>=0)
- {
- counter++;
- }
- else
- {
- cout<<"error, non-negitive ammounts please!\n";
- }
- if(counter == 12)done=!done;
- }
- bubble(rainfall, monthptr, SIZE, false);
- for(int i = 0; i<12;i++)
- {
- total += rainfall[i];
- }
- avg = total/12.0;
- cout << "Total rainfall: " << total << "\nAverage rainfall: " << avg << endl;
- cout << "Least rainfall: " << *(monthptr[0]) << "\nMost rainfall: " << *(monthptr[11]);
- cout << endl;
- printArray(rainfall, monthptr, SIZE);
- }
Advertisement
Add Comment
Please, Sign In to add comment