{"id":25841922,"url":"https://github.com/courseworks/aut_bp_2024_fall_hw5","last_synced_at":"2026-03-04T03:02:47.484Z","repository":{"id":265957834,"uuid":"896881084","full_name":"courseworks/AUT_BP_2024_Fall_HW5","owner":"courseworks","description":"The fifth assignment for AUT's Basic Programming course (Fall 2024) focuses on advanced C programming techniques. Students will implement the Newton-Raphson method for root finding, create `map` and `filter` functions using function pointers, and build an HTML tag generator using variable-length arguments.","archived":false,"fork":false,"pushed_at":"2024-12-05T08:27:01.000Z","size":129,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-01T05:32:48.733Z","etag":null,"topics":["basic-programming-course","c-programming","filter","html-tag-builder","map","newton-raphson"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/courseworks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-01T14:35:43.000Z","updated_at":"2024-12-05T08:27:04.000Z","dependencies_parsed_at":"2024-12-03T20:16:03.697Z","dependency_job_id":null,"html_url":"https://github.com/courseworks/AUT_BP_2024_Fall_HW5","commit_stats":null,"previous_names":["courseworks/aut_bp_2024_fall_hw5"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/courseworks/AUT_BP_2024_Fall_HW5","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/courseworks%2FAUT_BP_2024_Fall_HW5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/courseworks%2FAUT_BP_2024_Fall_HW5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/courseworks%2FAUT_BP_2024_Fall_HW5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/courseworks%2FAUT_BP_2024_Fall_HW5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/courseworks","download_url":"https://codeload.github.com/courseworks/AUT_BP_2024_Fall_HW5/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/courseworks%2FAUT_BP_2024_Fall_HW5/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30070479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T01:03:42.280Z","status":"online","status_checked_at":"2026-03-04T02:00:07.464Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["basic-programming-course","c-programming","filter","html-tag-builder","map","newton-raphson"],"created_at":"2025-03-01T05:32:46.258Z","updated_at":"2026-03-04T03:02:47.479Z","avatar_url":"https://github.com/courseworks.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n\u003cstrong\u003eAUT_BP_2024_Fall Homework 5\u003c/strong\u003e\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cstrong\u003e Deadline: 16th of Azar - Friday - 23:59 o'clock\u003c/strong\u003e\n\u003c/p\u003e\n\n---\n\n## **Question 1: Implementing Newton-Raphson Method for Root Finding**\n\n### **Objective**\n\nIn 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`.\n\n### **Explanation of Newton-Raphson Method**\n\nThe 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.\n\nThe formula used in each iteration is:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.latex?x_{n+1}=x_n-\\frac{f(x_n)}{f'(x_n)}\" alt=\"Newton-Raphson formula\" style=\"width: 250px;\"\u003e\n\u003c/p\u003e\n\nIn 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.\n\n**For simplicity, You may assume that the function passed to `newtonRaphson` has exactly one real root.**\n\n### **Function Prototype**\n\n```c\ndouble newtonRaphson(double (*func)(double), double initial);\n\ndouble derivative(double (*func)(double), double x);\n```\n\n### **Detailed Description**\n\n1. **`newtonRaphson`**: This function takes a function pointer `func` (to the function whose root we are trying to find) and an initial guess `initial`. 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.\n\n2. **`derivative`**: This function computes the derivative of the given function `func` at a point `x`. The derivative is approximated using the difference quotient:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://latex.codecogs.com/svg.latex?f'(x)%20%5Capprox%20%5Cfrac%7Bf(x+h)%20-%20f(x)%7D%7Bh%7D\" alt=\"Derivative approximation formula\" style=\"width: 250px\"\u003e\n\u003c/p\u003e\n\nWhere \\( h \\) is a small value (e.g., \\( h = 0.0001 \\)).\n\nThe 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.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./resource/NewtonIteration_Ani.gif\" alt=\"Newton Raphson Animation\" style=\"width: 80%;\"\u003e\n\u003c/p\u003e\n\n### **Example**\n\n#### **Input**\n\n```c\ndouble func(double x) {\n    return x*x - 4;  // Example function f(x) = x^2 - 4\n}\n```\n\n#### **Function Call**\n\n```c\ndouble root = newtonRaphson(func, -1.0);\n```\n\n#### **Expected Output**\n\nFor 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.\n\n### **Notes**\n\n-   The `newtonRaphson` function should stop when the absolute difference between two consecutive guesses is less than \\( 1e-6 \\).\n-   You may assume that the function passed to `newtonRaphson` has exactly one real root.\n\n---\n\n## **Question 2: Implementing Map and Filter Functions Using Pointer to Function**\n\n### **Objective**\n\nIn 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.\n\n### **Function Prototypes**\n\n```c\nvoid map(double *arr, int size, double (*func)(double));\n\ndouble* filter(const double *arr, int size, bool (*func)(double));\n```\n\n### **Detailed Description**\n\n1. **`map`**:\n   - The `map` function takes an array of integers `arr`, the size of the array `size`, and a pointer to a function `func` that outputs a number on each element of the array.\n   - The `map` function should apply the function `func` to each element of the array and modify the array in place. The map function should not return anything.\n\n   **Example Usage**:\n\n   ```c\n   double square(double x) {\n       return x*x;  // Example function to square the number\n   }\n\n   double arr[] = {1, 2, 3, 4};\n   int size = sizeof(arr) / sizeof(arr[0]);\n\n   map(arr, size, square);\n   ```\n\n   After running the `map` function, the array `arr` should be modified to `{1.0, 4.0, 9.0, 16.0}`.\n\n2. **`filter`**:\n   - The `filter` function takes an array of integers `arr`, the size of the array `size`, and a pointer to a function `func` that returns a boolean indicating whether an element should be included in the resulting array.\n   - The `filter` function should create a new array that contains only the elements for which `func` returns `true`. You should dynamically allocate memory for the result array as you identify elements that pass the test.\n\n   **Example Usage**:\n\n   ```c\n   bool isEven(double x) {\n       return ((int)x % 2 == 0);  // Example function to check if the number is even\n   }\n\n   int arr[] = {1, 2, 3, 4, 5, 6};\n   int size = sizeof(arr) / sizeof(arr[0]);\n\n   int *result = filter(arr, size, isEven);\n   ```\n\n   After running the `filter` function, the new array `result` should contain `{2, 4, 6}`.\n\n### **Notes**\n\n- The `map` function will modify the original array directly and does not return anything.\n- 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.\n\n---\n\n## **Question 3: Creating an HTML Tag Generator Using Variable Length Arguments**\n\n### **Objective**\n\nIn 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.\n\n### **Explanation**\n\nThe 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.\n\nThe 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.\n\n### **Function Prototype**\n\n```c\n#include \u003cstdarg.h\u003e\n\nchar* createHTMLTag(const char* tag, int count, ...);\n```\n\n### **Detailed Description**\n\n1. **`createHTMLTag`**: This function generates an HTML tag as a string.\n   - It takes the tag name as a string (e.g., `\"a\"`, `\"div\"`) and an integer `count`, which indicates how many attribute-value pairs will follow.\n   - The function uses variable-length arguments to accept pairs of attribute names and their corresponding values. The attributes are provided as `const char*` pairs.\n   - The function constructs the HTML tag string by appending each attribute to the tag in the form of `attribute=\"value\"`.\n   - The function must return a dynamically allocated string that represents the complete HTML tag.\n\n   **Key Concepts**:\n   - Use `va_list`, `va_start()`, and `va_arg()` to handle variable-length arguments.\n   - Dynamically allocate memory for the resulting string since the size of the tag depends on the number of attributes.\n   - Ensure proper memory management by allocating enough space and returning the string.\n\n### **Example Usage**\n\n```c\nint main() {\n\n    char* html = createHTMLTag(\"a\", 2, \"href\", \"https://example.com\", \"target\", \"_blank\");\n    printf(\"%s\\n\", html);  // Output: \u003ca href=\"https://example.com\" target=\"_blank\"\u003e\u003c/a\u003e\n    \n    free(html);\n    return 0;\n\n}\n```\n\n### **Expected Output**\n\nFor the example call:\n\n```c\nchar* html = createHTMLTag(\"a\", 2, \"href\", \"https://example.com\", \"target\", \"_blank\");\n```\n\nThe output will be:\n\n```html\n\u003ca href=\"https://example.com\" target=\"_blank\"\u003e\u003c/a\u003e\n```\n\n### **Notes**\n\n- The function uses variable-length arguments to handle a flexible number of attribute-value pairs.\n- 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.\n- After building the string, ensure that it is null-terminated and properly formatted.\n\n---\n\nIf you have any questions regarding the homework, feel free to reach out:\n\n-   **Teaching Assistant**: Seyyed Mohammad Hamidi\n-   **Telegram Group**: [t.me/AUT_BP_Fall_2024](https://t.me/AUT_BP_Fall_2024)\n-   **Github**: [github.com/smhamidi](https://github.com/smhamidi)\n\n---\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./resource/NoWarInBaSingSe.webp\" alt=\"No War in ba sing se\" style=\"width: 40%;\"\u003e\n\u003c/p\u003e\n\n**Best Regards, [Hamidi](https://github.com/smhamidi)**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcourseworks%2Faut_bp_2024_fall_hw5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcourseworks%2Faut_bp_2024_fall_hw5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcourseworks%2Faut_bp_2024_fall_hw5/lists"}