Question: You need to answer them all! EET 208 Intro to Programming for Embedded Systems Lab #2 Functions in C Objectives The objectives of this lab
You need to answer them all!
EET 208
Intro to Programming for Embedded Systems
Lab #2 Functions in C
Objectives
The objectives of this lab exercise are to learn to create and use your own functions in C.
Introduction
In this lab exercise, you will be writing your own functions to accomplish a variety of tasks.
Setup
(We will be using Code::Blocks for this, so create a new Console Application.)
Exercise 1: Minimum Value Function
Write and test a function in C that calculates the minimum of two values.
Ill walk you through this first one, so you can see how function creation works:
Use the following as the prototype: int min(int a, int b); Use the following as the function body:
int min(int a, int b){ if(a
return (a);
} return (b);
}
Try it out in a program (set it up like the pythagDist() example in the lecture slides.) Write some code in main() to test it out with several different input values.
...Do you see why it works, even though return(b); isnt inside an else clause? (If not, ask!)
Exercise 2: Absolute Value
Now youre on your own. Write a function to calculate the absolute value of an integer. abs() already does this but dont use it; write your own.
Use the following as your prototype:
double absoluteValue(double inputNum);
(Hint: Return inputNum if its zero or greater; return 0-inputNum otherwise.)
2
Exercise 3: The Quadratic Equation
Given the equation AX2+BX+C=0, the roots of the equation are found at 22
[-B + sqrt(B -4AC)] / (2A) (and [-B - sqrt(B -4AC)] / (2A) ) Create and test a function to evaluate this, using the following prototype:
double quadratic(double a, double b, double c); //Return the positive root (Have your function simply return the positive root, from the left solution above.)
To square a number, just multiply it by itself: n=n*n;
Note: Make sure your equation has real roots, or youll get an error due to sqrt()
trying to handle negative numbers. Well address this problem next...
Exercise 4: Checking your input
The above function works well if and only if the equation has real roots.
For input like A=1, B=0, and C=10, it will fail (and crash), because X2+10=0 has no real roots. (The
parabola does not intersect the X axis.)
To keep your function from crashing the program, well have it simply return 0 in this case. (This is simplifying things, but it will do for now; at least its better than a program crash.)
Modify your function from Exercise 3 above to start with an IF statement that will have it
2
print an error message and return zero if the discriminant (B -4AC) is less than zero.
(Your program should not execute the sqrt() line if it fails this discriminant test.)
3
Challenge Exercise 5: Complex Roots of Quadratics (EXTRA CREDIT):
The quadratic formula will always work to find the roots to any quadratic equation, whether those roots are real or complex. To handle complex roots, however, requires a bit more sophistication.
Specifically, the discriminant check described in Exercise 4 can be used to check if the square root results in a real or imaginary value. If imaginary, this is added to the imaginary part of the result.
For a bigger challenge and up to 30% extra credit on this lab, modify Exercise 4 above to output the answer as a complex number. For full credit, implement the function as follows:
complex complexQuadratic(double a, double b, double c);
This function should return a value of type complex, as defined in the typedef in the lecture slides:
typedef struct{ double real;
double imag; }complex;
(Note that even though were allowing complex roots here, were still just looking for one root specifically, the positive one.)
Rubric for extra credit:
Attempted, but code doesnt compile: Compiles, but throws an error or wrong result: Gives correct result but doesnt use the typedef: Correct result and correct use of typedef:
+1/2 point +1 point +2 points +3 points
(This is another typical technical interview style question for C programmers.)
Write a brief lab report detailing your work.
End of Lab #2.
4
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
