Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

HOMEWORK

Embedded Systems
Problem Set 04: C programming

Instructor: Dr. Hung Ta


Release: February 2, 2021

61FIT2ESY
HANU-Faculty of Information Technology Embedded Systems

Homework
Students should use the textbook [1] and Dev–C++ to solve problems below.

Problem 1 ([1][Exercise 2.16, page 97])


(Arithmetic) Write a program that asks the user to enter two numbers, obtains them from
the user and prints their sum, product, difference, quotient and remainder.

Problem 2 ([1][Exercise 2.19, page 97])


(Arithmetic, Largest Value and Smallest Value) Write a program that inputs three
different integers from the keyboard, then prints the sum, the average, the product, the
smallest and the largest of these numbers. Use only the single-selection form of the if
statement you learned in this chapter. The screen dialogue (Firure 1) should appear as
follows:

Figure 1: Output screen

Problem 3 ([1][Exercise 3.35, page 142])


(Printing the Decimal Equivalent of a Binary Number) Input an integer (5 digits or
fewer) containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent.
[Hint: Use the remainder and division operators to pick off the “binary” number’s digits one
at a time from right to left. Just as in the decimal number system, in which the rightmost
digit has a positional value of 1, and the next digit left has a positional value of 10, then
100, then 1000, and so on, in the binary number system the rightmost digit has a positional
value of 1, the next digit left has a positional value of 2, then 4, then 8, and soon. Thus
the decimal number 234 can be interpreted as 4*1+3*10+ 2*100. The decimal equivalent of
binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]

Problem Set 04: C programming Page 1 of 4


HANU-Faculty of Information Technology Embedded Systems

Problem 4 ([1][Exercise 3.45, page 143])


(Factorial) The factorial of a nonnegative integer n is written n! (pronounced “n factorial”)
and is defined as follows:

n! = n × (n − 1) × (n − 2) × . . . × 1, for n ≥ 1, n! = 1 for n = 0.

1. Write a program that reads a nonnegative integer and computes and prints its factorial.

2. Write a program that estimates the value of the mathematical constant e by using the
formula:
1 1 1
e = 1 + + + + ...
1! 2! 3!
3. Write a program that computes the value of ex by using the formula:

x x2 x3
ex = 1 + + + + ...
1! 2! 3!

Problem 5 ([1][Exercise 4.27, page 186])


(Pythagorean Triples) A right triangle can have sides that are all integers. The set of
three integer values for the sides of a right triangle is called a Pythagorean triple. These
three sides must satisfy the relationship that the sum of the squares of two of the sides is
equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2, and
the hypotenuse all no larger than 500. Use a triple-nested for loop that simply tries all
possibilities. This is an example of brute-force computing. Its not aesthetically pleasing
to many people. But there are many reasons why these techniques are important. First,
with computing power increasing at such a phenomenal pace, so- lutions that would have
taken years or even centuries of computer time to produce with the technology of just a few
years ago can now be produced in hours, minutes or even seconds. Recent microprocessor
chips can process a billion instructions per second! Second, as youll learn in more advanced
computer science courses, there are large numbers of interesting problems for which theres
no known algorithmic approach other than sheer brute force. We investigate many kinds of
problem-solving methodologies in this book. Well consider many brute-force approaches to
various interesting problems.

Problem 6 ([1][Exercise 4.31, page 187])


(Diamond-Printing Program) Write a program that prints the following diamond shape
(Figure 2). You may use printf statements that print either a single asterisk (*) or a single
blank. Maximize your use of iteration (with nested for statements) and minimize the number
of printf statements.

Problem Set 04: C programming Page 2 of 4


HANU-Faculty of Information Technology Embedded Systems

Figure 2: Output screen

Problem 7 ([1][Exercise 5.31, page 239])


(Coin Tossing) Write a program that simulates coin tossing. For each toss of the coin
the program should print Heads or Tails. Let the program toss the coin 100 times, and
count the number of times each side of the coin appears. Print the results. The program
should call a separate function flip that takes no arguments and returns 0 for tails and 1 for
heads. [Note: If the program realistically simulates the coin tossing, then each side of the
coin should appear approximately half the time for a total of approximately 50 heads and
50 tails.]

Problem 8 ([1][Exercise 5.35, page 240])


(Fibonacci) The Fibonacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, . . .

begins with the terms 0 and 1 and has the property that each succeeding term is the sum of
the two preceding terms.

1. Write a nonrecursive function fibonacci(n) that calculates the nth Fibonacci number.
Use unsigned int for the function’s parameter and unsigned long long int for its
return type.

2. Determine the largest Fibonacci number that can be printed on your system.

Problem 9 ([1][Exercise 6.30, page 305])


(The Sieve of Eratosthenes) A prime integer is any integer greater than 1 that can be
divided evenly only by itself and 1. The Sieve of Eratosthenes is a method of finding prime
numbers. It works as follows:

Problem Set 04: C programming Page 3 of 4


HANU-Faculty of Information Technology Embedded Systems

1. Create an array with all elements initialized to 1 (true). Array elements with prime
indices will remain 1. All other array elements will eventually be set to zero.

2. Starting with array index 2 (index 1 is not prime), every time an array element is
found whose value is 1, loop through the remainder of the array and set to zero every
element whose index is a multiple of the index for the element with value 1. For array
index 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero
(indices 4, 6, 8, 10, and so on.). For array index 3, all elements beyond 3 in the array
that are multiples of 3 will be set to zero (indices 6, 9, 12, 15, and so on.).

When this process is complete, the array elements that are still set to 1 indicate that the
index is a prime number. Write a program that uses an array of 1,000 elements to determine
and print the prime numbers between 1 and 999. Ignore element 0 of the array.

Problem 10 ([1][Exercise 6.35, page 305])


(Print an Array) Write a recursive function printArray that takes an array and the size
of the array as arguments, prints the array, and returns nothing. The function should stop
processing and return when it receives an array of size zero.

References
[1] Paul Deitel and Harvey Deitel, C How to Program, Pearson, 8th edition, 2016, Global
Edition.

Problem Set 04: C programming Page 4 of 4

You might also like