Functions

These are notes for 4 November.

a7sol.cpp

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    double sum = 0.0;
    int count = 0;
    cout << "ENter numbers one per line, or type 'end'.\n";
    while(1)
    {
        string input;
        getline(cin, input);
        if(input == "end")
        {
            break;
        } //if end
        istringstream ss(input);
        double num;
        if(ss >> num)
        {
            sum += num;
            count++;
        }
        else
        {
            cout << "?ignored\n";
        }
    }//while 
    cout << "Thanks for playing.\n";
    cout << "Sum is " << sum << "\n";
    cout << "Average is " << sum/count << "\n";
    return 0;
}

mathfunc.cpp

#include <iostream>
#include <cmath>
using namespace std;
// (1) Prototype the function. This is a "declaration"
// that goes at the TOP of the program, OUTSIDE main.
// Prototype includes (a) return type, (b) name, and
// (c) the types of its parameters.
double twiceroot(double num);
double midpoint(double x, double y);
// void is a place-holder for a function that
// doesn't return any value.
void print_chars(int how_many, char c);

// (2) Define the function. Now that we have a
// prototype, the definition can go anywhere else
// in the code (before or after main).
double twiceroot(double num)
{
    return sqrt(sqrt(num));
}

int main()
{
    // Analogy: Mathematical functions, like   
    // exponents, square root, sine, log, cosine.
    double num = 39;
    // Built-in function called "sqrt".
    cout << sqrt(num) << "\n"; // a function call
    // parameters go inside the parentheses.
    num = 164;
    double answer = sqrt(sqrt(num));
    cout << answer << "\n";
    answer = twiceroot(num); // (3) call my function
    cout << answer << "\n";
    // Try midpoint
    cout << "Midpoint is " << midpoint(30,35) << "\n";
    cout << "Midpoint is " << midpoint(20,11) << "\n";
    cout << "Midpoint is " << midpoint(72,6) << "\n";
    print_chars(60, '=');
    cout << "\n";
    print_chars(-5, '!');
    print_chars(20, '*');
    print_chars(20, ' ');
    print_chars(20, '*');
}

double midpoint(double x, double y)
{
    return (x + y) / 2;
}

void print_chars(int how_many, char my_silly_char)
{
    if(how_many < 0) 
    {
        cout << "OOPS! how_many < 0.\n";
        return;
    }
    for(int i = 1; i <= how_many; i++)
    {
        cout << my_silly_char;
    }
}


a6-with-functions.cpp

#include <iostream>
#include <iomanip>
using namespace std;

int ask_user_for_year();
void print_month(int month);
void print_header(int month, int year);
int compute_month_start(int month, int year);

int main()
{
    int year = ask_user_for_year();
    int month;
    // TODO: you write ask_user_for_month
    cout << "Enter month (1-12): ";
    cin >> month;
    while(month < 1 || month > 12)
    {
        cout << "ERROR. Enter month (1-12): ";
        cin >> month;
    }   
    cout << "\n";
    print_header(month, year);
    int dayOfWeek = compute_month_start(month, year);
   //cout << "(starts on " << dayOfWeek << ")\n";
   // skip over to correct day
   //cout << setw(4 * dayOfWeek) << "";
   for(int i = 0; i < dayOfWeek; i++) {
        cout << "    ";// four spaces
   }
   int daysInMonth = 31;
   if(month == 2) { daysInMonth = 29; }
   else if(month == 4 || month == 6 || month == 9
            || month == 11)
     { daysInMonth = 30; }
   int day = 1;
   while(day <= daysInMonth)
   {
        cout << setw(4) << day;
        // One way: check whether day%7 == saturday
        //int saturday = (7 - dayOfWeek) % 7;
        // Another way: also increment dayOfWeek
        day++;
        if(dayOfWeek == 6) // Saturday
        {
            cout << "\n";
        }
        dayOfWeek = (dayOfWeek + 1) % 7;
   }  
   
   return 0;
}

int ask_user_for_year()
{
    int year;
    while(1)
    {
        cout << "Enter year (1900-): ";
        cin >> year;
        if(year >= 1900)
        {
            return year;
        }
        cout << "ERROR, out of range.\n";
    }
}

void print_month(int month)
{
    switch(month)
    {
    case 1: cout << "January"; break;
    case 2: cout << "February"; break;
    case 3: cout << "March"; break;
    case 4: cout << "April"; break;
    case 5: cout << "May"; break;
    case 6: cout << "June"; break;
    case 7: cout << "July"; break;
    case 8: cout << "August"; break;
    case 9: cout << "September"; break;
    case 10: cout << "October"; break;
    case 11: cout << "November"; break;
    case 12: cout << "December"; break;
   }
}

void print_header(int month, int year)
{
    print_month(month);   
    cout << " " << year << "\n\n";
    cout << " Sun Mon Tue Wed Thu Fri Sat\n";
}

int compute_month_start(int month, int year)
{
   // on what day does the month start?
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    int y = year - (month < 3);
    int dayOfWeek = (y + y/4 - y/100 + y/400 + t[month-1] + 1) % 7;
    return dayOfWeek;
}