This week, we are going to write a C program that uses loops. You may want to reference the presentation L07 Control Flow lecture if you are not totally comfortable with loops already
To get an idea of what we are trying to do today, notice that there is a command available in Unix called factor. To run this command, type factor 30 in a Unix terminal window. The result is a string that starts with the number 30, followed by a colon, followed by the numbers 2, 3, and 5; which are the prime factors of 30. We are going to code a similar program that prints out the exact same stuff.
Make a lab3 sub-directory of your cs211 directory. Create and edit a file called factors.c.
We will use the C library functions printf and atoi so at the top of your factors.c file, add:
#include <stdio.h> #include <stdlib.h>We also need the standard main function, with the standard parameters to collect the command line argument. Our program is going to take a single command line argument, so make a check, like last week, to make sure we get the right number of arguments. If not, printf an error message telling the user how to invoke your program, and return a "1" to indicate something bad happened.
Once you are sure you have the right number of arguments, convert the first argument from a string into an integer. Last week, we used the atof function to convert to a floating point number; this week, we use the atoi function to convert to an integer. Keep the result in in integer variable called n.
We are only going to support factoring positive numbers. (Try running factor -12 to see how Unix handles negative numbers... we can do better than that!) Check to make sure the argument, converted to an integer, is a positive number. If not, print an error message that tells the user how to run your program, and quit, returning a non-zero return code. (Note that the atoi function returns a 0 if it cannot figure out how to convert the users argument, so checking for a positive number catches cases where the user types, for instance, something other than a number on the command line.)
Declare a local integer variable called f to keep track of the next potential factor of n. Also, print the beginning of the result line. You can do this with the statement printf("%d:",n);. Notice that there is no newline (\n) character at the end of the printf format string. Without the newline (\n) character, we can print more characters on the same line.
Next, loop through values of f, starting at the number 2, incrementing by 1, and stopping when f is equal to or greater than the current value of n. In the body of the loop, check to see if f is a factor of n. You can check this by using the C remainder operator. The number f is a factor of n if the remainder after dividing n by f is zero. In C, code this using the condition n%f==0. As long as f is a factor of n, you should do two things. First, print f, using the statement printf(" %d",f);. Secondly, replace the value of n by the value of n/f. Note that the same factor may appear multiple times in n... for instance, 8=2x2x2, so with n=8, 8%2=0, you would print out the first 2, and then set n to 4. Then, since 4%2=0, you would print out the second 2, and set n=4/2=2. Now, f is no longer less than n, so you would drop out of the loop.
When you end the loop, the f value will match the n value, indicating that the remaining f value is the last factor of the original n value. Therefore, after the loop, use the following printf statement to finish up: printf(" %d\n",f);. Note that since this is the last factor, the newline (\n) character is included to end the line.
Save your code with the above changes, compile it using the command gcc -g -Wall -o factors factors.c, and run it with various different command line arguments to make sure everything is working. Compare your results with the results of the Unix factor command to see if you get the same answers.
Go to MyCourses, and open the CS-211 page, and choose the "Content" hyperlink on the left list of options. You will see a "Lab 03 Submission" icon to upload your factors.c file. Make this submission before the due date of Friday, September 22 at 11:59 PM. You may submit as many times as you want before the due date, but only the latest submission will be graded.
This lab is worth 10 points. You will get deductions as follows:
You an increase the size of numbers handled by your factors.c program by using long integers instead of just integers. You will need to use the C library function atol to convert the argument to a long instead of an int. If you handle larger numbers, up to 3 points will be added to your lab03 score, as long as that score is not greater than 10.