p7new.cpp
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int dow = 6;
int day = 1 - dow;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
for(int row = 0; row < 6; row++)
{
for(int col = 0; col < 7; col++)
{
if(day < 1 || day > 31) {
cout << setw(4) << "";
}
else {
cout << setw(4) << day;
}
day++;
}
cout << endl;
}
return 0;
}
dice.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
const int NUM_ROLLS = 1000000;
const int SCALE = NUM_ROLLS/333;
int tally[13] = { 0 };
srand(time(NULL));
for(int i = 0; i < NUM_ROLLS; i++)
{
int r1 = rand() % 6 + 1;
int r2 = rand() % 6 + 1;
int sum = r1+r2;
//cout << r1 << " + " << r2 << " = " << sum << endl;
tally[sum]++;
}
for(int i = 2; i <= 12; i++)
{
cout << setfill(' ');
cout << setw(3) << i
<< ": " << setw(9) << tally[i];
cout << setfill('*') << setw(tally[i]/SCALE) << "";
cout << endl;
}
return 0;
}
funcmonth.cpp
// factoring out a function
#include <iostream>
using namespace std;
int numDaysInMonth(int m)
{
int days;
switch(m) {
case 2:
days = 28;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
default:
days = 31;
}
return days;
}
int main()
{
int month = 4;
int prevMonth = month-1;
if(prevMonth < 1) {
prevMonth = 12;
}
int days = numDaysInMonth(month);
int prevDays = numDaysInMonth(prevMonth);
cout << prevDays << " then " << days << endl;
return 0;
}