1
HW #1 Assignment (9 problems)
Due: Wed., 09/23/2020 for both Sessions 002 and 003.
Write C code that does the following:
1. Interactively read the three coefficients of a quadratic equation and compute the two roots.
The program must alert if there is no real root, i.e. read a, b, c of
a x2 + b x + c = 0,
and compute
Note: sqrt is available in math.h. You need to compile the program with "-lm" option, i.e.
gcc -lm myprogram.c
Make sure that you add a routine to check the positiveness of the discriminant using
the if statement. Show actual runs and examples.
2. Interactively read temperature in Celsius and convert it to Fahrenheit. Note
C = (F -32 ) × 5/9 .
Example:
$ a.out
Enter temperature in C = 29
It is 84.2 degrees in Fahrenheit.
3. Write a C program to sum up the first 100, 1000, and then 10000 terms of the series,
espectively, to see whether the sum is getting closer to
Note that the general term, an, is expressed as
See below for the background of this series.
2
4. Write a C program to sum up the first 100, 1000, and then 10000 terms of the series,
espectively, to see what value it may approach.
5. A sequence an is given with the following rule:
an+2 = - 2 an+1 + 3 an, a0 = 2, a1 = -1.
Write a C program to compute a17.
3
6. Write a C code to make a table of (x , x - x3/6 + x5/120, sin x) using functions in the format of
x x - x3/6 + x5/120 sin (x)
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
... ... ...
XXXXXXXXXX
Note: The values above are not co
ect.
Template
#include
float f(float x)
{
eturn x*x ;
}
* main */
int main()
{
int i;
printf(" x f(x) \n");
printf(" XXXXXXXXXXn");
for (i=0; i< 10; i++) printf( " (fill in your code) \n", 0.1*i, f(0.1*i));
eturn 0;
}
7.
(a) Write a function, int factorial(int n), which returns n! (the factorial of n, i.e. 1 ×2 ×3 ×...×n.)
(b) Using int factorial(int n) above, write a program to compute
4
8. We want to find one of the roots of a cubic equation given by
x3 + x - 1 = 0
which is between 0 and 1 by an iterative method. Modify the equation above to
Start with an appropriate initial value and do the iteration above until it is convergent. For
example,
The simplest (minimum) code may look like:
#include
int main()
{
int i;
float x;
printf("Enter initial guess =");scanf("%f",&x);
for (i=0;i<100;i++) /* insert your iteration code */
printf("%f\n",x);
eturn 0;
}
9. A set of 10 float numbers is given below. Write a C program to show how many out of 10 are
etween 2.0 and 10.0 (including these two values).
{1.0, 9.0, 13.0, 8.5, 3.0, 9.0, 17.0, 4.5, 10.0, 2.0}
=0.728…
5
Instruction for submission
• Go on Canvas, select course MAE XXXXXXXXXXor 003 (depending on which lab session that
you are enrolled in), then click on assignments and select the respective HW or lab
assignment to be submitted and upload the document.
• The format in which the assignment is to be submitted is only docx and doc.
• The time period given for the submission is the day that the assignment is given till the
mid-night of due date.
• It will be a late submission only until one day after due date. The late submission will lose
20% of the score. If someone delays beyond this, the link for submission of the assignment
will disappear and will be considered as “not submitted”.
• Two attempts will be allowed for the submission of the respective assignment.
• Please do not copy others works, plagiarism check will be deployed to catch such an
activity.
Template for HW
1. HW 01
MM/DD/Year (submission date)
Section 002 (or 3)
Doe, John (last name, first name)
1325 (last 4 digits of your student ID)
Problem 1
• Program listing
• Example run
• Discussion (optional)
Problem 2
Problem 3