IE1005 EXTRA Questions and Exercises Part 1
IE1005 EXTRA Questions and Exercises Part 1
QUESTION
1
(b) y e (1/ 2)( x5) / x
x
PRACTICAL EXERCISES
Exercise 1 %*c
You have learnt how to use %c to output a single character, and also the form with
a field width such as %5c to provide 4 leading blank spaces before the character.
There is an even more flexible way to control the number of leading blank spaces.
This is done through the use of the conversion specifier %*c. Retrieve the program
STAR_C.C from the Handson folder of the NTULearn IE1005 course-site, and
study the use of %*c in the statement:
printf("%*c\n",i,ch);
When the printf statement is executed, the value of integer i replaces the *. For
example, if i=5, the above printf statement is equivalent to:
printf("%5c\n",ch);.
2 Tutorial & Practical Exercises
Run this program. Then add statements using %*c so that your program produces
the following pattern.
#
#
#
#
#
#
#
#
#
#
#
Exercise 3 math.h
Write a program CALCULATE.C that asks the user for a real number x as input
and outputs the following using printf() statements:
You can refer to Lecture Lesson 5 notes (slide 5 for math.h) to get the relevant
functions required.
Exercise 4
Write a program using the floor() function to round a positive value to the nearest
integer.
Tutorial & Practical Exercises 3
QUESTION
if (x == y)
where x and y are floating-point variables. Explain. What should be done if you
wish to check the equality of two floating-point numbers?
PRACTICAL EXERCISES
Your program should also correctly display that Year 1700 and 1900 are not leap years.
4 Tutorial & Practical Exercises
QUESTION
PRACTICAL EXERCISES
Exercise 1—Factorial
Exercise 2
Modify Program 7.3 (Solution of quadratic equation) in the lecture notes so that
(a) it will only accept 'y', 'Y', 'n', 'N' as the response to the
question: "Solve another problem (y/n)?".
(b) it uses a for loop instead of a while loop.
(c) it uses a do-while loop instead of a while loop.
Exercise 3
Write a C program to compute the sum and average of all the positive integers in a list
of user-input integers. Negative integers are ignored. The average is to be calculated
as a variable of type double and output with 2 decimal places. You must take into
consideration the case where all input integers are negative. The following is a sample
screen display (user input in boldface):
6 Tutorial & Practical Exercises
Average = 75.00
Note the above that the average is computed over positive numbers only,
i.e. the average for above is sum (80+70) divided by 2 positive numbers,
not 3 input numbers, to give 75.
If all the input numbers are negative, display the following message:
No positive numbers to sum or average
----------------------------------------------------------------------------------