For all of these questions, you are not allowed to create your own functions in your solution. No credit will be given if this rule is violated.
Examine the following sample runs, and write a program that functions in exactly the same way.
$ ./lab3q1 Enter time (1-1000): 10 Enter interval (1-1000): 1 Time left: 10 Time left: 9 Time left: 8 Time left: 7 Time left: 6 Time left: 5 Time left: 4 Time left: 3 Time left: 2 Time left: 1 We have lift-off! $ ./lab3q1 Enter time (1-1000): 10 Enter interval (1-1000): 2 Time left: 10 Time left: 8 Time left: 6 Time left: 4 Time left: 2 We have lift-off! $ ./lab3q1 Enter time (1-1000): 10 Enter interval (1-1000): 3 Time left: 10 Time left: 7 Time left: 4 Time left: 1 We have lift-off! $
(a) Nemo, your friend who keeps giving you problems, comes up to you with the blueprint for an improved “CAP Computation” Program. He shows you what a sample run of the program will look like (user input is denoted in bold).
Enter number of modules: 5 Enter MC and GP for Module #1: 4 3 Enter MC and GP for Module #2: 5 2.5 Enter MC and GP for Module #3: 4 4.5 Enter MC and GP for Module #4: 4 3.5 Enter MC and GP for Module #5: 3 5 Number of modules: 5. My CAP is 3.58.
Write a C program called lab3q2a.c that follows the above sample run exactly. Your program first prompts the user to enter the number of modules (suppose we call it m). It then prompts the user to enter the MC (Modular Credit) and GP (Grade Point) for each of the m modules. It then shows the total number of modules and displays the CAP rounded to two decimal places.
Test your program with different MC / GP values and different number of modules. How do you know if your program is working perfectly?
(b) Nemo comes up with Version 2 of his blueprint. He shows you what a sample run of the program will look like (user input is denoted in bold).
Enter MC and GP for Module #1 (0 0 to quit): 4 3 Enter MC and GP for Module #2 (0 0 to quit): 5 2.5 Enter MC and GP for Module #3 (0 0 to quit): 4 4.5 Enter MC and GP for Module #4 (0 0 to quit): 4 3.5 Enter MC and GP for Module #5 (0 0 to quit): 3 5 Enter MC and GP for Module #6 (0 0 to quit): 0 0 Number of modules: 5. My CAP is 3.58.
The difference here is that the user does not need to specify in advance how many modules there are. He simply keeps entering MC and GP values. When he has entered all the module information, he terminates input by entering a zero for both MC and GP values.
Write a C program called lab3q2b.c that follows the above sample run exactly. Your program keeps prompting the user to enter the MC (Modular Credit) and GP (Grade Point) for a module while increasing the module number. Keep looping until the user enters zero or a negative number as the MC. It then shows the total number of modules and displays the CAP rounded to two decimal places.
Test your program with different MC / GP values and different number of modules. How do you know if your program is working perfectly?
A prime number is a number that is divisible only by one and itself. Any number that is not a prime number is a composite number.
Write a program that determines if the integer entered by the user is a prime number.
Assuming that the name of the executable file is lab3q3, sample run(s) of the program are shown below. User input is denoted in bold.
$ ./lab3q3 Enter an integer (2-2147483647): 2 2 is a prime number. $ ./lab3q3 Enter an integer (2-2147483647): 97 97 is a prime number. $ ./lab3q3 Enter an integer (2-2147483647): 221 221 is a composite number. $ ./lab3q3 Enter an integer (2-2147483647): 18181 18181 is a prime number. $ ./lab3q3 Enter an integer (2-2147483647): 330548761 330548761 is a composite number. $ ./lab3q3 Enter an integer (2-2147483647): 2147483647 2147483647 is a prime number. $
Every integer >= 2 has a unique prime factorization; it can be written uniquely as a product of primes in non-decreasing order. Here are some examples:
Assuming that the name of the executable file is lab3q4, sample run(s) of the program are shown below. User input is denoted in bold.
$ ./lab3q4 Enter an integer (2-2147483647): 2 2 = 2 $ ./lab3q4 Enter an integer (2-2147483647): 4 4 = 2 * 2 $ ./lab3q4 Enter an integer (2-2147483647): 1764 1764 = 2 * 2 * 3 * 3 * 7 * 7 $ ./lab3q4 Enter an integer (2-2147483647): 18181 18181 = 18181 $ ./lab3q4 Enter an integer (2-2147483647): 330548761 330548761 = 18181 * 18181 $ ./lab3q4 Enter an integer (2-2147483647): 2147483647 2147483647 = 2147483647 $
Hint: Making use of certain parts of your program in Question 3 may make this program easier.
A total of 21 different hosts have accessed this document in the last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.