CS1101C Lab 3

The deadline for this lab is Friday 19 September 2008, 23:59:59 hours.

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.

Question 1 (Lift-Off!)

The name of your C program file must be called lab3q1.c, files with any other name will not be marked.

Examine the following sample runs, and write a program that functions in exactly the same way.

Sample Runs

Assuming that the name of the executable file is lab3q1, sample run(s) of the program are shown below. User input is denoted in bold.

$ ./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!

$

Question 2 (CAP Computation)

Now that you have learned selection and repetition statements, you realize that your world famous “CAP Computation” Program has a serious shortfall; it is currently fixed to allow the computation of exactly five modules. If you wish to compute the CAP for a different number of modules, modifying your existing solution is tedious.

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

Question 3 (Prime Numbers)

The name of your C program file must be called lab3q3.c, files with any other name will not be marked.

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.

Sample Runs


$ ./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.

$

Question 4 (Prime Factorization)

The name of your C program file must be called lab3q4.c, files with any other name will not be marked.

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:

Write a program that displays the unique prime factorization for any integer >= 2.

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.

Sample Runs


$ ./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.

Note and Ponder

  1. Assume that all user input is valid unless otherwise stated.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 29 times since 25-Jun-24 11:57:13 +08. This is the 2nd time it has been accessed today.

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.