CSSE 120 -- Intro. to Software Development

Homework 23

  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. Do the reading in the text Programming in C, Stephen G. Kochan (if you have the text), listed on the schedule page, or the material linked from the schedule page for the next session.
  3. (12 points ) Complete the Angel quiz over the reading assignment. You'll find the quiz on the course Angel page under Lessons → Homework → Homework 23 → Quiz 23: Structs and typedef.
  4. You must do this assignment using Eclipse and the Session23_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/csse120-201110-username

    Within the Session23_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 14. 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 Session23_CNestedLoops project in Eclipse and choosing Team → Commit.... Be sure to enter in a sensible log message as the course staff will review the messages.

    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 using a sensible log message.

    5. Submit your code by committing the final version to your Subversion repository.
  5. That's Perfect.
    1. You must do this assignment using Eclipse and the Session23_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 Session23_ThatsPerfect project .
      1. What's the largest int produced by doublingInt() before it overflows?
      2. What happens IN PYTHON when an integer value grows too large to be represented by the int type?
      3. Edit the doublingInt() function to count how many times the value 1.0 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 with an appropriate commit message.
    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 one that is the sum of its factors. 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, actually compute the factors of n and add them together.

    6. (5 points) Change the while loop you created in step 5 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 fourth perfect number. This 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.