CProjects - six short assignments to be implemented in the C programming language
CSSE 221 – Fundamentals of Software Development Honors
Fall 2008–2009

Work on these exercises individually, but be quick to get help as needed from your student assistants, your instructor and your classmates.

Program 1: PerfectNumbers (expressions, statements, functions)

Overview

A perfect number is a number that equals the sum of its factors that are smaller than itself (including 1).

Instructions

  1. In your Eclipse C workspace, not your Java workspace, create a new C project called PerfectNumbers of type Hello World ANSI C Project.
  2. Make sure the comment at the top of the file has your own name as the author of the program.
  3. Write a function
        int factorsOfN(int n, int factors[])
    
    that puts into the given array all the factors of n, including 1 but not including n itself. The function should return the number of factors it put into the array.

    Test your function by calling it from main with appropriate arguments and printing the results.

  4. Write a function
        int isPerfect(int n)
    
    that returns 1 if n is perfect and 0 if it is not perfect.

    Do not attempt to find a closed-form solution. Instead, use your factorsOfN function to compute the factors of n, and then sum those factors and compare the sum to n.

    Test your function by calling it from main with appropriate arguments and printing the results.

  5. Write a function
        int printPerfect(int m)
    
    that prints all the perfect numbers less than or equal to m and returns the number of such perfect numbers.

    Test your function by calling it from main with appropriate arguments.

  6. Write a function
        int askPerfect()
    
    that repeatedly asks the user for a number, checks it for perfection, and prints an appropriate response, until the user enters -1 to quit. You do NOT have to deal with bad inputs (like 4.3 or do 10 pushups).

    A sample run of this part of the program might be:

    	Enter an integer (negative to quit): 6
    	6 is perfect.
    
    	Enter an integer (negative to quit): 24
    	24 is not perfect.
    
    	Enter an integer (negative to quit): 28
    	28 is perfect.
    
    	Enter an integer (negative to quit): -1
    	Goodbye!
    

    Test your function by calling it from main and entering appropriate inputs.

  7. Turn in your work by adding and committing your program to your individual repository.