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.
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....
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.
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.
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.
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?
(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.
(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.
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.
(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.
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.