0% found this document useful (0 votes)
255 views6 pages

IE1005 EXTRA Questions and Exercises Part 1

This document contains instructions and code snippets for 4 practical exercises related to C programming concepts like operators, standard library functions, decision making, and loops. The exercises involve tasks like using format specifiers like %*c to control output formatting, determining if a year is a leap year, calculating factorials using loops, and computing sums and averages of positive numbers. Code examples are provided to help complete the exercises.

Uploaded by

Rogs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views6 pages

IE1005 EXTRA Questions and Exercises Part 1

This document contains instructions and code snippets for 4 practical exercises related to C programming concepts like operators, standard library functions, decision making, and loops. The exercises involve tasks like using format specifiers like %*c to control output formatting, determining if a year is a leap year, calculating factorials using loops, and computing sums and averages of positive numbers. Code examples are provided to help complete the exercises.

Uploaded by

Rogs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorial & Practical Exercises 1

IE1005 From Computational Thinking to Programming


Extra Question and Practical Exercises 4 (Week 5)
C Operators and Standard Library Functions

QUESTION

1. Write a C statement for each of the following:


2
x
(a) y  1  sin 2 x
1x 2

1
(b) y  e  (1/ 2)( x5) / 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 2 A Character or an Integer


The data type char is used to store characters such as letters and punctuation
marks, but technically it is an integer type. Copy the program INT_CHAR.C from
the Handson folder of the NTULearn IE1005 course-site. Compile and execute the
program. Can you interpret the output?

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:

(a) its absolute value (try fabs(x), as well as abs(x)from stdlib.h),


(b) its sine, cosine, and tangent function values,
(c) its exponential, and natural logarithm function values,
(d) its square root (Try a negative value for x.),
(e) ceil(x) and floor(x).

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

IE1005 From Computational Thinking to Programming


Extra Question and Practical Exercises 5 (Week 6)
Decision Making

QUESTION

1. It is usually a bad idea to write a statement such as

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

Exercise 1 'a' to 'z'


Without using islower(), write a program CHK_CASE2.C to determine if a
user-input character is a lowercase letter. A lowercase letter is any character that is
greater than or equal to 'a' and less than or equal to 'z'. The screen output is as
follows:

Enter a character: B Enter a character: e


'B' is not a lowercase letter. 'e' is a lowercase letter.

Exercise 2 — Leap Year


A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400.
Otherwise, it is not a leap year. Write a complete C program to determine whether a
given year is a leap year. You may assume that the user input is a valid year. The
screen display should be as follows (user input is shown in boldface):

Enter the year to be tested => 2000


The year 2000 is a "leap year".

Otherwise, the message displayed should be in the form:

Enter the year to be tested => 1800


No, the year 1800 is not a "leap year".

Your program should also correctly display that Year 1700 and 1900 are not leap years.
4 Tutorial & Practical Exercises

Exercise 3 — Character Handling Functions


Write a program to do the following:
(a) Ask the user to input a character.
(b) Use standard library functions to check whether it is alphabetic. If it is
alphabetic, check whether it is in uppercase. If it is not in uppercase, convert it
into uppercase.
(c) If it is not an alphabet, check whether it is a digit.
Tutorial & Practical Exercises 5

IE1005 From Computational Thinking to Programming


Extra Question and Practical Exercises 6 (Week 7)
Loops

QUESTION

1. Rewrite a for loop using a while structure.

PRACTICAL EXERCISES

Exercise 1—Factorial

Write a program to calculate the factorial of a user-input positive integer.

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

How many numbers to process? 3


Enter number => 80
Enter number => -60
Enter number => 70
There are 2 positive integers.

Sum of integers = 150

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

----------------------------------------------------------------------------------

You might also like