ZornTaov

Untitled

Apr 30th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7.  
  8. void swap(int *x, int *y)
  9. {
  10.     int temp = *y;
  11.     *y = *x;
  12.     *x = temp;
  13. }
  14. void swaps(string *x, string *y)
  15. {
  16.     string temp = *y;
  17.     *y = *x;
  18.     *x = temp;
  19. }
  20. //dir = direction, true = highest first
  21. void bubble(int a[], string *b[], int len, bool dir)
  22. {
  23.     int count = len;
  24.     while(count)
  25.     {
  26.         for(int i = 0; i<count-1;i++)
  27.         {
  28.             if(a[i] < a[i+1] && dir)
  29.                 swap(a[i],a[i+1]);
  30.                 swaps(b[i],b[i+1]);
  31.        
  32.             if(a[i] > a[i+1] && !dir)
  33.                 swap(a[i],a[i+1]);
  34.                 swaps(b[i],b[i+1]);
  35.         }
  36.         count--;
  37.     }
  38. }
  39. void printArray(const int a[], string* b[], int len)
  40. {
  41.     for(int i = 0; i<len;i++)
  42.     {
  43.         cout << a[i] << " inches in " << *(b[i]) << endl;
  44.     }
  45. }
  46. int main()
  47. {
  48.  
  49.     const int SIZE = 12;
  50.     int rainfall[SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0}, counter = 0;
  51.     string month[SIZE] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  52.     string *monthptr[SIZE];
  53.     for(int i = 0; i<SIZE;i++)
  54.     {
  55.         monthptr[i] = &month[i];
  56.     }
  57.     int total = 0, high = 0, low = 9999;
  58.     float avg = 0;
  59.     bool done = false;
  60.     while(!done)
  61.     {
  62.         cout << "Enter rainfall for "<<month[counter]<<": ";
  63.         cin >> rainfall[counter];
  64.         if(rainfall[counter]>=0)
  65.         {
  66.             counter++;
  67.         }
  68.         else
  69.         {
  70.             cout<<"error, non-negitive ammounts please!\n";
  71.         }
  72.  
  73.         if(counter == 12)done=!done;
  74.     }
  75.     bubble(rainfall, monthptr, SIZE, false);
  76.     for(int i = 0; i<12;i++)
  77.     {
  78.         total += rainfall[i];
  79.     }
  80.     avg = total/12.0;
  81.     cout << "Total rainfall: " << total << "\nAverage rainfall: " << avg << endl;
  82.     cout << "Least rainfall: " << *(monthptr[0]) << "\nMost rainfall: " << *(monthptr[11]);
  83.  cout << endl;
  84.     printArray(rainfall, monthptr, SIZE);
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment