Previous Lecture lect07 Next Lecture

lect07, Tue 01/29

Midterm preparation and overview / More functions and debugging

The midterm is this Thursday, January 31st! It will take place during the lecture time you are enrolled in and will cover everything up through this lecture. You are allowed one sheet of notes, written or typed. Please show up 5 to 10 minutes early! The exam will start at exactly 3:30 or 6:30 sharp!

See the lecture slides for the list of topics on the exam, as well as the practice problems and solutions presented during lecture.

Here are some problems to expect:

Note that this list is not comprehensive, and you should be comfortable with any material discussed in lectures, homeworks, and labs.

Converting the example from the slides to accept the user input from the command line. Note that the program will crash if no additional command-line arguments (other than the name of the executable) are provided!

// mult_cl.cpp
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int sum = 0;
    int n = -1;

    n = atoi(argv[1]);

    for(int i = 1; i < n; i++)
    {
        if ((i % 3 == 0) || (i % 5 == 0))
        {
            sum += i;
        }
    }
    cout << "The sum is " << sum << endl;
    return 0;
}

Miscellaneous


Converting the example from the slides to use a function and provide a more elaborate output:

// mult_funct_n.cpp
#include <iostream>
using namespace std;

int compute_sum(int limit)
{
    int sum = 0;

	for(int i = 1; i < limit; i++)
	{
		if ((i % 3 == 0) || (i % 5 == 0))
		{
			sum += i;
		}
	}

    return sum;
}

int main()
{
   int n = -1;
	cout << "Enter n: ";
	cin >> n;

    for (int i = 0; i < n; i++)
    {
        cout <<"i = " << i << " The sum is " << compute_sum(i) << endl;
    }
	return 0;
}