CSSE 120 -- Intro. to Software Development

Homework 22

  1. Take a look at the Python vs C comparison document. You may find this helpful as you try to do things in C that you already know how to do in Python.  Feel free to suggest things that we might add to this document.
  2. You must do this assignment using Eclipse and the CNestedLoops project that you checked out from your individual SVN repository in class.

    Be sure to check out that project into your Eclipse C workspace, not your Python workspace. This will require connecting to your repository again in the new workspace, using the SVN Repository Exploring perspective. Recall that your individual SVN repository is at the URL:

    http://svn.cs.rose-hulman.edu/repos/(your class)-(your term)-(your username)
    IE: csse120-201310-username

    Within the CNestedLoops project, open the file NestedLoopsPatterns.c. It contains a template for the rest of this assignment.

    The output from most of the functions you are to write is similar (in some cases identical) to output from some of the Python functions that you wrote for Homework 12. So the main purpose of the assignment is to get you accustomed to C syntax.

    1. Look at the function rectangleOfStars. Make sure you understand how it works. Ask for help if you're confused. Note that, unlike Python's print, C's printf does not automatically end the output line or put in extra spaces anywhere.

      Note: You may assume that the functions that print numbers will only be given one-digit numbers as their "highest number to print" actual parameters. Otherwise, some of the output formatting would be very tricky.
    2. (8 points) Based on rectangleOfStars, fill in code for the function triangleOfStars. This prints out a triangle-shaped grid of stars. For example, the code:
      triangleOfStars(6);
      
      should produce the output:
      *
      **
      ***
      ****
      *****
      ******
      

      After testing and debugging this function, commit your work to your Subversion repository. Do this by right-clicking on your CNestedLoops project in Eclipse and choosing Team → Commit....

    3. (8 points) The next function, triangleAllNumsEachRow, is like triangleSameNumEachRow, except that each character is its position from the left, instead of from the top. For example:
      triangleAllNumsEachRow(6);
      
      should produce the output:
      1
      12
      123
      1234
      12345
      123456
      

      After testing and debugging this function, commit your work to your Subversion repository using a sensible log message.

    4. (8 points) The next function, triangleNumsCentered, is like triangleNumsRightJustified, except that the triangle is centered and includes spaces. For example:
      triangleNumsCentered(9);
      
      should produce the output:
              1 
             2 2 
            3 3 3 
           4 4 4 4 
          5 5 5 5 5 
         6 6 6 6 6 6 
        7 7 7 7 7 7 7 
       8 8 8 8 8 8 8 8 
      9 9 9 9 9 9 9 9 9 
      

      After testing and debugging this function, commit your work to your Subversion repository.

  3. That's Perfect.
    1. You must do this assignment using Eclipse and the ThatsPerfect project that you must check out from your individual SVN repository.

      Be sure to check out that project into your Eclipse C workspace, not your Python workspace.

    2. In the source file perfect.c, edit the initial comment to include your name and today's date.
    3. (10 points) Build and run the program.  Save your answer to this question in the file overflow.txt in the ThatsPerfect project .
      1. What's the largest int produced by doublingInt() before it overflows?
      2. The doublingInt() function demonstrates how in C (and most languages), eventually an integer gets so big that it can no longer be represented by the language (without some extra tricks that you'll see in later courses). In this example, the variable i eventually turned negative, indicating that what it represented was no longer the intended number.

        How about Python? Consider this Python code that is equivalent to doublingInt():

        def doubling_int():
            i = 1
            while i > 0:
                i = i * 2
                print(i)
            print('Overflowed at', i)
        

        What will happen, in Python, if you execute this function?

      3. Edit the doublingInt() function to count how many times the value 1 can be doubled before exceeding the maximum expressible int value (before it overflows). How many times can it be doubled?
      4. Comment the call to doublingInt() in function main() and uncomment the call to doublingDouble().  Now edit the doublingDouble() function to count how many times the value 1.0 can be doubled before exceeding the maximum expressible double value. How many times can it be?
      5. Commit your solutions to your svn repository.
    4. (5 points) Now, edit the main() function so that it uses a sentinel while loop, printf(), and scanf() to prompt the user to enter integers. Exit the loop when the user enters a negative number. Otherwise, just print the value the user entered and prompt for another number.

      Hint: if your project refuses to build (permission denied), you probably have at least one copy of it running. Go to debug mode, and select each version that's running and stop it by clicking on the red square. (It's common to forget to close a program that is waiting for keyboard input. More common errors and how to fix them can be found here.)

      When you have this part working, commit it to your repository and go on to the next step.

    5. (15 points) A perfect number is a positive number that is the sum of its factors (excluding itself). For example, 6 and 28 are perfect because 1+2+3=6 and 1+2+4+7+14=28. But 24 is not perfect because 1+2+3+4+6+8+12 is not 24.

      Define 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, loop through the numbers from 1 to n-1, checking each to see whether it is a factor of n. (Hint: use % for this check.) Add up the numbers that are factors. If that sum equals n, the number is perfect; otherwise, it is not.

    6. (5 points) Change the while loop you created in step 3d so that it prints whether or not the entered number is perfect. A sample run 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
      	Goodnight, Gracie.
      

      When you have this part working, commit it to your repository and go on to the next step.

    7. (10 points) The first two perfect numbers are 6 and 28. Modify your program so that after the interactive part, but before exiting, it calculates and prints the first four perfect numbers. The fourth perfect number is quite large, so your program might take a while to run.

      When you have this part working, commit it to your repository and enjoy a refreshing beverage.

Preparation for Next Time

Watch the set of videos assigned for next time and complete the take-home quiz. The main schedule page contains links to the videos, slides, and quiz. Paper copies of the quiz are due at the start of next class.