Guest User

loop excersize

a guest
May 8th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int Months = 12;
  5. const int Years = 3;
  6.  
  7. const char *months[Months] =
  8. {
  9.     "Jan.", "Feb.", "March", "April", "May", "June",
  10.     "July", "August", "Sept.", "October", "Nov.", "Dec."
  11. };
  12.  
  13. int main() {
  14.     cout << "This program prompts the user to input sales for an entire year, by month.";
  15.     cout << "\nThe data will be stored in a multi-dimensional array, with a month string";
  16.     cout << "\nrow and a double sales row.";
  17.  
  18.     double salesInput[Months];
  19.     double totalSales[Years][Months];
  20.     int yearCounter = 1;
  21.     double sumYear1 = 0, sumYear2 = 0, sumYear3 = 0, totalSum = 0;
  22.  
  23.     do {
  24.         cout << "\n\nPlease input the sales for year " << yearCounter;
  25.         cout << "\nPlease enter all 12 sales with a space in between each month" << endl;
  26.  
  27.         for (int i=0; i<Months; i++)
  28.             cin >> salesInput[i];
  29.  
  30.         for (int i=0; i<Months; i++)
  31.             totalSales[yearCounter-1][i] = salesInput[i];
  32.  
  33.         yearCounter++;
  34.  
  35.     } while (yearCounter<4);
  36.  
  37.     cout << "\n\nYour current monthly sales are for year 1:\n";
  38.  
  39.     for (int month=0; month<Months; ++month)
  40.         cout << months[month] << "\t";
  41.     cout << endl;
  42.     for (int i=0;i<Months; i++)
  43.         cout << totalSales[0][i] << "\t";
  44.  
  45.     cout << "\n\nYour current monthly sales are for year 2:\n";
  46.  
  47.     for (int month=0; month<Months; ++month)
  48.         cout << months[month] << "\t";
  49.     cout << endl;
  50.     for (int i=0;i<Months; i++)
  51.         cout << totalSales[1][i] << "\t";
  52.  
  53.     cout << "\n\nYour current monthly sales are for year 3:\n";
  54.  
  55.     for (int month=0; month<Months; ++month)
  56.         cout << months[month] << "\t";
  57.     cout << endl;
  58.     for (int i=0;i<Months; i++)
  59.         cout << totalSales[2][i] << "\t";
  60.  
  61.     cout << endl;
  62.  
  63.     for (int i=0; i<Years; i++) {
  64.         for (int j=0; j<Months; j++) {
  65.             if (i==0)
  66.                 sumYear1 = totalSales[i][j] + sumYear1;
  67.             if (i==1)
  68.                 sumYear2 = totalSales[i][j] + sumYear2;
  69.             if (i==2)
  70.                 sumYear3 = totalSales[i][j] + sumYear3;
  71.         }
  72.     }
  73.  
  74.     cout << "\nThe total for year 1 is: " << sumYear1 << " Dollars";
  75.     cout << "\nThe total for year 2 is: " << sumYear2 << " Dollars";
  76.     cout << "\nThe total for year 3 is: " << sumYear3 << " Dollars";
  77.     cout << "\nThe total sum for all three years is " << sumYear1 + sumYear2 + sumYear3 << " Dollars" << endl;
  78.     system("pause");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment