Docsity

Assignment 7: Seven-Segment Display and Exponential Function, Assignments of Computer Science

Toggle sidebar
bg1
CGS3460 Fall 2007
Assignment 7
Assigned: 10/6, Sat
Due: 10/12, Fri
1. Calculators, watches and other electronic devices often rely on a seven-segment display for
numerical output. To form a digit, such devices “turn on” some of the seven segments while
leaving others “off”.
Write a C program that generates the seven-segment display for all digits from 0 to 9 and prints
them one after another in a column. One way to implement this would be to use a 2-dimensional
array to store what segments to turn on and what segments to turn off for each digit. This array
would have 10 rows (one for each digit) and seven columns (one for each segment). You can
number the segments in some order and then for each digit set the i
th
column to 1 if the i
th
segment is to be turned on, and otherwise set it to 0. While displaying, print the appropriate
symbol or blank space depending on whether the segment is to appear “on” or “off”. Save this
program in a file call p1.c.
The output for first 4 digits should be:
-
| |
| |
-
|
|
-
|
-
|
-
-
|
-
|
-
| |
-
|
2. In assignment 6 you wrote a program that computed the value of e
x
to the desired precision.
Modify your solution so that now your program uses 5 functions in addition to the main function.
The first function should display the message “Enter the value of x: “, scan the value of x and
return it to the calling function. The second function should display the message “Enter the
value of n: “, scan the value of n and return it to the calling function. The third function should
take two arguments – x and m – and return the value x
m
. The fourth function should take one
argument – m – and return the value m!. Finally, the fifth function should take two arguments –
x and the final computed value of e
x
– and display them with appropriate message as shown in the
examples below. A correct solution would not have any printf statements in the main function.
You can use the third and the fourth functions to compute the numerator and denominator of each
term in the series. You should consider carefully the return types of the various functions so as to
pf2

Partial preview of the text

Download Assignment 7: Seven-Segment Display and Exponential Function and more Assignments Computer Science in PDF only on Docsity!

CGS3460 Fall 2007 Assignment 7

Assigned: 10/6, Sat Due: 10/12, Fri

  1. Calculators, watches and other electronic devices often rely on a seven-segment display for numerical output. To form a digit, such devices “turn on” some of the seven segments while leaving others “off”.

Write a C program that generates the seven-segment display for all digits from 0 to 9 and prints them one after another in a column. One way to implement this would be to use a 2-dimensional array to store what segments to turn on and what segments to turn off for each digit. This array would have 10 rows (one for each digit) and seven columns (one for each segment). You can number the segments in some order and then for each digit set the ith^ column to 1 if the ith segment is to be turned on, and otherwise set it to 0. While displaying, print the appropriate symbol or blank space depending on whether the segment is to appear “on” or “off”. Save this program in a file call p1.c.

The output for first 4 digits should be: (^) - | | | |-

| |

  • | |-
  • |
  • |

| |- |

  1. In assignment 6 you wrote a program that computed the value of ex^ to the desired precision. Modify your solution so that now your program uses 5 functions in addition to the main function. The first function should display the message “Enter the value of x: “, scan the value of x and return it to the calling function. The second function should display the message “Enter the value of n: “, scan the value of n and return it to the calling function. The third function should take two arguments – x and m – and return the value xm. The fourth function should take one argument – m – and return the value m!. Finally, the fifth function should take two arguments – x and the final computed value of ex^ – and display them with appropriate message as shown in the examples below. A correct solution would not have any printf statements in the main function. You can use the third and the fourth functions to compute the numerator and denominator of each term in the series. You should consider carefully the return types of the various functions so as to

not compromise the accuracy of your results. Save this program as p2.c. The problem statement is repeated here from the last assignment.

The exponential function, represented as ex, is one of the most important function in mathematics. The number e in the formula for exponential function is approximately equal to 2.71828 (in fact with this program you should be able to generate the value of e to a higher precision) and is also known as the Euler’s number. Interestingly, this function can also be computed using its power series, which is defined as following:

Here, the numbers in the denominators, m!, represent the factorial of the number m.

m! = 1 x 2 x 3 x … x m.

e.g. 3! = 1 x 2 x 3 = 6, 5! = 1 x 2 x 3 x 4 x 5 = 120.

Write a program that computes and displays the value of ex^ using its power series. User should enter the values of numbers x and n (the number of terms to use in the summation). Assume number x to be a floating point number. The number n governs the number of terms we want to include in our computation of the ex. Larger the n, better the accuracy. You can assume that user always enters a positive integer for n. Calculate and display your results to 10 decimal places. Round-off at the last decimal place is acceptable. A session with this program should be as follows:

Example 1: Enter value for x: 0 Enter value for n: 5 Value of exp(0.000000) = 1.0000000000.

Example 2: Enter value for x: 1 Enter value for n: 10 Value of exp(1.000000) = 2.7182817459.

Example 3: Enter value for x: 1. Enter value for n: 13 Value of exp(1.700000) = 5.4739475250.

Instructions for submitting Assignment 7

  1. First compress your program files into a single file. At the terminal command prompt (Putty incase you are working from Windows) type tar cvf assign7.tar p1.c p2.c. This should create the compressed file assign7.tar
  2. If you are working from Windows platform using Putty and WinSCP, you can transfer the tar file back to your Windows machine using WinSCP. It is reverse of the process you probably used to transfer your program(s) from your local machine to CISE machine.
  3. Upload the assign7.tar to CourseWorX at the course webpage.