Deadline: 16th of Azar - Friday - 23:59 o'clock
In this question, you will implement the Newton-Raphson method for finding the root of a function. The function should accept a pointer to the function whose root is to be found, along with the initial guess. The algorithm must continue iterating until the difference between two consecutive iterations is less than 1e-6
.
The Newton-Raphson method is an iterative technique used to find the root of a real-valued function. The algorithm begins with an initial guess initial
, and then iteratively refines this guess to get closer to the actual root.
The formula used in each iteration is:
In this implementation, the algorithm will continue iterating until the absolute difference between two consecutive approximations is smaller than a tolerance of ( 1e-6 ), indicating that the solution has converged.
For simplicity, You may assume that the function passed to newtonRaphson
has exactly one real root.
double newtonRaphson(double (*func)(double), double initial);
double derivative(double (*func)(double), double x);
-
newtonRaphson
: This function takes a function pointerfunc
(to the function whose root we are trying to find) and an initial guessinitial
. It applies the Newton-Raphson method iteratively until the difference between two consecutive approximations is less than ( 1e-6 ). The function will return the estimated root. -
derivative
: This function computes the derivative of the given functionfunc
at a pointx
. The derivative is approximated using the difference quotient:
Where ( h ) is a small value (e.g., ( h = 0.0001 )).
The Newton-Raphson method is stopped when the difference between two consecutive approximations is smaller than ( 1e-6 ), which ensures that the algorithm has converged to a solution.
double func(double x) {
return x*x - 4; // Example function f(x) = x^2 - 4
}
double root = newtonRaphson(func, -1.0);
For the example function ( f(x) = x^2 - 4 ), the root should be approximately ( 2.0 ), since the exact root is ( x = 2 ). The function newtonRaphson
should return a value close to ( 2.0 ) after running the algorithm.
- The
newtonRaphson
function should stop when the absolute difference between two consecutive guesses is less than ( 1e-6 ). - You may assume that the function passed to
newtonRaphson
has exactly one real root.
In this question, you will implement two functions, map
and filter
, which operate on an array and a function provided as a pointer. Both functions should accept the array size and a pointer to the function to be applied to each element of the array. The map
function modifies the array in place, while the filter
function creates a new array containing only the elements that pass a certain condition.
void map(double *arr, int size, double (*func)(double));
double* filter(const double *arr, int size, bool (*func)(double));
-
map
:- The
map
function takes an array of integersarr
, the size of the arraysize
, and a pointer to a functionfunc
that outputs a number on each element of the array. - The
map
function should apply the functionfunc
to each element of the array and modify the array in place. The map function should not return anything.
Example Usage:
double square(double x) { return x*x; // Example function to square the number } double arr[] = {1, 2, 3, 4}; int size = sizeof(arr) / sizeof(arr[0]); map(arr, size, square);
After running the
map
function, the arrayarr
should be modified to{1.0, 4.0, 9.0, 16.0}
. - The
-
filter
:- The
filter
function takes an array of integersarr
, the size of the arraysize
, and a pointer to a functionfunc
that returns a boolean indicating whether an element should be included in the resulting array. - The
filter
function should create a new array that contains only the elements for whichfunc
returnstrue
. You should dynamically allocate memory for the result array as you identify elements that pass the test.
Example Usage:
bool isEven(double x) { return ((int)x % 2 == 0); // Example function to check if the number is even } int arr[] = {1, 2, 3, 4, 5, 6}; int size = sizeof(arr) / sizeof(arr[0]); int *result = filter(arr, size, isEven);
After running the
filter
function, the new arrayresult
should contain{2, 4, 6}
. - The
- The
map
function will modify the original array directly and does not return anything. - The
filter
function will dynamically allocate memory for a new array. Make sure to reallocate memory each time a new element passes the filter, and ensure you return the new array.
In this question, you will implement a function that dynamically generates an HTML tag with attributes using variable-length arguments in C. This exercise will help you understand how to work with variable-length arguments and how to build dynamic strings based on input.
The function will take a tag name as a string, followed by a series of attribute-value pairs. It will generate a string representing an HTML tag with the given attributes.
The concept of variable-length arguments (...
) in C allows you to pass a variable number of arguments to a function. You will use the stdarg.h
library to handle these arguments.
#include <stdarg.h>
char* createHTMLTag(const char* tag, int count, ...);
-
createHTMLTag
: This function generates an HTML tag as a string.- It takes the tag name as a string (e.g.,
"a"
,"div"
) and an integercount
, which indicates how many attribute-value pairs will follow. - The function uses variable-length arguments to accept pairs of attribute names and their corresponding values. The attributes are provided as
const char*
pairs. - The function constructs the HTML tag string by appending each attribute to the tag in the form of
attribute="value"
. - The function must return a dynamically allocated string that represents the complete HTML tag.
Key Concepts:
- Use
va_list
,va_start()
, andva_arg()
to handle variable-length arguments. - Dynamically allocate memory for the resulting string since the size of the tag depends on the number of attributes.
- Ensure proper memory management by allocating enough space and returning the string.
- It takes the tag name as a string (e.g.,
int main() {
char* html = createHTMLTag("a", 2, "href", "https://example.com", "target", "_blank");
printf("%s\n", html); // Output: <a href="https://example.com" target="_blank"></a>
free(html);
return 0;
}
For the example call:
char* html = createHTMLTag("a", 2, "href", "https://example.com", "target", "_blank");
The output will be:
<a href="https://example.com" target="_blank"></a>
- The function uses variable-length arguments to handle a flexible number of attribute-value pairs.
- You must ensure the memory allocated is sufficient to hold the resulting string. The string will contain the tag name, the attributes, and the closing tag.
- After building the string, ensure that it is null-terminated and properly formatted.
If you have any questions regarding the homework, feel free to reach out:
- Teaching Assistant: Seyyed Mohammad Hamidi
- Telegram Group: t.me/AUT_BP_Fall_2024
- Github: github.com/smhamidi
Best Regards, Hamidi