CIS-3012 Homework #1: Basic Concepts

Due: 2023-09-08

Read Chapter 2 and Chapter 3 in the primary text (Lippman, et al.), but especially focus on Sections 2.3 (Compound Types), 2.4 (const Qualifier), and 3.3 (Library vector Type). Read LESSON #3 in my C++ tutorial that covers the basics of IOStreams. You may also find LESSON #2 on references and const, and LESSON #11 on vectors a useful supplement to the text.

  1. Write a C++ function called find_proper_divisors with the following declaration:

      void find_proper_divisors( unsigned long &number, vector<unsigned long> &divisors );
    

    The function takes a number and replaces the elements of the given vector with all of that number's proper divisors. Use the clear method in vector to ensure that the vector is empty before adding anything to it.

    A proper divisor of a number n, is an integer less than n that divides n evenly. For example, if given the number 12, find_proper_divisors should return 1, 2, 3, 4, and 6 in the divisors vector. Use the push_back method to append the proper divisors to the vector as they are found. The order in which the divisors are loaded into the vector is not important.

    If the given number is less than 2, ignore it (but erase the given vector anyway). Numbers less than 2 have no proper divisors. For this assignment, do not worry about indicating an error condition.

    Note that the modulus operator ('%') can be used to compute the remainder after division. A remainder of zero means the division was even. For example, 15 % 4 == 3 because after dividing 15 by 4, there is a remainder of 3. In contrast, 15 % 5 == 0 because there is no remainder after dividing 15 by 5.

  2. Write a C++ function called is_perfect with the following declaration:

      bool is_perfect( unsigned long number );
    

    The function returns true if the given number is a perfect number and false otherwise.

    A perfect number is a number that is equal to the sum of its proper divisors. For example, 6 is a perfect number because its proper divisors are 1, 2, and 3, and the sum 1+2+3 == 6.

  3. Write a main program that prints all perfect numbers that exist between 1 and 10,000. You can check your work against the information in the Wikipedia article linked above.

  4. Can either (or both) of the parameters to find_proper_divisors be made references to const? Add const where appropriate and explain your choices.

Submit a single C++ source file containing your final program. Be sure you include your name at the top of the file. I recommend (but do not require) you follow the style guide for your submissions. In any case, use a reasonable and consistent style. Be sure your submission can be compiled and tested using g++ on Lemuria; I will try it.


Last Revised: 2023-09-11
© Copyright 2023 by Peter Chapin <peter.chapin@vermontstate.edu>