<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Anan's Sight]]></title><description><![CDATA[Hindsight, Insight, and Foresight
Exploring the frontiers of technology with Khandoker Kafi Anan: From coding to innovation and beyond.]]></description><link>https://blog.khandokeranan.com</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 04:53:06 GMT</lastBuildDate><atom:link href="https://blog.khandokeranan.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[SDLC & Software Management Skills Every Tech Guy Needs]]></title><description><![CDATA[Software development life cycle (SDLC) is a structured process to design, develop, and test good-quality software. Here are the stages of the SDLC:

Problem Identification (Cost Benefit Analysis)

Requirement Analysis (Interactive / Unobtrusive metho...]]></description><link>https://blog.khandokeranan.com/sdlc-software-management-skills-every-tech-guy-needs</link><guid isPermaLink="true">https://blog.khandokeranan.com/sdlc-software-management-skills-every-tech-guy-needs</guid><category><![CDATA[SDLC]]></category><category><![CDATA[software development]]></category><category><![CDATA[System Design]]></category><category><![CDATA[requirement analysis]]></category><category><![CDATA[#project managment]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Wed, 06 Nov 2024 07:13:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1730852587762/ce5e7676-2bfd-4b8d-92ec-ef1e42a2c735.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Software development life cycle (SDLC) is a structured process to design, develop, and test good-quality software. Here are the stages of the SDLC:</p>
<ol>
<li><p>Problem Identification (Cost Benefit Analysis)</p>
</li>
<li><p>Requirement Analysis (Interactive / Unobtrusive method to analyze the project)</p>
</li>
<li><p>System Analysis (CASE Tools, Activity and Framework selection, Process Modeling - Prototyping, Agile, RAD, XP, Scrum, V-type, Waterfall, Concurrent)</p>
</li>
<li><p>System Designing (App Architecture, Usecase, UML, Sequence, State chart Diagram etc.)</p>
</li>
<li><p>Development and Documentation (Pair programming, Code reviewing, Code refactoring, SOLID principle)</p>
</li>
<li><p>Testing and Maintenance (Unit testing, Integration testing, System testing, Acceptance Testing)</p>
</li>
<li><p>Implementation And Evaluation (Deployment strategy, Security enhancement, Bug reporting, Feedback analysis)</p>
</li>
</ol>
<blockquote>
<p><strong>Interesting fact 😮😮😮</strong><br />Time spent on maintenance typically ranges from 48-60 percent of total time</p>
</blockquote>
<h1 id="heading-problem-identification">Problem Identification</h1>
<p>For selecting a project in an organization, it must meet five criteria:</p>
<ol>
<li><p>Backed by management</p>
</li>
<li><p>Having timed appropriate resources</p>
</li>
<li><p>It must move the business toward the attainment of its goal</p>
</li>
<li><p>Cost-effective</p>
</li>
<li><p>Important enough to select over other projects</p>
</li>
</ol>
<h3 id="heading-cost-benefit-analysis"><strong>Cost Benefit Analysis</strong></h3>
<p>We want to gain more benefits from the project than from its development and running costs over time. In this scenario, we may be concerned about how long it will take to return the investment or the profit percentage compared with the initial investment.</p>
<ol>
<li><p>Payback Analysis: Analysis that finds the unit of time when investors will get their money back.</p>
<p> $$payback \space period = total \space investment \div Annual \space cash \space flow$$</p>
</li>
<li><p>Net Present Value (NPV): This analysis calculates the difference between cash inflows and outflows over time. It’s unique because it uses the time-value perspective of the invested money and considers discounted cash flow reflecting risk, inflation, or opportunity cost.</p>
<p> $$npv = \sum^t_0 {cashflow \space X \space discounted factor}$$</p>
</li>
<li><p>Return on Investment: This analysis shows how much return you will get compared to the initial investment.</p>
<p> $$ROI = average\space net\space profit \space\div total \space investment$$</p>
</li>
</ol>
<p>We should check the project’s feasibility before requirement analysis: a. Technical b. Economical c. Operational.</p>
<p>We must plan its high-level activities and estimated time in this planning phase. We can use the GANTT chart or PERT to get a rough idea of the estimated time for the whole project. Here is an example of a GANTT chart:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F7aba5e45-0abf-4a80-97c1-c432b158ab99%2Fimage.png?table=block&amp;id=12ead486-0876-8097-b5ea-fa9a12bf0707&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h1 id="heading-requirement-analysis">Requirement Analysis</h1>
<p>To analyze the project's requirements, we must gather information from its stakeholders, documents, files, or the current system.</p>
<p>An analyst can gather information regarding the project based on two approaches: <strong>Interactive</strong> (Involvement of individuals) and <strong>Unobtrusive</strong> ( Sampling, Observing, Analyzing From files, documents, or current system behaviors / applying <a target="_blank" href="https://www.sciencedirect.com/science/article/abs/pii/0378720684900028#:~:text=STROBE%3A%20A%20structured%20approach%20to%20observation%20of%20the%20decision%2Dmaking%20environment,-Author%20links%20open"><strong>STROBE</strong></a>)</p>
<h3 id="heading-question-types"><strong>Question Types</strong></h3>
<p>Before collecting data, we must know that Information can be derived from two types of questions: Open-ended and closed-ended questions. Here are the significant differences between open-end and close-end questions:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Aspects</strong></td><td><strong>Open-end</strong></td><td><strong>Close-end</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Precision of Data</td><td>Low</td><td>High</td></tr>
<tr>
<td>Efficient use of time</td><td>Low</td><td>High</td></tr>
<tr>
<td>Reliability of Data</td><td>Low</td><td>High</td></tr>
<tr>
<td>Breadth and Depth</td><td>Much</td><td>Little</td></tr>
<tr>
<td>Interviewer Skill Requirement</td><td>Much</td><td>Little</td></tr>
<tr>
<td>Ease of Analysis</td><td>Difficult</td><td>Easy</td></tr>
</tbody>
</table>
</div><h3 id="heading-question-arrangement"><strong>Question Arrangement</strong></h3>
<p>We can arrange questions in three exciting formats:</p>
<ol>
<li><p><strong>Pyramid</strong> (Close-end → Open-end): It’s a helpful technique when the responder needs to be warmed up about the topic.</p>
</li>
<li><p><strong>Funnel</strong> (Open-end → Close-end): It’s useful when we want to connect emotionally with the responder by asking broad questions.</p>
</li>
<li><p><strong>Diamond</strong> (Close-end → Open-end → Close-end): This is an excellent way to conduct an interview, but it takes more time than the Pyramid or Funnel structure.</p>
</li>
</ol>
<h3 id="heading-an-interactive-way-to-collect-information">An interactive way to collect information</h3>
<p>Here are the ways to collect data from stakeholders of the project:</p>
<ol>
<li><p><strong>Interviewing:</strong></p>
<p> To initiate an interview, we must study specific background material regarding the project. Then, we will establish the interview’s objectives. Next, we should decide whom to call for an interview. After that, we will prepare the interviewee. Now, we will set up a structure of questions (Pyramid, Funnel, or Diamond). Finally, we must close the interview by asking if the interviewee has something to talk about, summarizing and providing feedback, asking whom we should talk with next if required, asking for the following schedule if needed, and appreciating for joining the interview.</p>
</li>
<li><p><strong>User stories:</strong></p>
<p> Stories are a remarkable way to understand the underlying problems of the current system. But they take more time than interviews, and sometimes, listening to stories is not efficient enough.</p>
</li>
<li><p><strong>JAD:</strong></p>
<p> Joint Application Design is a process for collecting information regarding a project in a group-based session. It usually happens where we need to sit within a group to discuss and talk in an offsite location. If the Pre-planning and follow-up report are incomplete, such a meeting would not be fruitful.</p>
</li>
<li><p><strong>Questionnaires:</strong></p>
<p> We can collect data by filling out a questionnaire form. Electrical forms are cheaper and easy to store for future use.</p>
</li>
</ol>
<p>After gathering all the information about the project, we must write an <a target="_blank" href="https://www.perforce.com/blog/alm/how-write-software-requirements-specification-srs-document">SRS</a> (Software Requirement Specification) so that we can validate upon testing that this software meets all the requirements.</p>
<h1 id="heading-system-analysis">System Analysis</h1>
<p>First, we should introduce ourselves with what is software actually.</p>
<p>Actually, software is a <strong>set of instructions</strong> (computer program) that, when executed, provides desired features, functions, and performance where the user can modify the data according to <strong>data structures</strong> and has its own <strong>documentation</strong> that describes its operation. And the software doesn’t wear out as much hardware because it doesn’t affect outer environment conditions.</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Ff2c799d9-a277-44b3-9759-2381dff364c5%2FScreenshot_2024-10-30_at_1.29.51_AM.png?table=block&amp;id=12ead486-0876-8011-97a1-dac7baf6a503&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<p>For analyzing a system, we may need to know whether the estimated size of the project is large or small. Larger projects need three things - Efficiency of work, Reliability, Ease of Maintenance, and Guaranteed Performance. That’s why we use one kind of activity framework called Umbrella Activities.</p>
<h3 id="heading-umbrella-activity"><strong>Umbrella Activity</strong></h3>
<p>The core activities of this policy are shown below:</p>
<ol>
<li><p>Formal Technical Interview</p>
</li>
<li><p>Software Project Management</p>
</li>
<li><p>Software Configuration Management</p>
</li>
<li><p>Software Quality Assurance</p>
</li>
<li><p>Work product preparation and production</p>
</li>
<li><p>Risk Management</p>
</li>
<li><p>Reusability</p>
</li>
<li><p>Measurements</p>
</li>
</ol>
<h3 id="heading-framework-activity">Framework Activity</h3>
<p>This activity is a good solution for small to mid projects because it only focuses on the below activities -</p>
<ol>
<li><p>Communication</p>
</li>
<li><p>Planning</p>
</li>
<li><p>Modeling</p>
</li>
<li><p>Construction</p>
</li>
<li><p>Deployment</p>
</li>
</ol>
<p>Now, let’s learn about process modeling, a structured way to develop software that aligns with a sequence of tasks and activities and sets up milestones and deliverables.</p>
<blockquote>
<p><strong>Interesting Fact 😮 😮 😮</strong><br />Do you know the difference between Milestones and deliverables? Ans: Milestones are short-rewarding activities that are submitted to project manager and deliverables are required functionalities that are submitted to the client. All the deliverables are milestones but not all the milestones are deliverables.</p>
</blockquote>
<h3 id="heading-process-model"><strong>Process Model</strong></h3>
<p>There are many software development models available. I have always preferred Scrum for long-term projects and Extreme Programming (XP) for complex projects.</p>
<p><strong>Baseline Models Comparison</strong></p>
<hr />
<p>Here is a comparison table showing key aspects of the <strong>Waterfall</strong>, <strong>Incremental</strong>, and <strong>Concurrent</strong> process models:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Aspect</strong></td><td><strong>Waterfall Model</strong></td><td><strong>Incremental Model</strong></td><td><strong>Concurrent Model</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Development Approach</strong></td><td>Linear</td><td>builds in increments</td><td>Parallel</td></tr>
<tr>
<td><strong>Phases</strong></td><td>Fixed (communication, planning, modeling, construction, and deployment)</td><td>Repeated waterfall phases for each increment</td><td>Activities overlap (none → under development → under review → baseline → done → awaiting changes → under revision)</td></tr>
<tr>
<td><strong>Client Feedback</strong></td><td>phase-end reviews</td><td>Regular feedback after each increment</td><td>Continuous feedback on parallel tasks</td></tr>
<tr>
<td><strong>Risk Management</strong></td><td>Low flexibility</td><td>Moderate</td><td>High adaptability</td></tr>
<tr>
<td><strong>Suitable Projects</strong></td><td>Stable requirements</td><td>Flexible projects (e.g., enterprise applications)</td><td>Complex, dynamic projects needing high adaptability (e.g., real-time systems, R&amp;D)</td></tr>
</tbody>
</table>
</div><p><strong>Efficient Models Comparison</strong></p>
<hr />
<p>Here’s a comparison table highlighting the differences between <strong>Agile Scrum</strong>, <strong>Agile Extreme Programming (XP)</strong>, and the <strong>V-Model</strong>:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Aspect</strong></td><td><strong>Agile Scrum</strong></td><td><strong>Agile Extreme Programming (XP)</strong></td><td><strong>V-Model</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Development Approach</strong></td><td>Time-boxed sprints</td><td>Frequent releases and updates</td><td>Sequential follows a V-shaped flow</td></tr>
<tr>
<td><strong>Phases</strong></td><td>Divided into sprints (e.g., sprint planning, development, sprint review, retrospective)</td><td>XP Planning (Stories) → XP Design (CRC card, KISS) → XP Coding (Unit test writing, Pair, refactoring) → XP Testing</td><td>Requirement, Architecture Design, Component Design, Code Generation → Unit, Integration, System, Integration Testing</td></tr>
<tr>
<td><strong>Client Feedback</strong></td><td>Regular feedback at the end of each sprint</td><td>Continuous feedback through frequent releases</td><td>Limited feedback, typically after each phase or after validation</td></tr>
<tr>
<td><strong>Risk Management</strong></td><td>Continuous adjustments, risk identified and mitigated each sprint</td><td>High adaptability, risks are addressed through continuous testing</td><td>High risk in early stages if changes occur late in development</td></tr>
<tr>
<td><strong>Suitable Projects</strong></td><td>Complex, evolving projects requiring collaboration (e.g., web apps, customer-facing products)</td><td>Highly flexible projects with changing requirements (e.g., software tools, utilities)</td><td>Well-defined requirements, safety-critical or regulated projects (e.g., healthcare, avionics)</td></tr>
</tbody>
</table>
</div><h3 id="heading-picture-time-for-process-modeling"><strong>Picture time for process modeling</strong></h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Ff258440c-cc4a-416a-a7ed-f3ad16e26587%2Fimage.png?table=block&amp;id=12ead486-0876-80be-b2dc-d2d776db6ed3&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=660&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Ff0611416-b0ec-485f-a4b5-4623e293f904%2Fimage.png?table=block&amp;id=12ead486-0876-8025-83cd-fbfc017bfb9d&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=660&amp;userId=&amp;cache=v2" alt class="image--center mx-auto" /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Fda4fe630-4f82-4f6d-ae70-2972bae8cfbb%2Fimage.png?table=block&amp;id=12ead486-0876-804f-be33-f86b0ae3b39d&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=660&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F66a4166f-3d1b-4cb1-ae9f-707c9eac4f0e%2Fimage.png?table=block&amp;id=12ead486-0876-80d9-b375-e1a614c79064&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=660&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F88bbeb7d-c637-4e27-991e-e6ae53ecfd91%2Fimage.png?table=block&amp;id=12ead486-0876-80e8-951c-e0848dae66f8&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=660&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-basic-estimation-using-loc">Basic Estimation using LOC</h3>
<p>Proper cost estimation is a multi-variant strategy for running a sustainable business. Without proper software design analysis, we cannot arrive at a logical costing of developing the software as we need people, a set of skills, identification of OTS and non-OTF components, tools, hardware, and network resources.</p>
<p>LOC means Line of code. Based on LOC and the cost of the person-month strategy, we can estimate a cost for a specific software. To do this→</p>
<ol>
<li>Functional decomposition of the requirements</li>
</ol>
<p>Estimate the size of each function LOC</p>
<p>Example Table:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature Name</td><td>Short Description</td><td>LOC (Optimistic)</td><td>LOC (Most Likely)</td><td>LOC (Pessimistic)</td></tr>
</thead>
<tbody>
<tr>
<td></td></tr>
</tbody>
</table>
</div><p>$$Total \space LOC = (\frac{{\text{LOC_Optimistic} + 4 \times \text{LOC_Most_Likely} + \text{LOC_Pessimistic}}}{6})$$</p><ol>
<li><p>Calculate the price</p>
<ol>
<li><p>$$[ \text{Effort} = \frac{\text{Total LOC}}{\text{Productivity Rate}} ]$$</p></li>
<li><p>$$[ \text{Estimated Cost} = \text{Effort} \times \text{Labour Rate} ]$$</p></li>
</ol>
</li>
</ol>
<blockquote>
<p><strong>Wow Fact 😮 😮 😮</strong></p>
<p>The Lines of Code (LOC) range can vary based on the industry, but general estimates for project sizes are often categorized as follows:</p>
<ul>
<li><p><strong>Small Project</strong>: Up to 10,000 LOC</p>
</li>
<li><p><strong>Medium Project</strong>: 10,000 to 100,000 LOC</p>
</li>
<li><p><strong>Large Project</strong>: 100,000 LOC and above</p>
</li>
</ul>
</blockquote>
<h1 id="heading-system-designing">System Designing</h1>
<p>When it comes to designing a system, a minimum set of skills is required to proceed with the development of software. This is why I will overview the concept and an example of each diagram used for designing software. They are:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Aspect</td><td>Short Description</td></tr>
</thead>
<tbody>
<tr>
<td><strong>App Architecture Diagram</strong></td><td>Provides a high-level view of the system architecture</td></tr>
<tr>
<td><strong>Use Case Diagram</strong></td><td>Actor-based interactions with different use cases within the system.</td></tr>
<tr>
<td><strong>Entity Relationship Diagram (ERD)</strong></td><td>Illustrates the data structure for a database.</td></tr>
<tr>
<td><strong>State Machine Diagram</strong></td><td>Represents the different states an object can be in, as well as transitions between these states.</td></tr>
<tr>
<td><strong>Relational Schema</strong></td><td>Defines the logical structure of a relational database.</td></tr>
<tr>
<td><strong>UML Class Diagram</strong></td><td>Displaying classes with their variables and operations and relationships (like inheritance, association, aggregation, composition) among classes.</td></tr>
<tr>
<td><strong>Sequence Diagram</strong></td><td>Depicting the sequence of input-output to complete a process between objects.</td></tr>
<tr>
<td><strong>Activity Diagram</strong></td><td>Visualizes the flow of control in a system using a flowchart</td></tr>
</tbody>
</table>
</div><h3 id="heading-use-case-diagram-components-and-example">Use Case Diagram components and example</h3>
<p>The components of this diagram are:</p>
<ol>
<li><p>Primary Actor</p>
</li>
<li><p>Secondary Actors</p>
</li>
<li><p>Relations</p>
<ol>
<li><p>Association</p>
</li>
<li><p>Generalization</p>
</li>
<li><p>Dependency (Include (mandatory), Exclude (not mandatory))</p>
</li>
</ol>
</li>
</ol>
<p>Examples:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F3fe961e2-1998-4505-b60f-9faa33b0138b%2Fimage.png?table=block&amp;id=12ead486-0876-8024-9fd2-f4da89cbc33b&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-uml-class-diagram">UML Class Diagram</h3>
<p>Here are the components:</p>
<p>Examples:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F8bdf1cb6-5204-44ba-9f64-c30056ee8828%2Fimage.png?table=block&amp;id=12ead486-0876-8092-863b-cfc25b485d5d&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=990&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F7c78c938-2fd4-4be4-acb5-54ba6c7e7d76%2Fimage.png?table=block&amp;id=12ead486-0876-80e4-aa48-c65c1758b021&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=990&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Fec1fcc08-9f01-409a-b36f-ef6ee38d46ba%2Fimage.png?table=block&amp;id=12ead486-0876-8055-8900-e30b4804f90a&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-sequence-diagram">Sequence Diagram</h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F22dbee21-1ef9-4e81-8bdb-87af6d340c85%2Fimage.png?table=block&amp;id=12ead486-0876-80c6-8974-f82ab7f158c8&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-activity-diagram">Activity Diagram</h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F33c071a0-a013-4385-9902-41413f448ef6%2Fimage.png?table=block&amp;id=12ead486-0876-8073-9f7d-f14bb596b756&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=580&amp;userId=&amp;cache=v2" alt /></p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F223b6751-03c8-4d10-a504-c8cf980e87c8%2Fimage.png?table=block&amp;id=12ead486-0876-80a6-b134-e648eca25c2c&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=740&amp;userId=&amp;cache=v2" alt="Example of Order-Payment Diagram" /></p>
<h3 id="heading-state-machine-diagram">State Machine Diagram</h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F8e6be900-9683-4c36-a453-4f9db07122ef%2Fimage.png?table=block&amp;id=12ead486-0876-800b-a739-ec9eddb87cec&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-app-architecture-diagram-i-love-this-diagram">App Architecture Diagram (I love this diagram)</h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Fa7c81d3e-a774-4f86-b83d-e9d8896a8b63%2FApp_Architecture.png?table=block&amp;id=12ead486-0876-8017-b453-e6e26a43bf57&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-relationship-schema-i-prefer-this-over-erd">Relationship Schema ( I prefer this over ERD )</h3>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Fa1cdb60f-f26c-4f66-a258-7e861792be7a%2FDam-Koto_(1).png?table=block&amp;id=12ead486-0876-80a7-a6b6-f086b5c612df&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1420&amp;userId=&amp;cache=v2" alt /></p>
<h1 id="heading-development-and-documentation">Development and Documentation</h1>
<p>I would not like to add details on this topic as it requires a set of programming skills and concepts of the technical stack and how it works. So here are the basics you must know about it.</p>
<h3 id="heading-pair-programming">Pair Programming</h3>
<p>When two developers at a time write higher-level programming instructions for developing software, one can observe the code structure and errors while the other one can write the actual code. It’s used in extreme programming.</p>
<h3 id="heading-code-refactoring">Code Refactoring</h3>
<p>It’s a way of organizing code where both input and output remain the same but are optimized based on performance, code readability, and reusability. We use SOLID principles for refactoring the code.</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F14b1a28b-8589-4d46-9ca1-abe2b86ed5a7%2Fimage.png?table=block&amp;id=12ead486-0876-8040-91c9-d8d44bd81249&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=1300&amp;userId=&amp;cache=v2" alt /></p>
<h1 id="heading-testing-and-maintenance">Testing and Maintenance</h1>
<h3 id="heading-white-box-vs-black-box-testing">White Box vs Black Box Testing</h3>
<p>Here’s a comparison table highlighting the differences between <strong>White Box Testing</strong> and <strong>Black Box Testing</strong>:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Aspect</td><td>White Box Testing</td><td>Black Box Testing</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Focus</strong></td><td>Focuses on code, internal structures, and paths within the application.</td><td>Focuses on input and output without concern for internal processes.</td></tr>
<tr>
<td><strong>Tester's Knowledge</strong></td><td>It requires knowledge of programming, algorithms, and the application's internal design.</td><td>It does not require programming knowledge; testers can be from a non-technical background.</td></tr>
<tr>
<td><strong>Techniques Used</strong></td><td>Techniques include code coverage, control flow, data flow, and path testing.</td><td>Techniques include functional testing, usability testing, performance testing, and regression testing.</td></tr>
<tr>
<td><strong>Testing Level</strong></td><td>Typically performed at the unit or integration level of testing.</td><td>Usually performed at the system or acceptance level of testing.</td></tr>
<tr>
<td><strong>Test Case Design</strong></td><td>Test cases are derived from the source code, focusing on paths, loops, and conditions.</td><td>Test cases are derived from requirements and specifications, focusing on user interactions and expected outcomes.</td></tr>
</tbody>
</table>
</div><h3 id="heading-unit-integration-penetration-system-testing">Unit, Integration, Penetration, System Testing</h3>
<p>Here’s a comparison table that highlights the differences between <strong>Unit Testing</strong>, <strong>Integration Testing</strong>, <strong>Penetration Testing</strong>, and <strong>System Testing</strong>:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Aspect</td><td>Unit Testing</td><td>Integration Testing</td><td>Penetration Testing</td><td>System Testing</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Definition</strong></td><td>Testing individual components or modules in isolation to verify that they function correctly.</td><td>Testing the interaction between integrated components or systems to identify interface defects.</td><td>Simulating attacks on a system to identify security vulnerabilities and weaknesses.</td><td>Testing the complete and integrated software system to evaluate its compliance with specified requirements.</td></tr>
<tr>
<td><strong>Focus</strong></td><td>Verifying the correctness of a specific piece of code (e.g., functions, classes).</td><td>Ensuring that combined components work together as expected.</td><td>Identifying security loopholes and vulnerabilities in the application.</td><td>Validating the overall behavior, performance, and functionality of the entire system.</td></tr>
<tr>
<td><strong>Level of Testing</strong></td><td>Performed at the lowest level of software development (individual units).</td><td>Performed after unit testing and before system testing.</td><td>Performed at various stages but often after system testing.</td><td>Conducted on the entire system after integration testing.</td></tr>
<tr>
<td><strong>Tools Used</strong></td><td>Unit testing frameworks (e.g., JUnit, NUnit, pytest).</td><td>Integration testing tools (e.g., Postman, SoapUI).</td><td>Penetration testing tools (e.g., OWASP ZAP, Burp Suite, Metasploit).</td><td>System testing tools (e.g., Selenium, LoadRunner, QTP).</td></tr>
<tr>
<td><strong>Testers</strong></td><td>Typically performed by developers.</td><td>Developers or testers can perform it.</td><td>Usually conducted by security specialists or ethical hackers.</td><td>Usually conducted by quality assurance (QA) testers.</td></tr>
<tr>
<td><strong>Test Cases</strong></td><td>Tests specific functionalities or methods in isolation.</td><td>Tests interactions and data flow between modules.</td><td>Tests potential exploits, vulnerabilities, and security controls.</td><td>Tests end-to-end scenarios to ensure complete functionality.</td></tr>
</tbody>
</table>
</div><h3 id="heading-maintenance">Maintenance</h3>
<p>The maintenance strategy was discussed shortly in the system analysis process modeling section.</p>
<p><img src="https://images.surferseo.art/b193a7fe-7699-4fb6-8c19-b51ba3a54e4f.png" alt="SDLC Guide: A Comprehensive Guide to Effective Software Maintenance Phase -  Stratoflow" /></p>
<p>This image provides a visual overview of the four main types of software maintenance. They are:</p>
<ol>
<li><p><strong>Corrective Software Maintenance</strong>: This involves fixing bugs or errors that emerge during the software's lifecycle to ensure its correct function.</p>
</li>
<li><p><strong>Preventative Software Maintenance</strong>: This type aims to prevent future problems by anticipating potential issues and improving the software's stability and performance.</p>
</li>
<li><p><strong>Perfective Software Maintenance</strong>: Enhancements are made to improve the software's functionality and usability based on user feedback or new requirements.</p>
</li>
<li><p><strong>Adaptive Software Maintenance</strong>: This involves updating the software to remain compatible with changing environments, such as new operating systems or hardware.</p>
</li>
</ol>
<h1 id="heading-implementation-and-evaluation">Implementation and Evaluation</h1>
<p><img src="https://semaphoreci.com/wp-content/uploads/2022/05/cicd-pipeline-introduction-1024x422-1.jpg" alt="CI/CD Pipeline: A Gentle Introduction - Semaphore" /></p>
<p>Have you heard the name of <strong>CI/CD Pipeline?</strong></p>
<p>A continuous integration and continuous deployment (<a target="_blank" href="https://www.redhat.com/en/topics/devops/what-is-ci-cd">CI/CD) pip</a>eline is a series of steps that must be performed to deliver a new version of software. CI/CD pipelines are a practice focused on improving software delivery via automation throughout the software development life cycle.  Here is an overall picture of how we can implement CI/CD through Jenkins:</p>
<p><img src="https://pbs.twimg.com/media/FTlsJM8WIAEQIKT.jpg:large" alt="eXperienceEngineering on X: &quot;Very nicely explained about CI/CD pipelines  via Jenkins and attack scenario by @sec_r0 https://t.co/cNaWOAhFhP The  flyer covers the below points: 1. What is Jenkins Pipeline? 2. What is  CI/CD?" /></p>
<blockquote>
<p>Here are the components of the CI/CD pipelines:</p>
<p><strong>GitHub</strong></p>
<p>The code will be stored in a GitHub repository. This repository will act as the source of truth for the codebase.</p>
<p><strong>Jenkins</strong></p>
<p>Jenkins will be set up on an EC2 instance for Continuous Integration/Continuous Deployment. Jenkins will handle tasks like testing the code and deploying it to production.</p>
<p><strong>Code Deployment Strategy</strong></p>
<p>When changes are pushed to the GitHub repository, Jenkins will automatically trigger a build and test sequence. If successful, Jenkins will deploy the new version to the EC2 instances.</p>
<p><strong>AWS CodeDeploy (Optional)</strong></p>
<p>AWS CodeDeploy could be used for deployment automation to EC2 instances, which allows handling complex deployment scenarios and performing blue/green deployments.</p>
</blockquote>
<h3 id="heading-deployment">Deployment</h3>
<p>As an AWS lover, I would love to discuss a summary of how we can deploy software using AWS servers.</p>
<p><img src="https://miro.medium.com/v2/resize:fit:1400/1*2oGrX4_Q6Y97LZ7RDj1cfA.gif" alt="Project 4 →Deploy Netflix Clone on Kubernetes | by Aakib | Medium" /></p>
<p><strong>AWS Setup Components Description</strong></p>
<blockquote>
<p>Here are the components of AWS setup that is required for deployments:</p>
<p><strong>EC2 Instances</strong></p>
<p>Use EC2 instances to host your Node.js backend. You can set up an Auto Scaling group to handle load changes.</p>
<p><strong>Elasticache</strong></p>
<p>Elasticache (Redis or Memcached) will be implemented for caching. This will help reduce the load on the database and improve response times.</p>
<p><strong>CloudWatch</strong></p>
<p>CloudWatch will be used for monitoring the application's performance and logging. Alarms will be set to get alerts on specific metrics like CPU usage or error rates.</p>
<p><strong>Route 53</strong></p>
<p>AWS Route 53 will manage DNS for the domain, providing reliable and scalable domain name services.</p>
<p><strong>RDS for MySQL</strong></p>
<p>Amazon RDS will manage the MySQL database. It provides automated backups, patch management, and scaling capabilities.</p>
<p><strong>Elasticache</strong></p>
<p>Elasticache (Redis or Memcached) will be implemented for caching. This will help reduce the load on the database and improve response times.</p>
<p><strong>CloudWatch</strong></p>
<p>CloudWatch will be used for monitoring the application's performance and logging. Alarms will be set to get alerts on specific metrics like CPU usage or error rates.</p>
<p><strong>Route 53</strong></p>
<p>AWS Route 53 will manage DNS for the domain, providing reliable and scalable domain name services.</p>
<p><strong>DynamoDB</strong></p>
<p>Amazon DynamoDB is a serverless, NoSQL database service that allows you to develop modern applications at any scale. As a serverless database, you only pay for what you use and DynamoDB scales to zero, has no cold starts, no version upgrades, no maintenance windows, no patching, and no downtime maintenance.  </p>
<p><strong>AWS Secrets Manager</strong><br />It will be used to handle secrets of the Node.js application. It aids in securely storing, managing, and retrieving secrets such as database credentials and API keys.</p>
</blockquote>
<p><strong>AWS step-by-step points</strong></p>
<p>Below are detailed step-by-step instructions for each component of your deployment strategy:</p>
<p><strong>1. VPC Initialization</strong></p>
<ol>
<li><p><strong>Create a VPC</strong></p>
</li>
<li><p><strong>Create Subnets</strong></p>
</li>
<li><p><strong>Set Up Internet Gateway</strong></p>
</li>
<li><p><strong>Configure Route Tables</strong></p>
</li>
</ol>
<p><strong>2. Connecting Route53 Nameserver</strong></p>
<ol>
<li><p><strong>Create a Hosted Zone</strong></p>
</li>
<li><p><strong>Update Domain Registrar</strong></p>
</li>
</ol>
<p><strong>3. Pushing dotenv to AWS Secrets Manager</strong></p>
<ol>
<li><strong>Create a New Secret</strong></li>
</ol>
<ol start="2">
<li><strong>Set Permissions</strong></li>
</ol>
<p><strong>4. Setting up EC2 and RDS in the same VPC (different subnets)</strong></p>
<p><strong>5. Creating Elasticache Cluster for Memcached within the same VPC</strong></p>
<p><strong>6. Adding DB Connection String to AWS SM</strong></p>
<p><strong>7. Installing Amazon-CloudWatch-Agent</strong></p>
<pre><code class="lang-bash">sudo yum install -y amazon-cloudwatch-agent
</code></pre>
<p><strong>Configure the Agent</strong>: Use <code>amazon-cloudwatch-agent-config-wizard</code> to configure metrics and logs to collect and save the configuration file under <code>/opt/aws/amazon-cloudwatch-agent/bin/config.json</code>.</p>
<p><strong>8. Enabling Detailed Monitoring for EC2</strong></p>
<p><strong>9. Setting up CloudWatch Alarms</strong></p>
<hr />
<p><strong>TO BE CONTINUED…..</strong></p>
]]></content:encoded></item><item><title><![CDATA[How do computers find the value of integration?]]></title><description><![CDATA[Direct integration, also known as symbolic or analytical integration, involves finding an antiderivative or primitive function that represents the area under a curve. However, in many real-world scenarios, the function we're trying to integrate might...]]></description><link>https://blog.khandokeranan.com/how-do-computers-find-the-value-of-integration</link><guid isPermaLink="true">https://blog.khandokeranan.com/how-do-computers-find-the-value-of-integration</guid><category><![CDATA[#numerical-methods]]></category><category><![CDATA[integration]]></category><category><![CDATA[romberg]]></category><category><![CDATA[simpson]]></category><category><![CDATA[trapezoidal]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Fri, 15 Sep 2023 08:23:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1694760390702/207f2386-2f31-4305-8fa2-c49b3ad7c195.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Direct integration, also known as symbolic or analytical integration, involves finding an antiderivative or primitive function that represents the area under a curve. However, in many real-world scenarios, the function we're trying to integrate might not have a simple antiderivative or might be too complex to integrate symbolically. In such cases, numerical methods come to the rescue.</p>
<p>Numerical integration methods provide approximate solutions to definite integrals by breaking down the area under the curve into smaller, more manageable shapes like rectangles, trapezoids, or parabolas. By summing up the areas of these shapes, we can get an approximation of the total area under the curve.</p>
<p><img src="https://i0.wp.com/jeremykun.com/wp-content/uploads/2011/12/trapezoid.png" alt="Rought Estimation on Integration" /></p>
<p><strong>Step Size (h):</strong></p>
<p>$$h = \frac{b-a}{n}$$</p><p> $$ n = number \space of \space intervals$$</p>
<p><strong>Try it out:</strong></p>
<p>$$\int_{0}^{1} x^2 \, dx = \left[ \frac{x^3}{3} \right]_0^1 = \frac{1^3}{3} - \frac{0^3}{3} = \frac{1}{3}$$</p><div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://replit.com/@khandokeranan/Trapezoidal-Integration#main.cpp">https://replit.com/@khandokeranan/Trapezoidal-Integration#main.cpp</a></div>
<p> </p>
<h3 id="heading-trapezoidal-rule"><strong>Trapezoidal Rule</strong></h3>
<p>This method approximates the area under the curve using trapezoids. The area of each trapezoid is computed and then summed up to get the total area.</p>
<p><strong>Main Idea</strong>: Divide the interval into smaller subintervals and approximate the area under the curve using trapezoids over each subinterval.</p>
<p><strong>Visualization:</strong></p>
<p><img src="https://miro.medium.com/v2/resize:fit:1200/1*EK2majk1Ge7-C6we9UGPGw.gif" alt="Trapezoidal Rule: A Method of Numerical Integration | by Kazi Abu Rousan |  Cantor's Paradise" /></p>
<p><strong>Equation:</strong></p>
<p>$$I(h) = h \times (\frac {y_0 + y_n}{2} + y_1 + y_2 +...)$$</p><p><strong>Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">f</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
<span class="hljs-comment">// Example function, you can replace this with any function</span>
<span class="hljs-keyword">return</span> x*x; 
}
<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">trapezoidalRule</span><span class="hljs-params">(<span class="hljs-keyword">double</span> a, <span class="hljs-keyword">double</span> b, <span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">double</span> h = (b - a) / n;
    <span class="hljs-keyword">double</span> result = <span class="hljs-number">0.5</span> * (f(a) + f(b));

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++) {
        result += f(a + i*h);
    }
    <span class="hljs-keyword">return</span> h * result;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">double</span> a = <span class="hljs-number">0.0</span>, b = <span class="hljs-number">1.0</span>; <span class="hljs-comment">// Integration limits</span>
    <span class="hljs-keyword">int</span> n = <span class="hljs-number">100</span>; <span class="hljs-comment">// Number of subintervals</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Approximate value of integral: "</span> &lt;&lt; trapezoidalRule(a, b, n) &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<h3 id="heading-simpsons-13-rule"><strong>Simpsons 1/3 Rule</strong></h3>
<p>This method uses parabolas to approximate the area under the curve.</p>
<p><strong>Main Idea</strong>: Use quadratic polynomials to approximate the function over pairs of subintervals.</p>
<p><strong>Visualization:</strong></p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/f/fc/Simpson%27s_One-Third_Rule.gif" alt="Integration" /></p>
<p><strong>Equations:</strong></p>
<p>$$I(h) = \frac{h}{3} \times ((y_0 + y_n) + 4(y_1 + y_3 + ..) + 2(y_2 + y_4 + ...))$$</p><p><strong>Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">simpsonsOneThirdRule</span><span class="hljs-params">(<span class="hljs-keyword">double</span> a, <span class="hljs-keyword">double</span> b, <span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">double</span> h = (b - a) / n;
    <span class="hljs-keyword">double</span> result = f(a) + f(b);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++) {
        <span class="hljs-keyword">if</span> (i % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
            result += <span class="hljs-number">2</span> * f(a + i*h);
        } <span class="hljs-keyword">else</span> {
            result += <span class="hljs-number">4</span> * f(a + i*h);
        }
    }
    <span class="hljs-keyword">return</span> (h / <span class="hljs-number">3</span>) * result;
}
</code></pre>
<h3 id="heading-simpsons-38-rule"><strong>Simpsons 3/8 Rule</strong></h3>
<p>This method is an extension of Simpson's 1/3 rule and uses cubic polynomials for approximation.</p>
<p><strong>Main Idea</strong>: Use cubic polynomials to approximate the function over sets of three subintervals.</p>
<p><strong>Visualization:</strong></p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/f/fc/Simpson%27s_One-Third_Rule.gif" alt="undefined" /></p>
<p><strong>Equation:</strong></p>
<p>$$I(h) = \frac{3h}{8} \times ((y_0 + y_n) + 3 (y_1 + y_2 + y_4 + ..) + 2(y_3 + y_6 + ..) )$$</p><p><strong>Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">simpsonsThreeEighthRule</span><span class="hljs-params">(<span class="hljs-keyword">double</span> a, <span class="hljs-keyword">double</span> b, <span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">double</span> h = (b - a) / n;
    <span class="hljs-keyword">double</span> result = f(a) + f(b);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++) {
        <span class="hljs-keyword">if</span> (i % <span class="hljs-number">3</span> == <span class="hljs-number">0</span>) {
            result += <span class="hljs-number">2</span> * f(a + i*h);
        } <span class="hljs-keyword">else</span> {
            result += <span class="hljs-number">3</span> * f(a + i*h);
        }
    }

    <span class="hljs-keyword">return</span> (<span class="hljs-number">3</span> * h / <span class="hljs-number">8</span>) * result;
}
</code></pre>
<p><strong>Difference between Trapezoidal and Simpson's method</strong></p>
<p><img src="https://miro.medium.com/v2/resize:fit:1400/1*MRj3f5MrlZuT5lFwIk8lAw.png" alt="How Does Simpson's Rule Work" /></p>
<h3 id="heading-romberg-integration"><strong>Romberg Integration</strong></h3>
<p>Romberg Integration is a powerful numerical integration method that combines the trapezoidal rule with Richardson extrapolation to achieve faster convergence and higher accuracy.</p>
<p><strong>Main Idea:</strong> Romberg Integration starts with the trapezoidal rule and then refines the approximation using Richardson extrapolation. The method takes advantage of the fact that the error in the trapezoidal rule can be expressed as a power series in the step size. By computing the trapezoidal rule for different step sizes and combining the results, the leading error terms can be eliminated, leading to a more accurate approximation.</p>
<p><strong>Details:</strong></p>
<ol>
<li><p><strong>Initialization</strong>: Start with the coarsest approximation using the trapezoidal rule over the entire interval [<em>a</em>,<em>b</em>].</p>
</li>
<li><p><strong>Refinement</strong>: Halve the step size and compute the trapezoidal rule again. This gives a finer approximation.</p>
</li>
<li><p><strong>Richardson Extrapolation</strong>: Use the coarse and fine approximations to compute a new, even more accurate approximation. This is done by eliminating the leading error term.</p>
</li>
</ol>
<p>$$f'(x) = \frac{D(rh)-D(h)r^2}{1-r^2}$$</p><p>$$D(h) = \frac{f(x+h) - f(x-h)}{2h}$$</p><ol>
<li><strong>Iteration</strong>: Repeat the refinement and extrapolation steps until the desired level of accuracy is achieved or a maximum number of iterations is reached.</li>
</ol>
<p><strong>Visualization:</strong></p>
<p><img src="https://miro.medium.com/v2/resize:fit:904/1*UfN9jeY-NN6niP2Rl7rDWw.png" alt="Day 98: Romberg integration. Romberg's method to find a definite… | by  Tomáš Bouda | 100 days of algorithms | Medium" /></p>
<p>The tabular method for calculating Romberg's approximation using h, h/2, h/4, h/8:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694764372739/1252446f-db7a-41e3-9179-ee743279ebfe.png" alt class="image--center mx-auto" /></p>
<p><strong>Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">rombergIntegration</span><span class="hljs-params">(<span class="hljs-keyword">double</span> a, <span class="hljs-keyword">double</span> b, <span class="hljs-keyword">int</span> n, <span class="hljs-keyword">int</span> m)</span> </span>{
    <span class="hljs-keyword">double</span> h = (b - a) / n;
    <span class="hljs-keyword">double</span> R[n+<span class="hljs-number">1</span>][m+<span class="hljs-number">1</span>];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= n; i++) {
        R[i][<span class="hljs-number">0</span>] = <span class="hljs-number">0.5</span> * h * (f(a + i*h) + f(b + i*h));
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt;= m; j++) {
        h /= <span class="hljs-number">2</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= n - <span class="hljs-built_in">pow</span>(<span class="hljs-number">2</span>, j<span class="hljs-number">-1</span>); i++) {
            R[i][j] = <span class="hljs-number">0.5</span> * R[i][j<span class="hljs-number">-1</span>] + h * f(a + i*h + h/<span class="hljs-number">2</span>);
        }
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> k = <span class="hljs-number">1</span>; k &lt;= j; k++) {
            R[<span class="hljs-number">0</span>][k] = R[<span class="hljs-number">0</span>][k<span class="hljs-number">-1</span>] + (R[<span class="hljs-number">0</span>][k<span class="hljs-number">-1</span>] - R[<span class="hljs-number">0</span>][k<span class="hljs-number">-2</span>]) / (<span class="hljs-built_in">pow</span>(<span class="hljs-number">4</span>, k) - <span class="hljs-number">1</span>);
        }
    }

    <span class="hljs-keyword">return</span> R[<span class="hljs-number">0</span>][m];
}
</code></pre>
<p>Thank you for reading the article!</p>
]]></content:encoded></item><item><title><![CDATA[TCP/IP Shortcuts for beginners]]></title><description><![CDATA[Data Communication
Data communication means the exchange of information between two or more devices using some transmission medium.

Effectiveness of Data Communication Layer: (DATJ)

Delivery, 2. Accuracy, 3. Timeliness, 4. Jitter


Components of Da...]]></description><link>https://blog.khandokeranan.com/tcpip-shortcuts-for-beginners</link><guid isPermaLink="true">https://blog.khandokeranan.com/tcpip-shortcuts-for-beginners</guid><category><![CDATA[data communication]]></category><category><![CDATA[tcp/ip-model]]></category><category><![CDATA[OSI]]></category><category><![CDATA[#PPP]]></category><category><![CDATA[protocols]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Sun, 10 Sep 2023 15:22:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1694358127817/c22d0a0d-0198-4e5a-8e98-3d89313f49b0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-data-communication">Data Communication</h1>
<p>Data communication means the exchange of information between two or more devices using some transmission medium.</p>
<ul>
<li><p><strong>Effectiveness of Data Communication Layer: (DATJ)</strong></p>
<ol>
<li>Delivery, 2. Accuracy, 3. Timeliness, 4. Jitter</li>
</ol>
</li>
<li><p><strong>Components of Data Communication</strong></p>
<ol>
<li>Message, 2. Sender, 3. Receiver, 4. Transmission Medium, 5. Protocol</li>
</ol>
</li>
<li><p><strong>Data Representation</strong></p>
<ol>
<li>Text, 2. Number, 3. Images, 4. Audios, 5. Videos</li>
</ol>
</li>
<li><p><strong>Data Flow</strong></p>
<ol>
<li>Simplex, 2. Half Duplex, 3. Full Duplex</li>
</ol>
</li>
<li><p><strong>Network Criteria (PRS)</strong></p>
<ol>
<li><p>Performance →</p>
<p> a. Throughput</p>
<p> The throughput is a measure of how fast we can actually send data through a network</p>
<p> <strong>Unit: bps</strong></p>
<p> b. Latency</p>
<p> The latency or delay defines how long it takes for an entire message to completely arrive at the destination from the time the first bit is sent out from the source.</p>
<p> <strong>Latency = propagation time + transmission time + queuing time + processing delay</strong></p>
<p> <strong>Unit: second</strong></p>
</li>
<li><p>Reliability,</p>
</li>
<li><p>Security</p>
</li>
</ol>
</li>
</ul>
<p>What is a network? Interconnection between devices</p>
<p>Network components → 1. Host, 2. Connecting Devices ( Router, Switch, Modem)</p>
<p>Router → Network to Network Connection</p>
<p>Switch → Device to Device Connection</p>
<p>Modem → Data form exchange</p>
<p>Types of Connections → 1. Wired, 2. Wireless</p>
<p>Wired Connection → 1. Point to Point, 2. Multi-point</p>
<p>Topologies → 1. Bus, 2. Ring, 3. Mesh, 4. Star</p>
<ul>
<li><p><strong>Accessing the Internet</strong></p>
<ol>
<li><p>Using Telephone → Dial-up and DSL</p>
</li>
<li><p>Using Wireless Network → point-to-point WAN</p>
</li>
<li><p>Using Cable Network → Through Cable</p>
</li>
<li><p>Direct Connect to the Internet → ISP</p>
</li>
</ol>
</li>
</ul>
<p>Switches →</p>
<ol>
<li><p>Circuit Switched Network</p>
</li>
<li><p>Packed Switched Network</p>
</li>
</ol>
<p><strong>Two popular Data Communication Models:</strong></p>
<ol>
<li><p>OSI → 1. Application, 2. Presentation, 3. Session, 4. Transport, 5. Network, 6. Data-link, 7. Physical</p>
</li>
<li><p>TCP/IP → 1. Application, 2. Transport, 3. Network, 4. Data-link, 5. Physical</p>
</li>
</ol>
<p><strong>Two principles for all protocols:</strong></p>
<ol>
<li><p>First principle: Each layer needs to work on two opposite tasks.</p>
</li>
<li><p>Second principle: Each layer must be identical between the source and destination</p>
</li>
</ol>
<p><strong>Addressing each layer of TCP/IP:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Packet</td><td>Layers</td><td>Address</td></tr>
</thead>
<tbody>
<tr>
<td>Message</td><td>Application</td><td>Name</td></tr>
<tr>
<td>Segment/ User Datagram</td><td>Transport</td><td>Port Number</td></tr>
<tr>
<td>Datagram</td><td>Network</td><td>Logical Address</td></tr>
<tr>
<td>Frame</td><td>Data-link</td><td>Data-link Address</td></tr>
<tr>
<td>Bits</td><td>Physical</td><td>-</td></tr>
</tbody>
</table>
</div><p><strong>Lacks of OSI Model Success:</strong></p>
<ol>
<li><p>To replace the infrastructure from TCP/IP to OSI model, it needs a lot of time and money,</p>
</li>
<li><p>Services are not fully described in the presentation and session layer of the OSI model and</p>
</li>
<li><p>Performances are not high enough to attract ISOC to switch TCP/IP to the OSI model.</p>
</li>
</ol>
<p><strong>There are 4 signals:</strong></p>
<ol>
<li><p>Periodic Analog Signal,</p>
</li>
<li><p>Non-periodic Analog Signal,</p>
</li>
<li><p>Periodic Digital Signal</p>
</li>
<li><p>Non-periodic Digital Signal</p>
</li>
</ol>
<p><strong>Signal Basics:</strong></p>
<p>Bandwidth: The bandwidth of a composite signal is the difference between the highest and the lowest frequencies contained in that signal. <strong>Unit: Hz</strong>.</p>
<p>$$B = F_h - F_l$$</p><p>Bit Rate: The number of bits transferred per second is called bitrate. Unit: <strong>bps</strong></p>
<p>$$number \space of \space bits = log_2 (bit \space level)$$</p><p><strong>Bit length = propagation speed * bit duration</strong></p>
<p>The non-periodic digital signal can be converted to as non-periodic analog signal.</p>
<p><strong>Transmission of Digital Signal</strong></p>
<ol>
<li><p>Baseband:</p>
<p> Baseband transmission means transferring <strong><em>digital signal to digital signal</em></strong> using a <strong><em>low-pass</em></strong> channel.</p>
<p> <img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F155d0b75-e884-40e6-a538-1ccf97929d38%2FUntitled.png?table=block&amp;id=17cd6084-69c8-41d8-be57-48a22c4dbe4d&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p> Case 1. Low Pass Channel with Wide Bandwidth</p>
<p> Case 2. Low Pass Channel with Limited Bandwidth,</p>
</li>
<li><p>Broadband:</p>
<p> Broadband transmission means converting <strong><em>digital signal to analog signal</em></strong> in the sending end using <strong><em>modulation,</em></strong> and in the receiving end, it converts <strong><em>analog signal to digital signal</em></strong> through the <strong><em>band-pass channel.</em></strong></p>
<p> <img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F7f7dce2a-0b70-4dd7-9a17-ffe4fd6754bd%2FUntitled.png?table=block&amp;id=28bb390f-c6b3-4ca2-b5c0-f67aa1163a55&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
</li>
</ol>
<p><strong>Transmission Impairment:</strong></p>
<ol>
<li><p>Attenuation (loss of energy),</p>
<p> loss of power (unit: dB)</p>
</li>
</ol>
<p>$$10log_{10}\frac{p_2}{p_1}$$</p><ol>
<li><p>Distortion (Changes in shape)</p>
</li>
<li><p>Noise</p>
<p> Type of Noises:</p>
<ol>
<li><p>thermal: the random motion of electrons in a wire, which creates an extra signal.</p>
</li>
<li><p>Induced: comes from sources such as motors and appliances.</p>
</li>
<li><p>Crosstalk: the effect of one wire on the other</p>
</li>
<li><p>Impulse: a spike (a signal with high energy in a very short time) that comes from power lines or lightning.</p>
</li>
</ol>
</li>
</ol>
<p><strong>Signal-to-Noise Ratio (SNR):</strong></p>
<p>to find the theoretical bit rate limit, we need to know the ratio of the signal power to the noise power. The signal-to-noise ratio is defined as,</p>
<p>$$SNR = \frac{average\space signal \space power}{average \space noise \space power}$$</p><p>A high SNR means the signal is less corrupted by noise; a low SNR means the signal is more corrupted by noise. Because SNR is the ratio of two powers, it is often described in decibel units,</p>
<p>$$SNR_{dB} = 10 log_{10}\frac{average\space signal \space power}{average \space noise \space power} = 10 log_{10}(SNR)$$</p><p><strong>Data Rate Limits:</strong></p>
<p>A very important consideration in data communications is how fast we can send data, in bits per second, over a channel. The data rate depends on three factors:</p>
<ol>
<li><p>The bandwidth available</p>
</li>
<li><p>The level of the signals we use</p>
</li>
<li><p>The quality of the channel (the level of noise)</p>
</li>
</ol>
<p>Two theoretical formulas were developed to calculate the data rate: one by Nyquist for a noiseless channel, and another by Shannon for a noisy channel.</p>
<p>Noiseless Channel: Nyquist Bit Rate</p>
<p>$$bitrate = 2 \times bandwidth \times bit \space length = 2 \times bandwidth \times log_2(Level)$$</p><p>Noisy Channel: Shannon Capacity</p>
<p>$$Capacity = bandwidth \times log_2(1 + SNR)$$</p><p>capacity is the capacity of the channel in bits per second</p>
<p><strong>Note: The Shannon capacity gives us the upper limit; the Nyquist formula tells us how many signal levels we need.</strong></p>
<p>Equations:</p>
<ol>
<li><p><strong>Transmission time = (Message size) / Bandwidth</strong></p>
</li>
<li><p><strong>The bandwidth-delay product defines the number of bits that can fill the link</strong></p>
</li>
</ol>
<h3 id="heading-digital-signal-conversion"><strong>Digital Signal Conversion</strong></h3>
<p>Characteristics of Digital Signal Conversion:</p>
<ol>
<li><p><strong>Baseline wandering</strong> refers to the slow, low-frequency variations or shifts in the baseline or the zero-level reference of a signal.</p>
</li>
<li><p>When the voltage level in a digital signal is constant for a while, the spectrum creates very low frequencies which creates <strong>DC Components</strong> problem.</p>
</li>
<li><p><strong>Self-synchronization</strong> refers to the sync between the receiver’s bits interval and the sender’s bits interval.</p>
</li>
<li><p><strong>Built-in error-detecting</strong> capability in the generated code to detect some or all of the errors that occurred during transmission.</p>
</li>
<li><p><strong>Immunity to Noise and Interference</strong></p>
</li>
<li><p><strong>Complexity:</strong> A more complex Scheme is costly.</p>
</li>
</ol>
<p><strong>Digital to Digital Data Encoding:</strong></p>
<p>$$r = \frac {data\space element}{signal \space element} = \frac{data \space rate}{ signal \space rate} = \frac{N}{S}$$</p><p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Fa70ea2fb-f261-49ed-9719-196debb6f02e%2FUntitled.png?table=block&amp;id=ea208083-5e25-432e-9b47-92c209d85a77&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p>Three types of encoding schemes:</p>
<ol>
<li><p>Line encoding:</p>
<ol>
<li><p>Unipolar: (0, + v / - v)</p>
<ol>
<li><p>NRZ:</p>
<p> bit 1 = + v bit 0 = 0</p>
</li>
</ol>
</li>
<li><p>Polar: (+ v, - v )</p>
<ol>
<li><p>NRZ-L:</p>
<p> bit 1 = - v</p>
<p> bit 0 = + v</p>
</li>
<li><p>NRZ-I:</p>
<p> bit 1 = change</p>
<p> bit 0 = no change</p>
</li>
<li><p>Biphase:</p>
<ol>
<li><p>Manchester:</p>
<p> bit 1 = - v to + v</p>
<p> bit 0 = + v to - v</p>
</li>
<li><p>Differential Manchester:</p>
<p> bit 1 = no transition = no inversion</p>
<p> bit 0 = transition = inversion</p>
</li>
</ol>
</li>
</ol>
</li>
<li><p>Bipolar:</p>
<ol>
<li><p>AMI:</p>
<p> bit 1 = transition</p>
<p> bit 0 = 0</p>
</li>
<li><p>Pseudo-ternary:</p>
<p> bit 1 = 0</p>
<p> bit 0 = transition</p>
</li>
</ol>
</li>
<li><p>Multilevel:</p>
<ol>
<li><p>2B1Q:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694360104120/8de6e9ac-3bc5-4459-a3a1-dde147ed410f.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
</li>
<li><p>Multitransition:</p>
<ol>
<li><p>MLT-3</p>
<p> bit 1 = opposite of last non-zero if the current level is zero 0 if the current level is non-zero</p>
<p> bit 0 = no transition</p>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<p>    <strong>Bandwidth Table:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Line encoding name</td><td>Bandwidth</td></tr>
</thead>
<tbody>
<tr>
<td>Unipolar NRZ, Polar NRZ, Polar RZ, AMI, Pseudo-ternary</td><td>N/2</td></tr>
<tr>
<td>Polar Biphase (Manchester, Diffential Manchester)</td><td>N</td></tr>
<tr>
<td>2B1Q</td><td>N/4</td></tr>
<tr>
<td>8B6Q</td><td>3 N/4</td></tr>
<tr>
<td>4D-PAM5</td><td>N/8</td></tr>
<tr>
<td>MLT-3</td><td>N/3</td></tr>
</tbody>
</table>
</div><ol>
<li><p>Block encoding: mBnB</p>
<ol>
<li>4B/5B, b. 8B/10B</li>
</ol>
</li>
<li><p>Scrambling:</p>
<ol>
<li><p>B8ZS: (Bipolar with 8 zeros Substuition)</p>
<p> 0000 0000 = 000VB0VB V = last non-zero level</p>
<p> B = opposite of the last non-zero level</p>
</li>
<li><p>HDB3: (High Density Bi-polar 3 Zeros)</p>
<p> 0000 = 000V (odd numbers of last non-zeros)</p>
<p> 0000 = B00V (even numbers of last non-zeros)</p>
</li>
</ol>
</li>
</ol>
<p><strong>Analog to Digital Conversion:</strong></p>
<ol>
<li><p>PCM: (Pulse Code Modulation)</p>
<ol>
<li><p>Sampling by Nyquist theorem</p>
</li>
<li><p>Quantizing : Delta = (V_max - V_min) / L</p>
</li>
<li><p>Encoding</p>
</li>
</ol>
</li>
<li><p>DM: (Delta Modulation)</p>
<ol>
<li><p>Modulator,</p>
</li>
<li><p>Demodulator,</p>
</li>
<li><p>Adaptive DM</p>
</li>
<li><p>Quantizing Error</p>
</li>
</ol>
</li>
</ol>
<p><strong>Digital</strong> <strong>Transmission Mode:</strong></p>
<ol>
<li><p>Serial:</p>
<ol>
<li><p>Asynchronous, (stop bit, start bit)</p>
</li>
<li><p>Synchronous (sender and receiver’s clock time = same)</p>
</li>
<li><p>Isochronous (video and audio real-time data)</p>
</li>
</ol>
</li>
<li><p>Parallel</p>
</li>
</ol>
<h3 id="heading-analog-signal-transmission">Analog Signal Transmission</h3>
<p>Digital to Analog: (Digital Signal + Carrier Signal = Modulated Signal)</p>
<ol>
<li><p>ASK</p>
<p> 0 = 0 amplitude</p>
<p> level = 2 (BASK)</p>
<p> r = Log( Level)</p>
<p> B = (1 + d) <em>S = (1 +d )</em> N/ r</p>
</li>
<li><p>PSK</p>
<p> 0 = 180 phase</p>
<p> level = 2 (BPSK)</p>
<p> r = Log( Level)</p>
<p> B = (1 + d) <em>S = (1 +d )</em> N/ r</p>
</li>
<li><p>FSK</p>
<p> 1 = Frequency Increased</p>
<p> B = ( 1 + d) * S + 2F</p>
</li>
<li><p>QAM (ASK + PSK)</p>
<p> Use constellation diagram</p>
<p> example: 4 QAM, 16 QAM, 64 QAM</p>
</li>
</ol>
<p>Analog to Analog: (Modulating Analog Signal + Carrier Signal = Modulated Signal)</p>
<ol>
<li><p>AM</p>
</li>
<li><p>FM</p>
</li>
<li><p>PM</p>
</li>
</ol>
<h3 id="heading-bandwidth-utilization"><strong>Bandwidth Utilization</strong></h3>
<p>There are 3 multiplexing techniques to utilize the bandwidth:</p>
<ol>
<li><p>FDM (Analog Signal)</p>
<p> Frequency Division Multiplexing: Create a composite signal of broad frequencies from input signals. Guard bands are strips of unused bandwidth. In Frequency-division multiplexing (FDM), Channels can be separated by guard bands to prevent signals from overlapping.</p>
</li>
<li><p>WDM (Optical Signal)</p>
<p> Wavelength Division Multiplexing: Create a multiple wavelength signal from input-specific wavelength signals. We use prism for WDM.</p>
</li>
<li><p>TDM (Digital Signal)</p>
<ol>
<li><p>Synchronous:</p>
<ol>
<li><p>The frame is a combination of time slots. In a frame, number of time slots is the number of input lines.</p>
</li>
<li><p>Frame rate = input data rate</p>
</li>
<li><p>Frame duration = 1 / Frame rate</p>
</li>
<li><p>bitrate = number of input lines * frame rate</p>
</li>
<li><p>To fill empty slots:</p>
<ol>
<li><p>Multilevel Multiplexing (100 + 100 → 200)</p>
</li>
<li><p>Multi-slot multiplexing (200 → 100 + 100)</p>
</li>
<li><p>Pulse stuffing ( 190 + 10 → 200)</p>
</li>
</ol>
</li>
<li><p>It uses framing bit as 0 or 1 for synchronization</p>
</li>
<li><p>Interleaving: TDM can be visualized as two fast-rotating switches, one on the multiplexing side and the other on the demultiplexing side. On the multiplexing side, as the switch opens in front of a connection, that connection has the opportunity to send a unit onto the path. This process is called interleaving</p>
</li>
</ol>
</li>
<li><p>Statistical:</p>
<ol>
<li><p>It uses no-synchronization</p>
</li>
<li><p>It only takes slots that are filled and inserts an address beside the time slot to identify which slot it is referred to.</p>
</li>
<li><p>In peak time, data lines need to wait for overhead bits.</p>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<p><strong>Spectrum Spreading for Preventing Interference and Spying:</strong></p>
<ol>
<li><p>FHSS (Frequency Hopping Spread Spectrum)</p>
<p> It uses a hopping sequence to hop frequencies for transferring data in runtime.</p>
<p> Hopping sequence: 100, 400, 300, 200</p>
<p> Data: 100, 010, 011, 101</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694360061669/244f175b-92c5-4ca5-8bea-305230f58824.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>DSSS (Direct Sequence Spread Spectrum)</p>
<p> The signal encoding scheme is Polar NRZ-L. ( bit 1 = -v, Bit 0 = +v)</p>
<p> Sending data: 101</p>
<p> Spreading code: 1011</p>
<p> Spread code: 1011 0100 1011</p>
</li>
</ol>
<h3 id="heading-transmission-media">Transmission Media</h3>
<ol>
<li><p>Guided Media</p>
<ol>
<li><p>Twisted pair:</p>
<ol>
<li><p>Ground and Signal</p>
</li>
<li><p>Plastic Cover + Core = UTP, Plastic Cover + Metal + Core = STP</p>
</li>
<li><p>Connected by RJ45</p>
</li>
</ol>
</li>
<li><p>Co-axial:</p>
<ol>
<li><p>Outer Layer + Insulator + Outer Conductor + Insulator + Inner Conductor</p>
</li>
<li><p>Connected by BNC</p>
</li>
</ol>
</li>
<li><p>Optical Fibre:</p>
<ol>
<li><p>Outer Layer + Dupont Kevlar + Plastic Buffer + Cladding + Core</p>
</li>
<li><p>ST or SC Connector</p>
</li>
<li><p>Mode:</p>
<p> <img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2Ff41085ff-7992-4315-bda0-b18a0ec195f1%2FUntitled.png?table=block&amp;id=d294867e-e3c5-4e6e-ba28-12b65afe146e&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<ol>
<li><p>Multimode:</p>
<ol>
<li><p>Step index:</p>
<ol>
<li><p>The density of the core is constant</p>
</li>
<li><p>Cladding and core are made of the same material</p>
</li>
</ol>
</li>
<li><p>Graded index:</p>
<ol>
<li><p>The density of the center of the core is higher and decreases gradually to its lowest at the edge.</p>
</li>
<li><p>Cladding and core are not made of the same material</p>
</li>
</ol>
</li>
</ol>
</li>
<li><p>Single mode:</p>
<ol>
<li><p>The critical angle is almost up to 90</p>
</li>
<li><p>The propagation of the beam is almost horizontal</p>
</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<p>    Unguided Media:</p>
<ol>
<li><p>Radiowave: (3 KHz - 1 GHz)</p>
</li>
<li><p>Microwave: (1 GHz - 300 GHz)</p>
</li>
<li><p>Infrared: (300 GHz - 400 THz)</p>
</li>
</ol>
<p>    Propagation of Radiowave:</p>
<ol>
<li><p>Ground propagation:</p>
<ol>
<li><p>It used a very low-frequency band</p>
</li>
<li><p>propagates below the lower point of the atmosphere</p>
</li>
<li><p>distance depends on the power of the signal</p>
</li>
</ol>
</li>
<li><p>Sky propagation:</p>
<ol>
<li><p>Works by reflecting in the ionosphere</p>
</li>
<li><p>Greater distance traveled by lower output data</p>
</li>
<li><p>Band: High, very high-frequency band</p>
</li>
</ol>
</li>
<li><p>Line of sight:</p>
<ol>
<li><p>Antenna-to-antenna data transfer</p>
</li>
<li><p>Band: UHF, EHF</p>
</li>
<li><p>Used in satellite</p>
</li>
</ol>
</li>
</ol>
<p>    Antenna:</p>
<ol>
<li><p>Omnidirectional Antenna:</p>
<ol>
<li><p>One sender to many receivers</p>
</li>
<li><p>Used by radio wave</p>
</li>
</ol>
</li>
<li><p>Unidirectional / Parabolic Antenna:</p>
<ol>
<li><p>Send only to one direction</p>
</li>
<li><p>Used by microwave signal</p>
</li>
</ol>
</li>
</ol>
<p>    Infrared:</p>
<ol>
<li><p>Line of sight propagation</p>
</li>
<li><p>No Antenna needed</p>
</li>
<li><p>Used for shorter distance</p>
</li>
</ol>
<h3 id="heading-introduction-to-data-link-layer">Introduction to Data-link Layer</h3>
<p>    Packet: frame, Address: link-layer address</p>
<p>    Connection between: node</p>
<p>    Nodes are connected with links. Links are 2 types:</p>
<ol>
<li><p>point to point link (DLC sublayer)</p>
</li>
<li><p>broadcast link (MAC and DLC sublayers)</p>
</li>
</ol>
<p>    Services of Data-link layer:</p>
<ol>
<li><p>Framing</p>
</li>
<li><p>Flow Control (Produced Frames &gt; Consumed Frames)</p>
</li>
<li><p>Error Control</p>
</li>
<li><p>Congestion Control</p>
</li>
</ol>
<p>    Link layer Addressing: (48 bits / 6 bytes / 12 hexadecimal digits)</p>
<ol>
<li><p>Unicast:</p>
<p> Address: 23:53:29:61:07:F1</p>
</li>
<li><p>Multicast:</p>
<p> Address: 24:54:28:62:08:F2</p>
</li>
<li><p>Broadcast:</p>
<p> Address: FF:FF:FF:FF:FF:FF</p>
</li>
</ol>
<p>    ARP (Address Resolution Protocol):</p>
<p>    ARP accepts an IP Address from Internet Protocol and maps the address to the link-layer address in the data-link layer.</p>
<p>    ARP Packet format:</p>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694359121436/50888257-290e-412e-85b4-c0aeb4be334e.png" alt class="image--center mx-auto" /></p>
<p>    ARP Caching:</p>
<p>    Broadcast Link → Store Destination’s link-layer address → Unicast Link</p>
<h3 id="heading-types-of-error">Types of error</h3>
<p>    1. single error, 2. burst error</p>
<p>    Burst length = length (position of first error, position, last error)</p>
<p>    There are 2 types of block coding:</p>
<p>    1. Dataword, 2. Codeword</p>
<p>    The hamming distance can be solved by XORing a two-bit string</p>
<p>    Minimum Hamming distance = s + 1 where s = number of errors that can be solved.</p>
<p>    <strong>CRC and Polynomials:</strong></p>
<p>    <img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F0a31210b-807b-41c0-806b-058eb96a1f09%2FScreenshot_2023-09-10_at_12.25.44_PM.png?table=block&amp;id=b809cb0c-b803-4c8b-8b43-56f5ca79cc07&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p>    <strong>Checksum</strong>:</p>
<p>    <img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F3fe48459-2a50-4a95-ab27-4b8625371b94%2FUntitled.png?table=block&amp;id=a9f8c9ff-7fa8-4275-a928-1bf92fafcac3&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p>    Forward Error Correction Methods:</p>
<ol>
<li><p>Hamming Distance</p>
</li>
<li><p>XOR</p>
</li>
<li><p>Chunk interleaving</p>
</li>
<li><p>Chunk interleaving with Hamming Distance</p>
</li>
<li><p>Compounding high and low-resolution packet</p>
</li>
</ol>
<p><strong>Note: CRC is better than Checksum in correcting errors.</strong></p>
<h3 id="heading-dlc-data-link-control-protocols">DLC (Data link Control) Protocols</h3>
<p>Framing: 1. Bit oriented, 2. Character Oriented (byte-oriented)</p>
<p>Byte stuffing: When data has a flag-like byte, we add an ESC byte before it so that the receiver doesn’t end the frame while reading user information. It is used in Character oriented framing</p>
<p>Bit stuffing: The flag is 01111110; while reading user information, it replaces 01111110 with 011111010</p>
<p>Buffer: A buffer is a set of memory locations that can hold packets at the sender and receiver. When the buffer of the receiving data-link layer is full, it informs the sending data-link layer to stop pushing frames. It is used in flow control.</p>
<p>Error Control: This service is provided by a 2-4 byte CRC added to the frame header.</p>
<p>Services: 1. Framing, 2. Flow control, 3. Error Control</p>
<p>DLC protocols can be:</p>
<ol>
<li><p>Connectionless oriented: Frames are unnumbered</p>
</li>
<li><p>Connection-oriented: Frames are unnumbered</p>
</li>
</ol>
<p>Simple protocol:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F82cb0d89-c839-457e-b201-842c38858049%2FUntitled.png?table=block&amp;id=c89086b0-99b0-45e0-8476-00513eedeef3&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p>Stop and wait Protocol:</p>
<p>Flow control is provided by sequence number (0,1,0,1,..) and acknowledge number (1,0,1,0,..)</p>
<p>Two states:</p>
<ol>
<li><p>Ready State:</p>
<ol>
<li><p>Waiting for the packet from the network layer</p>
</li>
<li><p>The sender creates a frame and sends it.</p>
</li>
<li><p>The sender saves a copy of the frame</p>
</li>
</ol>
</li>
</ol>
<p>Blocking state:</p>
<ol>
<li><p>If a timeout occurs, the sender resends the frame</p>
</li>
<li><p>If ACK is corrupted, the frame is discarded</p>
</li>
<li><p>else receiver sends error-free ACK to receive the next frame</p>
</li>
</ol>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F4b06dfb8-acd7-41b4-919a-9de5973dd7c7%2FUntitled.png?table=block&amp;id=ad072c09-1ea3-4331-9d95-d38b3a1af3d1&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<p>Piggybacking:</p>
<p>Bi-directional protocol that is sophisticated and costly. The sender and receiver both can send signals and acknowledgment.</p>
<p>HDLC (High-Level Data Link Control Protocol):</p>
<p>Characteristics: 1. Bit oriented, 2. More theoretical than practical, 3. Use Stop and Wait Protocol, 4. Use point-to-point or multi-point links.</p>
<p>Transfer modes:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F6a3332d8-f4f2-4927-a939-6bfb43f90300%2FUntitled.png?table=block&amp;id=628dc5e1-f962-41b9-83bc-ac8db244439a&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<ol>
<li><p>NRM (Normal Response Mode): (Point to point / Multi-point, Unbalanced, 1 Primary Station → Multi Secondary Stations)</p>
</li>
<li><p>ABM (Asynchronous Balanced Mode) (Point to point, Balance)</p>
</li>
</ol>
<p><strong>Framing of HDLC</strong></p>
<p>Information Frame: (Used for transferring data)</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Flag</td><td>Address</td><td>Control</td><td>User Information</td><td>FCS</td><td>Flag</td></tr>
</thead>
<tbody>
<tr>
<td></td></tr>
</tbody>
</table>
</div><p>Supervisory Frame: (Used for Flow and Error Control)</p>
<p>ACKs: RR (Receive Ready), RNR (Receive Not Ready), REJ (Reject), SREJ (Selective Reject)</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Flag</td><td>Address</td><td>Control</td><td>FCS</td><td>Flag</td></tr>
</thead>
<tbody>
<tr>
<td></td></tr>
</tbody>
</table>
</div><p>Unnumbered Frame: (Used for connection establishment and release)</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Flag</td><td>Address</td><td>Control</td><td>Management Information</td><td>FCS</td><td>Flag</td></tr>
</thead>
<tbody>
<tr>
<td></td></tr>
</tbody>
</table>
</div><p><strong>PPP (Point-to-Point Protocol)</strong></p>
<p>Services: 1. Most used protocol, 2. Provide Authentication and network configuration, 3. Error Control.</p>
<p>PPP doesn’t provide flow control.</p>
<p>PPP Framing:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Flag</td><td>Address</td><td>Control</td><td>Protocol</td><td>Payload</td><td>FCS</td><td>Flag</td></tr>
</thead>
<tbody>
<tr>
<td>01111110</td><td>11111111</td><td>00000011</td><td>1-2 bytes</td><td>Variable-sized</td><td>2-4 byte CRC</td><td>01111110</td></tr>
</tbody>
</table>
</div><p>PPP has created two protocols for authentication: <strong>PAP</strong> (Password Authentication Protocol) and <strong>CHAP</strong> (Challenge Handshake Authentication Protocol). Internet Protocol Control Protocol (<strong>IPCP</strong>) is framed inside PPP framing for carrying IP data packets.</p>
<p>Transition phase of PPP:</p>
<p><img src="https://anwholesquare.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb6067351-1921-4756-90ef-19d0061185e8%2F0b034b54-0b19-45ea-a836-d3f25b24a8cc%2FUntitled.png?table=block&amp;id=fd0deec0-8008-42b6-86ea-c9420fb995e4&amp;spaceId=b6067351-1921-4756-90ef-19d0061185e8&amp;width=2000&amp;userId=&amp;cache=v2" alt /></p>
<h3 id="heading-mac-protocols-media-access-control">MAC Protocols (Media Access Control)</h3>
<ol>
<li><p>Random Access Protocol:</p>
<ol>
<li><p>ALOHA (Advocates of Linux Open-source Hawaii Association): Radio LAN</p>
</li>
<li><p>CSMA ( Carrier Sense Multiple Access): Ethernet</p>
</li>
<li><p>CSMA / CD (Carrier Sense Multiple Access with Collision Detection): Traditional Ethernet of 10 Mbps data rate</p>
</li>
<li><p>CSMA / CA (Carrier Sense Multiple Access with Collision Avoidance): Wireless Network</p>
</li>
</ol>
</li>
<li><p>Control Access Protocol:</p>
<ol>
<li><p>Reservation: Device needs reservation before sending data</p>
</li>
<li><p>Polling: One device is the primary station, and others are the secondary station</p>
</li>
<li><p>Token Passing: Networks are organized in a logical ring, and all stations are either predecessors or successors.</p>
</li>
</ol>
</li>
<li><p>Channelization:</p>
<ol>
<li><p>FDMA (Frequency Division Multiple Access): The bandwidth of the common channel is divided into bands that are separated by guard bands.</p>
</li>
<li><p>TDMA (Time Division Multiple Access): Stations share timeslots.</p>
</li>
<li><p>CDMA (Code Division Multiple Access): All data are sent by one channel with different codes.</p>
</li>
</ol>
</li>
</ol>
<p>Abbreviations:</p>
<ol>
<li><p>ARPANET: Advanced Research Projects Agency Network</p>
</li>
<li><p>TCP: Transmission Control Protocol</p>
</li>
<li><p>IP: Internet Protocol</p>
</li>
<li><p>ISOC: Internet Society</p>
</li>
<li><p>IAB: Internet Architecture Board</p>
</li>
<li><p>ASCII: American Standard Code for Information Interchange</p>
</li>
<li><p>ISO: Internation Organization for Standardization</p>
</li>
<li><p>OSI: Open Systems Interconnection</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Popular algorithm and passings for single source shortest path]]></title><description><![CDATA[Three commonly used methods for determining the shortest path from a single starting node require a fundamental understanding of graphs. These algorithms are as follows:

Shortest path in a Directed Acyclic Graph (DAG) using topological sorting

The ...]]></description><link>https://blog.khandokeranan.com/single-source-shortest-path</link><guid isPermaLink="true">https://blog.khandokeranan.com/single-source-shortest-path</guid><category><![CDATA[dijkstra]]></category><category><![CDATA[ShortestPath]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[C++]]></category><category><![CDATA[single source shortest path]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Fri, 01 Sep 2023 23:04:02 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1693609373392/22d2ba1d-0db4-4d05-80b1-6966b238fc6c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Three commonly used methods for determining the shortest path from a single starting node require a fundamental understanding of graphs. These algorithms are as follows:</p>
<ol>
<li><p>Shortest path in a Directed Acyclic Graph (DAG) using topological sorting</p>
</li>
<li><p>The Dijkstra algorithm</p>
</li>
<li><p>The Bellman-Ford algorithm</p>
</li>
</ol>
<h3 id="heading-relax-function-edge-relaxation-important"><strong>Relax Function (Edge Relaxation): (IMPORTANT)</strong></h3>
<p>In graph algorithms, particularly shortest path algorithms like Dijkstra's and the Bellman-Ford algorithm, the "Relaxation" process, also known as "Node Relaxation," is a crucial step that helps in finding the shortest paths efficiently.</p>
<p>Relaxation involves optimizing the current known distance to a node by potentially replacing it with a shorter distance. The process compares the distance of a potential path through a neighboring node to the currently known shortest path to the destination node. If the potential path is indeed shorter, the distance of the destination node is updated, and this action is referred to as "relaxing" the edge or the node.</p>
<p>Consider a graph where you are trying to find the shortest distance from a source node to other nodes. During relaxation, if you find a path that is shorter than the currently known path, you update the distance of the destination node with this shorter distance. This process iterates through all the nodes in the graph, ensuring that the distances are accurately updated based on the shortest paths discovered so far.<br />Algorithm:</p>
<pre><code class="lang-cpp">relax(i,j,distance[], weight[][]):
<span class="hljs-keyword">if</span> distance[i] + weight[i][j] &lt; distance[j]:
   distance [j] = distance[i] + weight[i][j]
   predecessor [j] = i
end <span class="hljs-keyword">if</span>
</code></pre>
<h3 id="heading-shortest-path-in-a-dag-using-topological-sorting">Shortest path in a DAG using topological sorting</h3>
<p>When dealing with a Directed Acyclic Graph (DAG), a graph without any cycles, we can find the shortest path from a single source node to all other nodes using a combination of topological sorting and edge relaxation. The key insight is that we process nodes in topological order, calculating the shortest distance to each node based on the distances to its incoming neighbors.</p>
<pre><code class="lang-cpp"><span class="hljs-number">0.</span> single_source_shortest_path_dag (source, topolist, weight, adj):
<span class="hljs-number">1.</span> <span class="hljs-keyword">for</span> each vertex i in topolist:
<span class="hljs-number">2.</span>     distance [i] = INFINITY
<span class="hljs-number">3.</span>     predecessor [i] = <span class="hljs-literal">NULL</span>
<span class="hljs-number">4.</span> end <span class="hljs-keyword">for</span>
<span class="hljs-number">5.</span> distance[source] = <span class="hljs-number">0</span>
<span class="hljs-number">6.</span> <span class="hljs-keyword">for</span> each vertex i in topolist:
<span class="hljs-number">7.</span>     <span class="hljs-keyword">for</span> each adjacent vertex j in adj[i]:
<span class="hljs-number">8.</span>         relax(i,j,distance,weight)
<span class="hljs-number">9.</span>     end <span class="hljs-keyword">for</span>
<span class="hljs-number">10.</span> end <span class="hljs-keyword">for</span>
<span class="hljs-comment">// Now using the distance and predecessor </span>
<span class="hljs-comment">// we can measure distance from the single source </span>
<span class="hljs-comment">// and track the path from source to destination</span>
</code></pre>
<p>To make a graph topologically sorted, we have used below algorithm:</p>
<pre><code class="lang-cpp"><span class="hljs-number">0.</span> toposort(u):
<span class="hljs-number">1.</span> visited[u] = <span class="hljs-literal">true</span>
<span class="hljs-number">2.</span> <span class="hljs-keyword">for</span> all the adjacency vertex i of adj[u]:
<span class="hljs-number">3.</span>     <span class="hljs-keyword">if</span> visited [i] is <span class="hljs-keyword">not</span> <span class="hljs-literal">true</span>
<span class="hljs-number">4.</span>         toposort(i)
<span class="hljs-number">5.</span>     end <span class="hljs-keyword">if</span>
<span class="hljs-number">6.</span> end <span class="hljs-keyword">for</span>
<span class="hljs-number">5.</span> insert u in a linkedlist <span class="hljs-string">'topolist'</span> from front
</code></pre>
<p>Example:<br />The source node is <strong><em>r</em></strong> and explores all shortest paths from the source node. Show the tabular passing.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693433816600/99c170a3-20ab-441a-86ad-740e9e3312a3.png" alt class="image--center mx-auto" /></p>
<p>Answer: Tabular passing is given below in the chart,<br />where distance/pi = cost to reach the node from source node/predecessor's node</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>nodes</td><td>r</td><td>s</td><td>t</td><td>x</td><td>y</td><td>z</td></tr>
</thead>
<tbody>
<tr>
<td>initial</td><td>0/r</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td></tr>
<tr>
<td>r</td><td>0/r</td><td>5/r</td><td>3/r</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td></tr>
<tr>
<td>s</td><td>0/r</td><td>5/r</td><td>3/r</td><td>11/s</td><td>INF/NIL</td><td>INF/NIL</td></tr>
<tr>
<td>t</td><td>0/r</td><td>5/r</td><td>3/r</td><td>10/t</td><td>7/t</td><td>5/t</td></tr>
<tr>
<td>x</td><td>0/r</td><td>5/r</td><td>3/r</td><td>10/t</td><td>7/t</td><td>5/t</td></tr>
<tr>
<td>y</td><td>0/r</td><td>5/r</td><td>3/r</td><td>10/t</td><td>7/t</td><td>5/t</td></tr>
<tr>
<td>z</td><td>0/r</td><td>5/r</td><td>3/r</td><td>10/t</td><td>7/t</td><td>5/t</td></tr>
</tbody>
</table>
</div><p>Now, we need to find paths from the source node to other nodes:</p>
<p><strong><em>r to s:</em></strong> 5 (r -&gt; s)<br /><strong><em>r to t:</em></strong> 3 (r -&gt; t)<br /><strong><em>r to x:</em></strong> 10 (r -&gt; t -&gt; x)<br /><strong><em>r to y:</em></strong> 7 (r -&gt; t -&gt; y)<br /><strong><em>r to z:</em></strong> 5 (r -&gt; t -&gt; z)</p>
<h3 id="heading-dijkstra-algorithm">Dijkstra Algorithm</h3>
<p>Dijkstra is useful for cyclic non-negative edge graphs where we must find all the shortest paths from a single source node. The algorithm maintains a set of explored nodes and continually updates the shortest distance from the source node to each node in the graph.</p>
<p>Here's the step-by-step description of Dijkstra's algorithm:</p>
<p><strong>Input:</strong></p>
<ul>
<li><p><code>graph</code>: The graph is represented as an adjacency list or matrix.</p>
</li>
<li><p><code>source</code>: The starting node from which to find the shortest paths.</p>
</li>
</ul>
<p><strong>Output:</strong></p>
<ul>
<li><p>An array <code>distances</code> where <code>distances[i]</code> represents the shortest distance from the source node to the node <code>i</code>.</p>
</li>
<li><p>An array <code>prev</code> where <code>prev[i]</code> represents the previous node on the shortest path from the source node to the node <code>i</code>.</p>
</li>
<li><p>Note: To reconstruct the shortest paths themselves, you can use the <code>prev</code> array.</p>
</li>
</ul>
<p><strong>Algorithm:</strong></p>
<ol>
<li><p>Initialize an array <code>distances</code> with the shortest distance from the source node to each node. Set <code>distances[source]</code> to 0 and all other distances to infinity.</p>
</li>
<li><p>Initialize a priority queue or a min-heap <code>pq</code> to store nodes and their tentative distances. Add the source node with a distance of 0 to <code>pq</code>.</p>
</li>
<li><p>Initialize an array <code>prev</code> to store the previous node on the shortest path for each node.</p>
</li>
<li><p>While <code>pq</code> is not empty: a. Extract the node <code>u</code> with the smallest distance from <code>pq</code>. b. For each neighbor <code>v</code> of <code>u</code>:</p>
<ul>
<li><p>Calculate a tentative distance <code>new_distance</code> from the source to <code>v</code> through <code>u</code>. It is the sum of the distance from the source to <code>u</code> and the weight of the edge from <code>u</code> to <code>v</code>.</p>
</li>
<li><p>If <code>new_distance</code> is less than the current distance stored in <code>distances[v]</code>, update <code>distances[v]</code> with <code>new_distance</code> and set <code>prev[v]</code> to <code>u</code>.</p>
</li>
<li><p>Add node <code>v</code> to <code>pq</code> with its updated distance.</p>
</li>
</ul>
</li>
<li><p>Once the algorithm finishes, the <code>distances</code> array will contain the shortest distances from the source node to all other nodes and the <code>prev</code> array will allow you to reconstruct the shortest paths.</p>
</li>
</ol>
<pre><code class="lang-cpp">dijkstra(source, adj):
    distances.assign(N, INF)
    prev.assign(N, <span class="hljs-number">-1</span>)
    distances[source] = <span class="hljs-number">0</span>
    prev[source] = source
    pq.push({distances[source], source})
    <span class="hljs-keyword">while</span> pq is <span class="hljs-keyword">not</span> empty:
        u = pq.top().first
        u.pop()
        <span class="hljs-keyword">for</span> all neighbour node v of adj[u]:
            <span class="hljs-keyword">if</span> (distances[u] + weight[u][v] &lt; distances[v]):
                distances[v] = distances[u] + weight[u][v]
                prev[v] = u
                pq.push({distances[v], v})
            end <span class="hljs-keyword">if</span>
        end <span class="hljs-keyword">for</span>
    end <span class="hljs-keyword">while</span>
</code></pre>
<p>Example:<br />The source node is <strong><em>s</em></strong> and explores all shortest paths from the source node. Show the tabular passing.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693605651502/5b03d96c-62bb-4996-8b6e-9765f7474a6d.png" alt class="image--center mx-auto" /></p>
<p>Answer: Tabular passing is given below in the chart,<br />where distance/pi = cost to reach the node from source node/predecessor's node</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Nodes</td><td>s</td><td>a</td><td>b</td><td>c</td><td>d</td></tr>
</thead>
<tbody>
<tr>
<td>initial</td><td>0/s</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td><td>INF/NIL</td></tr>
<tr>
<td>s</td><td>0/s</td><td>10/s</td><td>INF/NIL</td><td>5/s</td><td>INF/NIL</td></tr>
<tr>
<td>c</td><td>0/s</td><td>8/c</td><td>14/c</td><td>5/s</td><td>7/c</td></tr>
<tr>
<td>d</td><td>0/s</td><td>8/c</td><td>13/d</td><td>5/s</td><td>7/c</td></tr>
<tr>
<td>a</td><td>0/s</td><td>8/c</td><td>9/a</td><td>5/s</td><td>7/c</td></tr>
<tr>
<td>b</td><td>0/s</td><td>8/c</td><td>9/a</td><td>5/s</td><td>7/c</td></tr>
</tbody>
</table>
</div><p>Now, we need to find paths from the source node to other nodes:</p>
<p><strong><em>s to a:</em></strong> 8 (s -&gt; c -&gt; a)<br /><strong><em>s to b:</em></strong> 9 (s -&gt; c -&gt; a -&gt; b)<br /><strong><em>s to c:</em></strong> 5 ( s-&gt; c)<br /><strong><em>s to d:</em></strong> 7 (s -&gt; c -&gt; d)</p>
<h3 id="heading-bellman-ford-algorithm"><strong>Bellman-Ford Algorithm</strong></h3>
<p>The Bellman-Ford algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted, directed graph. It can handle graphs with negative weight edges and detect the presence of negative weight cycles. This algorithm is named after Richard Bellman and Lester Ford. This algorithm is one of the easiest to find a single source shortest path.</p>
<p>Here's an overview of how the Bellman-Ford algorithm works:</p>
<ol>
<li><p>Initialize an array to store the minimum distance from the source vertex to each vertex in the graph. Initially, set the distance to infinity for all vertices except the source, which is set to zero.</p>
</li>
<li><p>Iterate over all edges in the graph repeatedly for a number of times equal to the number of vertices minus one. In each iteration, update the minimum distance to each vertex by considering all possible paths through other vertices.</p>
</li>
</ol>
<p>$$Total\space number\space of \space iteration = Number \space of \space vertex-1$$</p><ol>
<li><p>After the iteration is complete, check for negative weight cycles. If any vertex's distance can still be improved in one more iteration, it means there is a negative weight cycle in the graph.</p>
</li>
<li><p>If no negative weight cycles are detected, the minimum distances from the source vertex to all other vertices have been computed.</p>
</li>
</ol>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Edge</span> {</span>
    <span class="hljs-keyword">int</span> source, destination, weight;
};

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">BellmanFord</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;Edge&gt;&amp; edges, <span class="hljs-keyword">int</span> V, <span class="hljs-keyword">int</span> source)</span> </span>{
    <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">distance</span><span class="hljs-params">(V, INT_MAX)</span></span>;
    distance[source] = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; V - <span class="hljs-number">1</span>; i++) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> Edge&amp; edge : edges) {
            <span class="hljs-keyword">int</span> u = edge.source;
            <span class="hljs-keyword">int</span> v = edge.destination;
            <span class="hljs-keyword">int</span> w = edge.weight;

            <span class="hljs-keyword">if</span> (distance[u] != INT_MAX &amp;&amp; distance[u] + w &lt; distance[v]) {
                distance[v] = distance[u] + w;
            }
        }
    }

    <span class="hljs-comment">// Check for negative weight cycles</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> Edge&amp; edge : edges) {
        <span class="hljs-keyword">int</span> u = edge.source;
        <span class="hljs-keyword">int</span> v = edge.destination;
        <span class="hljs-keyword">int</span> w = edge.weight;

        <span class="hljs-keyword">if</span> (distance[u] != INT_MAX &amp;&amp; distance[u] + w &lt; distance[v]) {
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Graph contains a negative weight cycle!"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
            <span class="hljs-keyword">return</span>;
        }
    }

    <span class="hljs-comment">// Print the shortest distances from the source vertex</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Shortest distances from source vertex "</span> &lt;&lt; source &lt;&lt; <span class="hljs-string">":"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; V; i++) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Vertex "</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">": "</span> &lt;&lt; distance[i] &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> V, E;
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Enter the number of vertices and edges: "</span>;
    <span class="hljs-built_in">cin</span> &gt;&gt; V &gt;&gt; E;

    <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;Edge&gt; <span class="hljs-title">edges</span><span class="hljs-params">(E)</span></span>;

    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Enter source, destination, and weight for each edge:"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; E; i++) {
        <span class="hljs-built_in">cin</span> &gt;&gt; edges[i].source &gt;&gt; edges[i].destination &gt;&gt; edges[i].weight;
    }

    <span class="hljs-keyword">int</span> source;
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Enter the source vertex: "</span>;
    <span class="hljs-built_in">cin</span> &gt;&gt; source;

    BellmanFord(edges, V, source);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Example:<br />The source node is <strong><em>s</em></strong> and explores all shortest paths from the source node. Show the manual passing. <strong>(DO IT BY YOURSELF)</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693756367568/184253d8-ee63-44f4-8b40-30544b3fc2aa.png" alt class="image--center mx-auto" /></p>
<p><strong>Answer:</strong> Passing is given below:<br /><strong>Initial distances:</strong><br />s = 0<br />a = Infinity<br />b = Infinity<br />c = Infinity<br />d = Infinity</p>
<p><strong>Edge list:</strong></p>
<p>1. (a, b) = 5<br />2. (a, c) = 8<br />3. (a, d) = -4<br />4. (b, a) = -2<br />5. (c, b) = -3<br />6. (c, d) = 9<br />7. (d, s) = 2<br />8. (d, b) = 7<br />9. (s, a) = 6<br />10. (s,c) = 7</p>
<p><strong>Iteration 1:</strong><br />Relax edges (s, a) and (s, c).<br />s = 0<br />a = 6<br />b = Infinity<br />c = 7<br />d = Infinity</p>
<p><strong>Iteration 2:</strong><br />Relax edges (a, b), (a, d), and (c, b).<br />s = 0<br />a = 6<br />b = 4<br />c = 7<br />d = 2</p>
<p><strong>Iteration 3:</strong><br />Relax edges (b, a).<br />s = 0<br />a = 2<br />b = 4<br />c = 7<br />d = 2</p>
<p><strong>Iteration 4:</strong><br />Relax edges (a, d).<br />s = 0<br />a = 2<br />b = 4<br />c = 7<br />d = -2</p>
<p><strong>Iteration 5:</strong><br />No relaxes in edge list so it has no negative cycle.<br />s = 0<br />a = 2<br />b = 4<br />c = 7<br />d = -2<br />Note: We can store the predecessor value on edge relaxing updates to get the path.</p>
<p>Here is a table summarizing the differences between DAG shortest path algorithms using topological sort, Dijkstra, and Bellman-Ford:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Algorithm</strong></td><td><strong>Applicability</strong></td><td><strong>Time complexity</strong></td><td><strong>Can handle negative weights?</strong></td><td><strong>Can detect negative weight cycles?</strong></td></tr>
</thead>
<tbody>
<tr>
<td>DAG shortest path using topological sort</td><td>Directed acyclic graphs (DAGs)</td><td>O(V + E)</td><td>No</td><td>No</td></tr>
<tr>
<td>Dijkstra</td><td>General graphs</td><td>O(E * log V)</td><td>No</td><td>No</td></tr>
<tr>
<td>Bellman-Ford</td><td>General graphs</td><td>O(VE)</td><td>Yes</td><td>Yes</td></tr>
</tbody>
</table>
</div><p>Thank you for reading the article.</p>
]]></content:encoded></item><item><title><![CDATA[AUST OPEN: Campus Companion for Seamless Excellence!!]]></title><description><![CDATA[AUST OPEN is a comprehensive Android app designed exclusively for AUST students, providing a one-stop solution for all your academic and campus-related needs. With an intuitive user interface and useful features, this app is your go-to companion for ...]]></description><link>https://blog.khandokeranan.com/aust-open-android</link><guid isPermaLink="true">https://blog.khandokeranan.com/aust-open-android</guid><category><![CDATA[android app development]]></category><category><![CDATA[#universityexperience]]></category><category><![CDATA[Dhaka]]></category><category><![CDATA[aust]]></category><category><![CDATA[Ahsanullah University of Engineering and Technology]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Wed, 30 Aug 2023 19:45:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1693299191324/d148ede4-4764-4a1c-b8ab-680c66ba9574.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>AUST OPEN</strong> is a comprehensive Android app designed exclusively for AUST students, providing a one-stop solution for all your academic and campus-related needs. With an intuitive user interface and useful features, this app is your go-to companion for maximizing productivity and staying up-to-date with the latest information. This app is only available on Android phones.</p>
<p><strong>TARGET AUDIENCE</strong><br />Ongoing students Of <em>Ahsanullah University of Science and Technology</em>, Bangladesh. Only AUST students can explore features signing with their edu mail provided by aust.edu</p>
<p><strong>ANDROID VERSION</strong><br />Android 6.0 Marshmallow (API level 23) or higher.</p>
<p><strong>FEATURE LIST</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Features Name</strong></td><td><strong>Completion Rate (%)</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Finding Vacant Classroom</td><td>100%</td></tr>
<tr>
<td>Department-wise Teacher Information</td><td>100%</td></tr>
<tr>
<td>AUST Live Notices Web Scrapping with PDF View</td><td>100%</td></tr>
<tr>
<td>Class Routine Management and Set Alarm</td><td>65%</td></tr>
<tr>
<td>AUST Bus Schedule</td><td>100%</td></tr>
<tr>
<td>CR/SR/Editor's Routine Management Panel</td><td>40%</td></tr>
<tr>
<td>Semester's Routine Data Extraction using Form Data Recognition</td><td>10%</td></tr>
</tbody>
</table>
</div><p><strong>SCREENSHOTS</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693299210388/2db55486-9e83-4339-9b49-8cc30bab6718.png" alt="Home Page, Vacant Rooms, Information Page" class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693299218514/60aca00d-2fbb-499a-8c11-287e823de763.png" alt="Live Notices, Class Routine and Teacher Information Page" class="image--center mx-auto" /></p>
<p><strong>APP LINK</strong></p>
<p><a target="_blank" href="https://github.com/anwholesquare/aust_open_android/blob/main/app-debug.apk">https://github.com/anwholesquare/aust_open_android/blob/main/app-debug.apk</a>  </p>
<p><strong>PRESENTATION</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386303318/67f91a84-3493-44e8-abd3-095799d7f316.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386319319/7a783e1d-56b4-49b6-aa3b-ab75d192236c.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386334701/4c8d896b-7014-49c0-b3d8-c7274a9ad115.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386343923/ee6b5e88-ed13-4b9c-8fa7-bdb7dd3f80eb.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386352932/16ba1f2a-32f9-4d3e-ac9f-65b321145d1c.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386368465/337f7587-7f8b-4612-885f-1545338abe6c.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693386380651/23867e6b-9a95-416c-badf-c91b23ca6c1c.jpeg" alt class="image--center mx-auto" /></p>
<p>The new update is coming soon for both Android and iOS using Flutter.</p>
]]></content:encoded></item><item><title><![CDATA[Assembler 8086 Shortcuts]]></title><description><![CDATA[This article is intended for those with basic knowledge of assemblers and basic ideas of high-level programming languages. To go through the details, we need to download the emu8086 software.
DOWNLOAD EMU8086https://emu8086-microprocessor-emulator.en...]]></description><link>https://blog.khandokeranan.com/assembler-8086-shortcuts</link><guid isPermaLink="true">https://blog.khandokeranan.com/assembler-8086-shortcuts</guid><category><![CDATA[assembly language]]></category><category><![CDATA[assembler]]></category><category><![CDATA[microprocessors]]></category><category><![CDATA[8086]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Mon, 28 Aug 2023 09:06:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1693213446091/7e63cbe2-6f73-4f3f-bdf9-fa685ffa1e3e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This article is intended for those with basic knowledge of assemblers and basic ideas of high-level programming languages. To go through the details, we need to download the emu8086 software.</p>
<p><strong>DOWNLOAD EMU8086</strong><br /><a target="_blank" href="https://emu8086-microprocessor-emulator.en.softonic.com/">https://emu8086-microprocessor-emulator.en.softonic.com/</a>  </p>
<p><strong>TOPICS WE WILL REVIEW</strong></p>
<ol>
<li><p>Registers,</p>
</li>
<li><p>Template</p>
</li>
<li><p>I/O,</p>
</li>
<li><p>Operations,</p>
</li>
<li><p>Conditions,</p>
</li>
<li><p>Loop,</p>
</li>
<li><p>Array,</p>
</li>
<li><p>Stack</p>
</li>
</ol>
<ol>
<li><p><strong>REGISTERS</strong></p>
<p> The 8086 microprocessor, part of the x86 family of processors, has a set of registers that play a crucial role in its operation. These registers store data, addresses, and intermediate results during the execution of instructions. The 8086 assembler registers can be categorized into various types based on their functionality:</p>
<ol>
<li><p><strong>General Purpose Registers:</strong></p>
<ul>
<li><p>AX (Accumulator): This 16-bit register is used for arithmetic and logical operations. It's often used in conjunction with other registers for more complex operations.</p>
</li>
<li><p>BX (Base): Another 16-bit register that can be used to address memory or as a general-purpose register.</p>
</li>
<li><p>CX (Count): Often used as a loop counter in string and loop instructions. It can also be used for arithmetic operations.</p>
</li>
<li><p>DX (Data): This register is used for I/O operations and extended precision arithmetic.</p>
</li>
</ul>
</li>
<li><p><strong>Segment Registers:</strong></p>
<ul>
<li><p>CS (Code Segment): Points to the start of the code segment in memory.</p>
</li>
<li><p>DS (Data Segment): Points to the start of the data segment, where variables and data are stored.</p>
</li>
<li><p>SS (Stack Segment): Points to the stack segment, which is used for storing temporary data during function calls and other operations.</p>
</li>
<li><p>ES (Extra Segment): An additional segment register that can be used to point to another data segment.</p>
</li>
</ul>
</li>
<li><p><strong>Pointer Registers:</strong></p>
<ul>
<li><p>SP (Stack Pointer): Points to the top of the stack in the stack segment.</p>
</li>
<li><p>BP (Base Pointer): Often used to access parameters passed to functions and local variables within stack frames.</p>
</li>
<li><p>SI (Source Index): Used as an index for string operations.</p>
</li>
<li><p>DI (Destination Index): Similar to SI, but used as an index for destination data in string operations.</p>
</li>
</ul>
</li>
<li><p><strong>Index Registers:</strong></p>
<ul>
<li><p>IP (Instruction Pointer): Points to the next instruction to be executed in the code segment.</p>
</li>
<li><p>Flags Register (FLAGS): Contains various flag bits that reflect the status of previous arithmetic and logic operations. These flags are used for conditional branching.</p>
</li>
</ul>
</li>
</ol>
</li>
<li><p><strong>Template</strong></p>
<pre><code class="lang-clojure"> .MODEL SMALL
 .STACK <span class="hljs-number">100</span>H

 .DATA
 <span class="hljs-comment">; ALL DATA MUST BE HERE EXCEPT REGISTERS</span>
 <span class="hljs-comment">; DB = DEFINE BYTE | DW = DEFINE WORD </span>
 MSG DB 'HELLO WORLD $'

 .CODE
 MAIN PROC
      <span class="hljs-comment">; IT IS A COMMENT</span>

      MAIN ENDP
 END MAIN
</code></pre>
</li>
<li><p><strong>I/O</strong></p>
<ol>
<li><p><strong>Print "Hello World":</strong></p>
<pre><code class="lang-clojure"> .MODEL SMALL
 .STACK <span class="hljs-number">100</span>H

 .DATA
 MSG DB 'HELLO WORLD $'

 .CODE
 MAIN PROC
      MOV AX, @DATA
      MOV DS, AX

      MOV AH, 
      LEA DX, MSG
      INT <span class="hljs-number">21</span>H

      MAIN ENDP
 END
</code></pre>
</li>
<li><p><strong>Read a character and print the character in a new line:</strong></p>
<pre><code class="lang-clojure"> .MODEL SMALL
 .STACK <span class="hljs-number">100</span>H

 .CODE
 MAIN PROC
     <span class="hljs-comment">; Display a character prompt and read a character from the keyboard</span>
     MOV AH, <span class="hljs-number">1</span>       <span class="hljs-comment">; Set AH to 1 to indicate "Read Character" function</span>
     INT <span class="hljs-number">21</span>H         <span class="hljs-comment">; Call the DOS interrupt to read a character from standard input</span>

     MOV BL, AL      <span class="hljs-comment">; Store the input character in BL register</span>

     <span class="hljs-comment">; Display newline character (LF)</span>
     MOV AH, <span class="hljs-number">2</span>       <span class="hljs-comment">; Set AH to 2 to indicate "Display Character" function</span>
     MOV DL, <span class="hljs-number">10</span>      <span class="hljs-comment">; Set DL to ASCII code 10 (newline character)</span>
     INT <span class="hljs-number">21</span>H         <span class="hljs-comment">; Call the DOS interrupt to display the newline character</span>

     <span class="hljs-comment">; Display carriage return character (CR)</span>
     MOV AH, <span class="hljs-number">2</span>       <span class="hljs-comment">; Set AH to 2 again</span>
     MOV DL, <span class="hljs-number">13</span>      <span class="hljs-comment">; Set DL to ASCII code 13 (carriage return character)</span>
     INT <span class="hljs-number">21</span>H         <span class="hljs-comment">; Call the DOS interrupt to display the carriage return character</span>

     <span class="hljs-comment">; Display the stored character in BL</span>
     MOV AH, <span class="hljs-number">2</span>       <span class="hljs-comment">; Set AH to 2 again</span>
     MOV DL, BL      <span class="hljs-comment">; Set DL to the value stored in BL (the input character)</span>
     INT <span class="hljs-number">21</span>H         <span class="hljs-comment">; Call the DOS interrupt to display the character</span>

     MAIN ENDP
 END
</code></pre>
</li>
</ol>
</li>
<li><p><strong>Operations</strong></p>
<pre><code class="lang-clojure"> <span class="hljs-number">1</span>. SUM:
 ADD AX, BX<span class="hljs-comment">; AX = AX + BX</span>

 <span class="hljs-number">2</span>. SUBSTRACT:
 SUB AX, BX<span class="hljs-comment">; AX = AX - BX</span>

 <span class="hljs-number">3</span>. MULTIPLY:
 MOV AX, <span class="hljs-number">-4</span>
 IMUL BX<span class="hljs-comment">; AX * BX</span>

 <span class="hljs-number">4</span>. DIVISION:
 MOV AL, NUM_<span class="hljs-number">3</span> 
 MOV BL, <span class="hljs-number">3</span> 
 IDIV BL<span class="hljs-comment">; AL / BL</span>

 <span class="hljs-number">5</span>. LOGICAL SHIFT:

 ROL AX, <span class="hljs-number">1</span><span class="hljs-comment">; ROTATE LEFT FOR THE VALUE OF AX REGISTER</span>
 ROR AX, <span class="hljs-number">1</span><span class="hljs-comment">; ROTATE RIGHT FOR THE VALUE OF AX REGISTER</span>
 SHL AX, <span class="hljs-number">1</span><span class="hljs-comment">; SHIFT LEFT FOR THE VALUE OF AX REGISTER</span>
 SRL AX, <span class="hljs-number">1</span><span class="hljs-comment">; SHIFT RIGHT FOR THE VALUE OF AX REGISTER</span>

 <span class="hljs-number">6</span>. LOGICAL OPERATION:

 AND AX, BX
 OR AX, BX
 NOT AX
 XOR AX, BX
</code></pre>
</li>
<li><p><strong>Conditions</strong></p>
<ol>
<li><strong>Read two decimal digits and print the largest digit:</strong></li>
</ol>
</li>
</ol>
<pre><code class="lang-clojure">    .MODEL SMALL
    .STACK <span class="hljs-number">100</span>H

    .DATA
    FIRST DB 'FIRST INPUT: $'
    SECOND DB 'SECOND INPUT: $'

    .CODE
    MAIN PROC     
         <span class="hljs-comment">; Initialize DS register with the address of the data segment</span>
         MOV AX, @DATA
         MOV DS, AX

         <span class="hljs-comment">; Display "FIRST INPUT: " prompt</span>
         MOV AH, <span class="hljs-number">9</span>
         LEA DX, FIRST
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Read a character from the keyboard</span>
         MOV AH, <span class="hljs-number">1</span>
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Store the input character in BL register</span>
         MOV BL, AL 

         <span class="hljs-comment">; Display newline character</span>
         MOV AH, <span class="hljs-number">2</span>
         MOV DL, <span class="hljs-number">10</span>
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Display carriage return character</span>
         MOV AH, <span class="hljs-number">2</span>
         MOV DL, <span class="hljs-number">13</span>
         INT <span class="hljs-number">21</span>H 

         <span class="hljs-comment">; Display "SECOND INPUT: " prompt</span>
         MOV AH, <span class="hljs-number">9</span>
         LEA DX, SECOND
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Read another character from the keyboard</span>
         MOV AH, <span class="hljs-number">1</span>
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Store the input character in BH register</span>
         MOV BH, AL 

         <span class="hljs-comment">; Display newline character</span>
         MOV AH, <span class="hljs-number">2</span>
         MOV DL, <span class="hljs-number">10</span>
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Display carriage return character</span>
         MOV AH, <span class="hljs-number">2</span>
         MOV DL, <span class="hljs-number">13</span>
         INT <span class="hljs-number">21</span>H

         <span class="hljs-comment">; Compare BH and BL, and set DL to the larger of the two</span>
         MOV DL, BH
         CMP BH, BL
         JG PRINT  
         MOV DL, BL


    PRINT:
         <span class="hljs-comment">; Display the value stored in DL (larger of BH and BL)</span>
         MOV AH, <span class="hljs-number">2</span>
         INT <span class="hljs-number">21</span>H


         MAIN ENDP
    END
</code></pre>
<p>    More Jump Instructions<br />    1. JC: Stands for 'Jump if Carry'</p>
<p>    2. JNC: Stands for 'Jump if Not Carry'</p>
<p>    3. JE / JZ: Stands for 'Jump if Equal' or 'Jump if Zero'</p>
<p>    4. JNE / JNZ: Stands for 'Jump if Not Equal' or 'Jump if Not Zero'</p>
<p>    5. JP / JPE: Stands for 'Jump if Parity' or 'Jump if Even Parity'</p>
<p>    6. JNP / JPO: Stands for 'Jump if Not Parity' or 'Jump if Odd Parity'</p>
<p>    Note: For multi-conditions, we may need to use unconditional jump condition which is <strong><em>JMP</em></strong>  </p>
<ol>
<li><p><strong>LOOP</strong></p>
<ol>
<li><strong>Print “HELLO WORLD” 10 times:</strong></li>
</ol>
</li>
</ol>
<pre><code class="lang-clojure">    .MODEL SMALL
    .STACK <span class="hljs-number">100</span>H

    .DATA
    MSG DB 'HELLO WORLD $'  <span class="hljs-comment">; Define a null-terminated string message</span>
    NEWLINE DB <span class="hljs-number">13</span>,<span class="hljs-number">10</span>, '$'   <span class="hljs-comment">; Define a newline sequence (carriage return + line feed)</span>

    .CODE
    MAIN PROC 
        <span class="hljs-comment">; Initialize DS register with the address of the data segment</span>
        MOV AX, @DATA
        MOV DS, AX     

        MOV CX, <span class="hljs-number">10</span>       <span class="hljs-comment">; Set CX to the number of times to repeat the printing</span>

    PRINT:
        <span class="hljs-comment">; Display the message</span>
        MOV AH, <span class="hljs-number">9</span>        <span class="hljs-comment">; Set AH to 9 to indicate "Display String" function</span>
        LEA DX, MSG      <span class="hljs-comment">; Load the address of MSG into DX</span>
        INT <span class="hljs-number">21</span>H          <span class="hljs-comment">; Call the DOS interrupt to display the message</span>

        <span class="hljs-comment">; Display a newline</span>
        MOV AH, <span class="hljs-number">9</span>        <span class="hljs-comment">; Set AH to 9 again</span>
        LEA DX, NEWLINE  <span class="hljs-comment">; Load the address of NEWLINE into DX</span>
        INT <span class="hljs-number">21</span>H          <span class="hljs-comment">; Call the DOS interrupt to display the newline</span>

        LOOP PRINT       <span class="hljs-comment">; Decrement CX and repeat the loop if CX is not zero</span>

    MAIN ENDP
    END
</code></pre>
<ol>
<li><p><strong>Array</strong></p>
<ol>
<li><p><strong>Read 10 numbers and find the average of the sum:</strong></p>
<pre><code class="lang-clojure"> .MODEL SMALL
 .STACK <span class="hljs-number">100</span>H

 .DATA
     numbers DB <span class="hljs-number">10</span> DUP (<span class="hljs-name">?</span>)  <span class="hljs-comment">; Array to store 10 numbers</span>
     prompt DB 'Enter a number: $'
     newline DB <span class="hljs-number">13</span>, <span class="hljs-number">10</span>, '$'
     avgMsg DB 'Average: $'

 .CODE
 MAIN PROC 
     <span class="hljs-comment">; Initialize DS register with the address of the data segment</span>
     MOV AX, @DATA
     MOV DS, AX

     <span class="hljs-comment">; Initialize variables</span>
     MOV CX, <span class="hljs-number">10</span>          <span class="hljs-comment">; Set CX to the number of elements</span>
     MOV SI, <span class="hljs-number">0</span>           <span class="hljs-comment">; Initialize index for array traversal</span>
     MOV BX, <span class="hljs-number">0</span>           <span class="hljs-comment">; Initialize sum</span>

 INPUT_LOOP:
     <span class="hljs-comment">; Display prompt</span>
     MOV AH, <span class="hljs-number">9</span>
     LEA DX, prompt
     INT <span class="hljs-number">21</span>H

     <span class="hljs-comment">; Read a number</span>
     MOV AH, <span class="hljs-number">1</span>
     INT <span class="hljs-number">21</span>H
     SUB AL, '<span class="hljs-number">0</span>'         <span class="hljs-comment">; Convert ASCII character to numerical value</span>
     MOV numbers[SI], AL <span class="hljs-comment">; Store the number in the array at index SI</span>

     INC SI              <span class="hljs-comment">; Move to the next index in the array</span>

     LOOP INPUT_LOOP

     <span class="hljs-comment">; Calculate the sum of the numbers</span>
     MOV CX, <span class="hljs-number">10</span>          <span class="hljs-comment">; Reset CX for sum calculation</span>
     MOV SI, <span class="hljs-number">0</span>           <span class="hljs-comment">; Reset index for array traversal</span>
     MOV BX, <span class="hljs-number">0</span>           <span class="hljs-comment">; Reset sum</span>

 SUM_LOOP:
     ADD BL, numbers[SI]
     INC SI
     LOOP SUM_LOOP

     <span class="hljs-comment">; Calculate the average</span>
     MOV AL, BL
     MOV BL, <span class="hljs-number">10</span>
     DIV BL              <span class="hljs-comment">; AL = Sum / 10</span>

     <span class="hljs-comment">; Display the average</span>
     MOV AH, <span class="hljs-number">9</span>
     LEA DX, avgMsg
     INT <span class="hljs-number">21</span>H

     MOV AH, <span class="hljs-number">2</span>
     ADD AL, '<span class="hljs-number">0</span>'
     INT <span class="hljs-number">21</span>H

     <span class="hljs-comment">; Display a newline</span>
     MOV AH, <span class="hljs-number">9</span>
     LEA DX, newline
     INT <span class="hljs-number">21</span>H

     <span class="hljs-comment">; Exit program</span>
     MOV AH, <span class="hljs-number">4</span>CH
     INT <span class="hljs-number">21</span>H

 MAIN ENDP
 END
</code></pre>
</li>
</ol>
</li>
<li><p><strong>STACK</strong></p>
<p> <img src="https://faculty.kfupm.edu.sa/COE/shazli/coe205/Help/stack.gif" alt class="image--center mx-auto" /></p>
<ol>
<li><strong>Balanced Bracket Problem using PUSH and POP in 8086 assembler:</strong></li>
</ol>
</li>
</ol>
<pre><code class="lang-clojure">.MODEL SMALL
.STACK <span class="hljs-number">100</span>H

.DATA
    expression DB '(<span class="hljs-name">a</span> + [b * {c + d}] - e) $'

.CODE
MAIN PROC
    <span class="hljs-comment">; Initialize DS register with the address of the data segment</span>
    MOV AX, @DATA
    MOV DS, AX

    <span class="hljs-comment">; Initialize stack pointer</span>
    MOV SP, <span class="hljs-number">100</span>H

    LEA SI, expression

CHECK_LOOP:
    MOV AL, [SI]
    CMP AL, '$'         <span class="hljs-comment">; Check for end of expression</span>
    JE EXPRESSION_END

    CMP AL, '(<span class="hljs-name">'</span>         <span class="hljs-comment">; Opening parenthesis</span>
    JE PUSH_STACK
    CMP AL, '['         <span class="hljs-comment">; Opening square bracket</span>
    JE PUSH_STACK
    CMP AL, '{'         <span class="hljs-comment">; Opening curly brace</span>
    JE PUSH_STACK

    CMP AL, ')'         <span class="hljs-comment">; Closing parenthesis</span>
    JE POP_STACK
    CMP AL, ']'         <span class="hljs-comment">; Closing square bracket</span>
    JE POP_STACK
    CMP AL, '}'         <span class="hljs-comment">; Closing curly brace</span>
    JE POP_STACK

    INC SI              <span class="hljs-comment">; Move to the next character</span>
    JMP CHECK_LOOP

PUSH_STACK:
    PUSH AL             <span class="hljs-comment">; Push opening bracket onto the stack</span>
    INC SI              <span class="hljs-comment">; Move to the next character</span>
    JMP CHECK_LOOP

POP_STACK:
    MOV AH, [SP]        <span class="hljs-comment">; Pop bracket from the stack</span>
    CMP AL, '(<span class="hljs-name">'</span>         <span class="hljs-comment">; Matching parenthesis</span>
    JE POP_STACK_DONE
    CMP AL, '['         <span class="hljs-comment">; Matching square bracket</span>
    JE POP_STACK_DONE
    CMP AL, '{'         <span class="hljs-comment">; Matching curly brace</span>
    JE POP_STACK_DONE

    <span class="hljs-comment">; Unmatched brackets, display error message and exit</span>
    MOV AH, <span class="hljs-number">9</span>
    LEA DX, UNMATCHED_MSG
    INT <span class="hljs-number">21</span>H

    <span class="hljs-comment">; Exit program</span>
    MOV AH, <span class="hljs-number">4</span>CH
    INT <span class="hljs-number">21</span>H

POP_STACK_DONE:
    ADD SP, <span class="hljs-number">1</span>           <span class="hljs-comment">; Pop one element from the stack</span>
    INC SI              <span class="hljs-comment">; Move to the next character</span>
    JMP CHECK_LOOP

EXPRESSION_END:
    <span class="hljs-comment">; Display balanced expression message</span>
    MOV AH, <span class="hljs-number">9</span>
    LEA DX, BALANCED_MSG
    INT <span class="hljs-number">21</span>H

    <span class="hljs-comment">; Exit program</span>
    MOV AH, <span class="hljs-number">4</span>CH
    INT <span class="hljs-number">21</span>H

.DATA
    UNMATCHED_MSG DB 'Unmatched brackets in expression', '$'
    BALANCED_MSG DB 'Expression is balanced', '$'

MAIN ENDP
END
</code></pre>
<h1 id="heading-to-be-continued"><strong><em>To be Continued ...</em></strong></h1>
]]></content:encoded></item><item><title><![CDATA[First outlook of the app 'Daily Sigma']]></title><description><![CDATA[This mobile application empowers individuals seeking to enhance their life's discipline, workout routines, mindset, and cash flow. With a unique approach to personal development, the app provides a comprehensive toolkit and go-to tool for personal gr...]]></description><link>https://blog.khandokeranan.com/daily-sigma-first-look</link><guid isPermaLink="true">https://blog.khandokeranan.com/daily-sigma-first-look</guid><category><![CDATA[app development]]></category><category><![CDATA[Flutter]]></category><category><![CDATA[Flutter Examples]]></category><category><![CDATA[Mindset]]></category><category><![CDATA[discipline]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Thu, 06 Jul 2023 12:00:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1688642999510/8a3c6fce-22f6-481e-abe0-a680b26fe282.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This mobile application empowers individuals seeking to enhance their life's discipline, workout routines, mindset, and cash flow. With a unique approach to personal development, the app provides a comprehensive toolkit and go-to tool for personal growth and development.</p>
<h2 id="heading-unlock-a-new-card-every-day">Unlock a new card every day</h2>
<p>Daily Sigma provides users with a structured and rewarding experience by presenting a new card to unlock daily. These 101 cards are carefully curated to offer valuable insights, challenges, and tasks to help users build positive habits, strengthen discipline, optimize their workouts, cultivate a growth mindset, and manage their cash flow effectively.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1688643686332/47ede38c-6a05-470c-86d9-6db51204cbfb.png" alt class="image--center mx-auto" /></p>
<p>This app uses the Google mobile application's Flutter framework. The dependencies used in this project are given below:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">flutter_screenutil:</span> <span class="hljs-string">^5.3.1</span>
<span class="hljs-attr">flutter_svg:</span> <span class="hljs-string">^0.23.0+1</span>
<span class="hljs-attr">font_awesome_flutter:</span> <span class="hljs-string">^[latest_version]</span>
<span class="hljs-attr">get:</span> <span class="hljs-string">^4.3.8</span>
<span class="hljs-attr">google_fonts:</span> <span class="hljs-string">^2.1.0</span>
<span class="hljs-attr">cupertino_icons:</span> <span class="hljs-string">^1.0.5</span>
</code></pre>
<h2 id="heading-comprehensive-life-disciplines">Comprehensive Life Disciplines</h2>
<p>Daily Sigma covers various life disciplines to ensure holistic personal growth. From developing healthy habits and time management techniques to effective financial planning and mental resilience strategies, the app provides valuable insights and actionable steps to help users thrive in every aspect of their lives.</p>
<h2 id="heading-inspiring-feed-for-inner-growth">Inspiring Feed for Inner Growth</h2>
<p>In addition to the card system, Daily Sigma offers a dynamic feed filled with relatable and uplifting posts. Discover thought-provoking articles, inspiring success stories, expert advice, and motivational quotes carefully curated to nourish your inner self. Engage with a vibrant community of like-minded individuals who share your passion for personal growth and exchange insights and experiences.</p>
<h2 id="heading-customize-your-experience">Customize Your Experience</h2>
<p>Tailor Daily Sigma to your unique needs and preferences. Choose from various themes, set reminders, and personalize your daily card challenges. Whether an early riser or a night owl, the app adapts to your lifestyle, ensuring you can engage with the content at your own pace.</p>
<p>This app will be published by excess3.com, a unique IT firm that works on mobile and web development, brand guidelines, and AR/VR.</p>
<p>If you have any queries, email me at <mark>khandokeranan@gmail.com</mark>.</p>
]]></content:encoded></item><item><title><![CDATA[Flutter State Management with GetX and many more...]]></title><description><![CDATA[Github link: https://github.com/anwholesquare/flutter_get_x
In this article, we will learn about Flutter state management with getX which is a powerful state management library for building applications using the Flutter framework. It offers a simple...]]></description><link>https://blog.khandokeranan.com/flutter-getx</link><guid isPermaLink="true">https://blog.khandokeranan.com/flutter-getx</guid><category><![CDATA[Flutter]]></category><category><![CDATA[GetX]]></category><category><![CDATA[State Management ]]></category><category><![CDATA[navigation]]></category><category><![CDATA[snackbar]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Sun, 02 Jul 2023 02:54:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1688248445403/ed080ea1-a020-4f83-b25f-14265566f7fd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><mark>Github link:</mark> <a target="_blank" href="https://github.com/anwholesquare/flutter_get_x">https://github.com/anwholesquare/flutter_get_x</a></p>
<p>In this article, we will learn about Flutter state management with getX which is a powerful state management library for building applications using the Flutter framework. It offers a simple and intuitive way to manage the state of your application, making it easier to develop and maintain complex UIs. Except for handling states within the app, here are some of the key functionalities and features that GetX offers:</p>
<ol>
<li><p>Dependency injection</p>
</li>
<li><p>Route management</p>
</li>
<li><p>Dialog and snackbar</p>
</li>
<li><p>Multi-language support</p>
</li>
<li><p>Animation management</p>
</li>
<li><p>Worker management</p>
</li>
</ol>
<p>Let's create a flutter boilerplate using this command:</p>
<pre><code class="lang-bash">flutter create --org com.yourcompanyname -i swift -a kotlin appname
</code></pre>
<p>Now we need to install two GetX components by adding these lines in</p>
<h3 id="heading-pubspecyaml">pubspec.yaml</h3>
<pre><code class="lang-yaml"><span class="hljs-attr">dependencies:</span>
  <span class="hljs-attr">flutter:</span>
    <span class="hljs-attr">sdk:</span> <span class="hljs-string">flutter</span>
  <span class="hljs-comment">#add these lines</span>
  <span class="hljs-attr">get:</span> <span class="hljs-string">^4.6.1</span>
  <span class="hljs-attr">get_storage:</span> <span class="hljs-string">^2.0.3</span>
</code></pre>
<p>Let's make an app where we can <strong><mark>change a text based on TextField input</mark></strong> and <strong><mark>toggle the app theme by ElavatedButton.</mark></strong></p>
<p>To do this, we need two GetXControllers where we will initialize our observable variables and functions that can update the widget. They are AppController and ThemeController.</p>
<h3 id="heading-appcontrollerdart">AppController.dart</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:get/get.dart'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">GetxController</span> </span>{
  <span class="hljs-keyword">var</span> appName = <span class="hljs-string">"GetX Flutter"</span>.obs;

  <span class="hljs-keyword">void</span> setAppName (<span class="hljs-built_in">String</span> _appName) {
    appName.value = _appName;
  }
}
</code></pre>
<p>Using this AppController class which extends GetxController, we will observe the value of the app name.</p>
<h3 id="heading-themecontrollerdart">ThemeController.dart</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:get/get.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:get_storage/get_storage.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThemeController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">GetxController</span> </span>{
    <span class="hljs-keyword">final</span> _box = GetStorage();
    <span class="hljs-keyword">final</span> themeModeKey = <span class="hljs-string">"isDarkMode"</span>;
    <span class="hljs-built_in">bool</span> _loadTheme() =&gt; _box.read(themeModeKey) ?? <span class="hljs-keyword">false</span>;

    ThemeData darkTheme = ThemeData(
      primarySwatch: Colors.red,
      brightness: Brightness.dark
    );

    ThemeData lightTheme = ThemeData(
      primarySwatch: Colors.red,
      brightness: Brightness.light
    );

    ThemeData <span class="hljs-keyword">get</span> theme =&gt; _loadTheme() ? darkTheme : lightTheme;

    <span class="hljs-keyword">void</span> toggleTheme () {
      _box.write(themeModeKey, !_loadTheme());
      Get.changeTheme(theme);
    }
}
</code></pre>
<p>In ThemeController.dart, we use GetStorage Library to persist the theme each time running the app on the mobile. By default, the theme will get the value of light theme as no data can be loaded as a theme from the _box or GetStorage. toggleTheme() is used to toggle the theme and save it in the _box.</p>
<h3 id="heading-maindart">main.dart</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter_get_x/Controller/AppController.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter_get_x/Controller/ThemeController.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter_get_x/HomePage.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:get/get.dart'</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span> {
  <span class="hljs-keyword">await</span> GetStorage.init();
  runApp(MyApp());
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{

  <span class="hljs-keyword">final</span> AppController appController = Get.put(AppController());
  <span class="hljs-keyword">final</span> ThemeController themeController = Get.put(ThemeController());
  MyApp({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> GetMaterialApp(
      home: HomePage(),
      debugShowCheckedModeBanner: <span class="hljs-keyword">false</span>,
      theme: themeController.theme,
    );
  }
}
</code></pre>
<p>Here we use GetMaterialApp as the root of widget tree which is similar to MaterialApp and we put the controller using <mark>Get.put()</mark> method to find those two controllers by <mark>Get.find&lt;ControllerName&gt;()</mark> method within the child widgets of the tree. Inside main() async, <mark>await GetStorage.init();</mark> this line is important to initialize the GetStorage();</p>
<p>Now lastly add the HomePage class to render the initial widget of MaterialApp.</p>
<h3 id="heading-homepagedart">HomePage.dart</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter_get_x/Controller/AppController.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter_get_x/Controller/ThemeController.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:get/get.dart'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomePage</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
  <span class="hljs-keyword">final</span> AppController appController = Get.find&lt;AppController&gt;();
  <span class="hljs-keyword">final</span> ThemeController themeController = Get.find&lt;ThemeController&gt;();
  TextEditingController appNameController = TextEditingController(); 
  HomePage({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> Scaffold(
      body: SafeArea(
        child: Obx( () =&gt;   
           Center(child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [  
              Padding(
                padding: <span class="hljs-keyword">const</span> EdgeInsets.all(<span class="hljs-number">8.0</span>),
                child: Text(appController.appName.value, style: <span class="hljs-keyword">const</span> TextStyle(fontSize: <span class="hljs-number">24</span>, fontWeight: FontWeight.w600),),
              ),    
              SizedBox(
                width: <span class="hljs-number">200</span>,
                child: TextField(
                  controller: appNameController,
                  textAlign: TextAlign.center,
                  decoration: <span class="hljs-keyword">const</span> InputDecoration(hintText: <span class="hljs-string">"Write App Name"</span>,),
                )
              ),
              <span class="hljs-keyword">const</span> SizedBox(height: <span class="hljs-number">10</span>,),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(onPressed: ()=&gt; 
                    appController.setAppName(appNameController.text)
                  , child: <span class="hljs-keyword">const</span> Text(<span class="hljs-string">"Submit"</span>)),
                  <span class="hljs-keyword">const</span> SizedBox(width: <span class="hljs-number">10</span>,),
                  ElevatedButton(onPressed: ()=&gt; 
                    themeController.toggleTheme()
                  ,
                  style: ElevatedButton.styleFrom(backgroundColor: Colors.black), child: <span class="hljs-keyword">const</span> Text(<span class="hljs-string">"Toggle Theme"</span>),
                  ),
                ],
              )      
            ],
           ))
        )
      ),
    );
  }
}
</code></pre>
<p>In the HomePage class, We use <mark>Obx( () =&gt; WidgetClass() )</mark> to render the widget frequently when an update is available by the controller classes' observable variables.</p>
<p>Now this app looks like this.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1688250640420/5b3e1043-e423-4388-884d-d893a42d3d33.png?height=600" alt class="image--center mx-auto" /></p>
<p>In summary, we need to make a controller to observe variables (adding .obx at the end of the value) that update the widgets inside the Obx widget. And GetStorage() can read and write the data for persistence. All of these works have been done in this GitHub repository.</p>
<p>Checkout till this point: <mark>git checkout d6b63d2036b6c6fa178f114a3ca5ed5b198e94dc</mark></p>
<p>Now your task is to add a few features to the app using GetX and GetStorage:</p>
<ol>
<li><p>Login system based on email and password,</p>
</li>
<li><p>Change password page</p>
</li>
<li><p>Logout option</p>
</li>
</ol>
<p>To do this task, there are a few more things to learn.</p>
<ol>
<li><pre><code class="lang-dart">  Get.to( () =&gt; NewPage() );
  <span class="hljs-comment">// this code is used to go to the new Page </span>
  <span class="hljs-comment">// and leave the current page in a stack.</span>
</code></pre>
</li>
<li><pre><code class="lang-dart">  Get.off(() =&gt; NewPage());
  <span class="hljs-comment">// this code is used to go to the new Page </span>
  <span class="hljs-comment">// and remove the current page from the stack.</span>
</code></pre>
</li>
<li><pre><code class="lang-dart">  Get.offAll( () =&gt; NewPage());
  <span class="hljs-comment">// this code is used to go to the new Page </span>
  <span class="hljs-comment">// and clear the stack</span>
</code></pre>
</li>
<li><pre><code class="lang-dart">  <span class="hljs-comment">// add this importation for the persistance</span>
  <span class="hljs-keyword">import</span> <span class="hljs-string">'package:get_storage/get_storage.dart'</span>;

  <span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
    <span class="hljs-comment">// to work after restarting the app, we need to add this line</span>
    <span class="hljs-keyword">await</span> GetStorage.init();
    runApp(MyApp());
  }
  <span class="hljs-comment">// to write to the storage via key-value pair</span>
  GetStorage().write(<span class="hljs-string">"email"</span>, <span class="hljs-string">"khandokerxyz@gmail.com"</span>);
  <span class="hljs-comment">// to read from the storage via key</span>
  GetStorage().read(<span class="hljs-string">"email"</span>);
</code></pre>
<p> To use GetX SnackBar, you can use this snippet:</p>
</li>
<li><pre><code class="lang-dart"> Get.snackbar(<span class="hljs-string">"Flutter with GetX"</span>, <span class="hljs-string">"Logging out from the device"</span>, snackPosition: SnackPosition.BOTTOM);
</code></pre>
<p> If you're stuck on this task, you can see how it has been done:</p>
</li>
<li><pre><code class="lang-dart">  git clone https:<span class="hljs-comment">//github.com/anwholesquare/flutter_get_x.git</span>
  git checkout <span class="hljs-number">0</span>f42abc84d33f2268c9e58eb282d2daad8b2ff7e
</code></pre>
<p> The outcome of the task may seem like this video:</p>
</li>
<li><div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=aNNVR2fYVCs">https://www.youtube.com/watch?v=aNNVR2fYVCs</a></div>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Android XML Design Review]]></title><description><![CDATA[This article will review different layouts and widgets like Buttons, Toggle-Buttons, Spinners, Clock, RadioGroup and its RadioButtons, Checkboxes, CardView, Switch, Chip, Form building, Time Picker, Date Picker, etc.
Layouts
There are many layouts wh...]]></description><link>https://blog.khandokeranan.com/android-xml-design-review</link><guid isPermaLink="true">https://blog.khandokeranan.com/android-xml-design-review</guid><category><![CDATA[Android]]></category><category><![CDATA[linearlayout]]></category><category><![CDATA[relativeLayout]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Wed, 21 Jun 2023 06:18:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1686209065108/eb50540e-6744-4008-aa39-ec763da93b60.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This article will review different layouts and widgets like Buttons, Toggle-Buttons, Spinners, Clock, RadioGroup and its RadioButtons, Checkboxes, CardView, Switch, Chip, Form building, Time Picker, Date Picker, etc.</p>
<h2 id="heading-layouts">Layouts</h2>
<p>There are many layouts where you can add components to it. They are:</p>
<ol>
<li><h3 id="heading-relative-layout">Relative Layout</h3>
</li>
</ol>
<p>The Android Relative Layout is a type of layout manager in Android that allows you to position views relative to each other or the parent container. It enables you to define the position of child views based on their relationship to other views, such as aligning them to the left, right, top, bottom, or center and specifying their margin.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687277400350/14b2dc18-5220-4e07-8058-76d2a7d7c447.png?height=600" alt="Relative Layout" class="image--center mx-auto" /></p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">RelativeLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:text</span>=<span class="hljs-string">"I'm the parent"</span>
        <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/mainBtn"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"10dp"</span>
        <span class="hljs-attr">android:layout_centerHorizontal</span>=<span class="hljs-string">"true"</span>
        <span class="hljs-attr">android:layout_centerVertical</span>=<span class="hljs-string">"true"</span>
    /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:text</span>=<span class="hljs-string">"Baccha Down Left"</span>
        <span class="hljs-attr">android:layout_marginLeft</span>=<span class="hljs-string">"20dp"</span>
        <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/leftMainBtn"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_toLeftOf</span>=<span class="hljs-string">"@+id/mainBtn"</span>
        <span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">"@+id/mainBtn"</span>/&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:text</span>=<span class="hljs-string">"Baccha Down Right"</span>
        <span class="hljs-attr">android:layout_marginRight</span>=<span class="hljs-string">"20dp"</span>
        <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/RightMainBtn"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_toRightOf</span>=<span class="hljs-string">"@+id/mainBtn"</span>
        <span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">"@+id/mainBtn"</span>/&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:text</span>=<span class="hljs-string">"Baccha Up Left"</span>
        <span class="hljs-attr">android:layout_marginLeft</span>=<span class="hljs-string">"20dp"</span>
        <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/leftMainBtn1"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_toLeftOf</span>=<span class="hljs-string">"@+id/mainBtn"</span>
        <span class="hljs-attr">android:layout_above</span>=<span class="hljs-string">"@+id/mainBtn"</span>/&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
        <span class="hljs-attr">android:text</span>=<span class="hljs-string">"Baccha Up Right"</span>
        <span class="hljs-attr">android:layout_marginRight</span>=<span class="hljs-string">"20dp"</span>
        <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/RightMainBtn1"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_toRightOf</span>=<span class="hljs-string">"@+id/mainBtn"</span>
        <span class="hljs-attr">android:layout_above</span>=<span class="hljs-string">"@+id/mainBtn"</span>/&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">RelativeLayout</span>&gt;</span>
</code></pre>
<p>Here are some common use cases:</p>
<ol>
<li><p><strong>User Interfaces with Flexible Positioning:</strong> You can position views relative to each other, making it easier to adapt to different screen sizes or orientations.</p>
</li>
<li><p><strong>Form-Based Screens:</strong> Relative Layout is well-suited for forms or input screens where you need to align labels and input fields in a specific manner.</p>
</li>
<li><p><strong>Custom List Items:</strong> If you are creating a custom listview or a recycler view, Relative Layout can define the layout of individual list items.</p>
</li>
<li><p><strong>Toolbars and Action Bars:</strong> You can position the title, action buttons, and other components relative to each other, allowing you to create a consistent and organized layout for your app's navigation and actions.</p>
</li>
<li><p><strong>Overlapping Views:</strong> Relative Layout enables you to create views that overlap each other, which can be useful for creating effects like pop-up windows, tooltips, or overlay screens.</p>
</li>
</ol>
<ol>
<li><h3 id="heading-linear-layout">Linear Layout</h3>
</li>
</ol>
<p>The Linear Layout is a type of layout manager in Android that arranges child views in a single direction, either horizontally or vertically. It provides a straightforward and flexible way to create user interfaces by linearly aligning views one after another.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687279195011/590ad961-d693-4a4b-821d-ec2ff613ab0f.png?height=600" alt class="image--center mx-auto" /></p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"vertical"</span>
    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
    <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"10dp"</span>
    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>&gt;</span>




    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"1"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"2"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"3"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"4"</span>/&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"160dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"5"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"6"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"7"</span>/&gt;</span>


    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"8"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"160dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"9"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"10"</span>/&gt;</span>


    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"11"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"75dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"12"</span>/&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"160dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"13"</span>/&gt;</span>


    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>&gt;</span>


        <span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"330dp"</span>
            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"80dp"</span>
            <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">"5dp"</span>
            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"32sp"</span>
            <span class="hljs-attr">android:text</span>=<span class="hljs-string">"SUBMIT"</span>/&gt;</span>


    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>
</code></pre>
<p>Here are some use cases of Linear Layout:</p>
<ol>
<li><p><strong>Lists and RecyclerView Items:</strong> Linear Layout is commonly used to create lists or RecyclerView items in Android applications.</p>
</li>
<li><p><strong>Forms and Input Screens:</strong> Linear Layout is useful for creating forms or input screens where you must organize various input fields, labels, or buttons in a linear order.</p>
</li>
<li><p><strong>Chat Bubbles:</strong> You can achieve a linear representation of messages in a conversation by aligning views horizontally or vertically within each chat bubble.</p>
</li>
<li><p><strong>Navigation Bars:</strong> Linear Layout can create navigation bars at the bottom or top of the screen. By arranging icons or buttons horizontally within the linear layout, you can provide easy navigation options for users.</p>
</li>
<li><p><strong>Sliders and Seek Bars:</strong> Linear Layout is suitable for creating sliders or seek bars that allow users to select a value within a range.</p>
</li>
<li><p><strong>Media Players:</strong> Linear Layout can create media player controls, such as play/pause buttons, progress bars, and volume controls.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Ugh, Not Sorting Algorithms Again!]]></title><description><![CDATA[Before starting the advanced topics for sorting elements, we must have these basic sorting methods:

Bubble Sort

Quick Sort

Merge Sort

Insertion Sort

Selection Sort


Let's quickly review the algorithm one by one below:
Bubble Sort
The idea is to...]]></description><link>https://blog.khandokeranan.com/sorting-algorithms-again</link><guid isPermaLink="true">https://blog.khandokeranan.com/sorting-algorithms-again</guid><category><![CDATA[sorting]]></category><category><![CDATA[#SortingTechniques]]></category><category><![CDATA[merge sort]]></category><category><![CDATA[Quick Sort]]></category><category><![CDATA[bubble sort]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Thu, 11 May 2023 18:41:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683749313743/c3b17591-6b35-4f03-8ff6-ca5bef94316f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before starting the advanced topics for sorting elements, we must have these basic sorting methods:</p>
<ol>
<li><p>Bubble Sort</p>
</li>
<li><p>Quick Sort</p>
</li>
<li><p>Merge Sort</p>
</li>
<li><p>Insertion Sort</p>
</li>
<li><p>Selection Sort</p>
</li>
</ol>
<p>Let's quickly review the algorithm one by one below:</p>
<h3 id="heading-bubble-sort">Bubble Sort</h3>
<p><em>The idea is to place the largest/minimal element in its position and keep doing the same for every other element.</em></p>
<p>Code:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">bubble</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> n = v.size();
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i&lt;n; i++) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j =<span class="hljs-number">0</span>; j&lt;n; j++) {
            <span class="hljs-keyword">if</span> (v[i] &lt; v[j]) 
                swap(v[i], v[j]);
        }
    }
}
</code></pre>
<h3 id="heading-selection-sort">Selection Sort</h3>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20230419183518/selection-sort-geeksforgeeks.gif" alt /></p>
<p>Code:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">selection</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> n = v.size();
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i&lt;n; i++) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j =i+<span class="hljs-number">1</span>; j&lt;n; j++) {
            <span class="hljs-keyword">if</span> (v[i] &gt; v[j]) 
                swap(v[i], v[j]);
        }
    }
}
</code></pre>
<h3 id="heading-insertion-sort">Insertion Sort</h3>
<p><img src="https://i.pinimg.com/originals/92/b0/34/92b034385c440e08bc8551c97df0a2e3.gif" alt="Sorting Algorithm Explained With GIF Animations | Insertion sort, Bubble  sort, Bubble sort algorithm" /></p>
<p>Code:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertion</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> n = v.size();
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>;i&lt;n;i++){
        <span class="hljs-keyword">int</span> x = v[i];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i<span class="hljs-number">-1</span>; j &gt;=<span class="hljs-number">0</span>; j--) {
            <span class="hljs-keyword">if</span> (x &lt; v[j])  {
                v[j+<span class="hljs-number">1</span>] = v[j];
                v[j] = x;
            }<span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">break</span>;
            }
        }
    }
}
</code></pre>
<h3 id="heading-quick-sort">Quick Sort:</h3>
<p><img src="https://engineering.fb.com/wp-content/uploads/2022/07/Hermes-quicksort.gif?w=720" alt="Using Hermes's Quicksort to run Doom: A tale of JavaScript exploitation" /></p>
<p>Code:</p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">partition</span> <span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt;&amp; v, <span class="hljs-keyword">int</span> low, <span class="hljs-keyword">int</span> high)</span> </span>{
    <span class="hljs-keyword">int</span> pivot = v[low];
    <span class="hljs-keyword">int</span> i = low +<span class="hljs-number">1</span>;
    <span class="hljs-keyword">int</span> j = high;
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">while</span> (i &lt;= j &amp;&amp; v[i] &lt;= pivot) {
            i++;
        }
        <span class="hljs-keyword">while</span> ( i &lt;= j &amp;&amp; v[j] &gt;= pivot) {
            j--;
        }
        <span class="hljs-keyword">if</span> (i &lt;= j) {
            swap (v[i], v[j]);
        }<span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">break</span>;
        }
    }
    swap(v[low], v[j]);
    <span class="hljs-keyword">return</span> j;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">quicksort</span> <span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt;&amp; v, <span class="hljs-keyword">int</span> low, <span class="hljs-keyword">int</span> high)</span>
</span>{
    <span class="hljs-keyword">if</span> (low &lt; high) {
        <span class="hljs-keyword">int</span> pi = partition (v, low, high);
        quicksort(v, low, pi<span class="hljs-number">-1</span>);
        quicksort(v, pi+<span class="hljs-number">1</span>, high);    
    }
}
</code></pre>
<h3 id="heading-merge-sort">Merge Sort</h3>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20230331125035/Merge-sort-gif-(1).gif" alt="Merge Sort Algorithm - GeeksforGeeks" /></p>
<p>Code:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-keyword">int</span> arr[] = {<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">-30</span>, <span class="hljs-number">-02</span>, <span class="hljs-number">3</span>, <span class="hljs-number">-60</span>};
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">merge</span><span class="hljs-params">(<span class="hljs-keyword">int</span> low, <span class="hljs-keyword">int</span> mid, <span class="hljs-keyword">int</span> hi)</span>
</span>{
    <span class="hljs-keyword">int</span> sa1 = mid - low + <span class="hljs-number">1</span>;
    <span class="hljs-keyword">int</span> sa2 = hi - mid;
    <span class="hljs-keyword">int</span> L[sa1 + <span class="hljs-number">1</span>], R[sa2 + <span class="hljs-number">1</span>];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; sa1; i++)
    {
        L[i] = arr[low + i];
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; sa2; j++)
    {
        R[j] = arr[mid + <span class="hljs-number">1</span> + j];
    }
    L[sa1] = INT_MAX;
    R[sa2] = INT_MAX;
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>, j = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> k = low; k &lt;= hi; k++)
    {
        <span class="hljs-keyword">if</span> (L[i] &lt;= R[j])
            arr[k] = L[i++];
        <span class="hljs-keyword">else</span>
            arr[k] = R[j++];
    }
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mergeSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> low, <span class="hljs-keyword">int</span> hi)</span>
</span>{
    <span class="hljs-keyword">if</span> (low &lt; hi)
    {
        <span class="hljs-keyword">int</span> mid = (low + hi) / <span class="hljs-number">2</span>;
        mergeSort(low, mid);
        mergeSort(mid + <span class="hljs-number">1</span>, hi);
        merge(low, mid, hi);
    }
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">int</span> size = <span class="hljs-keyword">sizeof</span>(arr) / <span class="hljs-keyword">sizeof</span>(arr[<span class="hljs-number">0</span>]);
    mergeSort(<span class="hljs-number">0</span>, size);
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size; i++)
    {
        <span class="hljs-built_in">cout</span> &lt;&lt; arr[i] &lt;&lt; <span class="hljs-string">" "</span>;
    }
}
</code></pre>
<p><strong>N.B:</strong> Interesting sorting techniques are coming soon in this article.</p>
]]></content:encoded></item><item><title><![CDATA[Finding the shortest path with topological sort in Directed Acyclic Graph]]></title><description><![CDATA[Introduction
In a directed acyclic graph (DAG), a topological sort is a linear ordering of the vertices such that for every directed edge, the vertex u comes before v in the ordering. This ordering can be used to find the shortest path between two ve...]]></description><link>https://blog.khandokeranan.com/finding-the-shortest-path-with-topological-sort-in-directed-acyclic-graph</link><guid isPermaLink="true">https://blog.khandokeranan.com/finding-the-shortest-path-with-topological-sort-in-directed-acyclic-graph</guid><category><![CDATA[DFS]]></category><category><![CDATA[Graph]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[ShortestPath]]></category><category><![CDATA[Topological Sort]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Sat, 06 May 2023 11:56:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683369743364/47602509-49d5-4051-9973-63aa5aa2906c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>In a directed acyclic graph (DAG), a topological sort is a linear ordering of the vertices such that for every directed edge, the vertex <code>u</code> comes before <code>v</code> in the ordering. This ordering can be used to find the shortest path between two vertices in the DAG.</p>
<p>To find the shortest path between two vertices <code>s</code> and <code>t</code>, We can perform a modified version of Dijkstra's algorithm in a Directed Acyclic Graph with the help of topological sort. First, we initialize the distance to <code>s</code> as 0 and the distance to all other vertices as infinity. Then, we perform a topological sort of the graph and visit each vertex in the sorted order.</p>
<p>For each visited vertex <code>u</code>, we examine each of its outgoing edges <code>(u, v)</code>. If the distance to <code>u</code> plus the weight of <code>(u, v)</code> is less than the current distance to <code>v</code>, we update the distance to <code>v</code>. At the end of the algorithm, the distance to <code>t</code> will give us the shortest path from <code>s</code> to <code>t</code>.</p>
<p>This algorithm works because the topological sort ensures that we always visit vertices in the correct order. Specifically, if we visit <code>u</code> before <code>v</code>, we know that there can be no path from <code>v</code> to <code>u</code>, so we can safely update the distance to <code>v</code> based on the distance to <code>u</code>.</p>
<p>Overall, using a topological sort to find the shortest path in a DAG is an efficient way to solve this problem, with a time complexity of <code>O(V + E)</code>, where <code>V</code> is the number of vertices and <code>E</code> is the number of edges in the graph.</p>
<p>C++ code for topological sort where every vertex holds a string:</p>
<pre><code class="lang-cpp"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Graph</span> {</span>
    ll nodes;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>,<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;&gt; adj;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>, <span class="hljs-keyword">bool</span> &gt; visited;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">pair</span>&lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>&gt;, ll &gt; wgt;
    <span class="hljs-keyword">public</span>:
    Graph (ll nodes) {
        <span class="hljs-keyword">this</span>-&gt;nodes = nodes;
    }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addEdge</span> <span class="hljs-params">(<span class="hljs-built_in">string</span> u, <span class="hljs-built_in">string</span> v, ll weight)</span> </span>{
        wgt [<span class="hljs-built_in">make_pair</span>(u,v)] = weight;
        <span class="hljs-keyword">if</span> (v != <span class="hljs-string">""</span>) {
            adj[u].pb(v);
            visited[u] = <span class="hljs-literal">false</span>;
            visited[v] = <span class="hljs-literal">false</span>;
        }<span class="hljs-keyword">else</span> {
            adj[u];
            visited[u] = <span class="hljs-literal">false</span>;
        }
    }
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; tSort;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">tsort</span> <span class="hljs-params">(<span class="hljs-built_in">string</span> u)</span> </span>{
        visited [u] = <span class="hljs-literal">true</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : adj[u]) {
            <span class="hljs-keyword">if</span> (visited[i] == <span class="hljs-literal">false</span>) {
                tsort(i);
            }
        }
        tSort.insert(tSort.begin(), u);
    }
}
</code></pre>
<h3 id="heading-algorithm-for-topological-sort">Algorithm for topological sort</h3>
<pre><code class="lang-bash">0. toposort(u):
1. visited[u] = <span class="hljs-literal">true</span>
2. <span class="hljs-keyword">for</span> all the adjacency vertex i of adj[u]:
3.     <span class="hljs-keyword">if</span> visited [i] is not <span class="hljs-literal">true</span>
4.         toposort(i)
5.     end <span class="hljs-keyword">if</span>
6. end <span class="hljs-keyword">for</span>
5. insert u <span class="hljs-keyword">in</span> a linkedlist <span class="hljs-string">'topolist'</span> from front
</code></pre>
<p>We can efficiently solve the single source shortest path problem using topological sort in a directed acyclic graph. The idea is described above. So, let's start writing the algorithm for it.</p>
<h3 id="heading-algorithm-for-dag-single-source-shortest-path-with-the-help-of-topological-sort">Algorithm for DAG single source shortest path with the help of topological sort</h3>
<pre><code class="lang-bash">0. single_source_shortest_path_dag (<span class="hljs-built_in">source</span>, topolist, weight, adj):
1. <span class="hljs-keyword">for</span> each vertex i <span class="hljs-keyword">in</span> topolist:
2.     distance [i] = INFINITY
3.     predecessor [i] = NULL
4. end <span class="hljs-keyword">for</span>
5. distance[<span class="hljs-built_in">source</span>] = 0
6. <span class="hljs-keyword">for</span> each vertex i <span class="hljs-keyword">in</span> topolist:
7.     <span class="hljs-keyword">for</span> each adjacent vertex j <span class="hljs-keyword">in</span> adj[i]:
8.         <span class="hljs-keyword">if</span> distance[i] + weight[(i,j)] &lt; distance[j]:
9.                distance [j] = distance[i] + weight[(i,j)]
10.               predecessor [j] = i
11.        end <span class="hljs-keyword">if</span>
12.     end <span class="hljs-keyword">for</span>
13. end <span class="hljs-keyword">for</span>

// Now using the distance and predecessor we can measure distance from the single <span class="hljs-built_in">source</span> and track the path from <span class="hljs-built_in">source</span> to destination
</code></pre>
<h3 id="heading-problem-1">Problem 1</h3>
<p>Mr. Bombasta is an alien and is confused about how to dress up like a human being. So he needs to know the order for dressing up. And he has a cost to pick up dresses. Tell him the shortest path from picking up other things if he starts with underwear. Here is the graph of the dresses:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683373233169/0844bd40-7394-4d63-bbef-25536931e40c.png" alt class="image--center mx-auto" /></p>
<p>Think for a while and solve the problem. Though I have attached the solution to this article, you should try to solve the problem yourself.</p>
<p>Solution:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> pb push_back</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> ll;
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> INF 10e9</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Graph</span> {</span>
    ll nodes;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>,<span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt;&gt; adj;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>, <span class="hljs-keyword">bool</span> &gt; visited;
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">pair</span>&lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>&gt;, ll &gt; wgt;
    <span class="hljs-keyword">public</span>:
    Graph (ll nodes) {
        <span class="hljs-keyword">this</span>-&gt;nodes = nodes;
    }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addEdge</span> <span class="hljs-params">(<span class="hljs-built_in">string</span> u, <span class="hljs-built_in">string</span> v, ll weight)</span> </span>{
        wgt [<span class="hljs-built_in">make_pair</span>(u,v)] = weight;
        <span class="hljs-keyword">if</span> (v != <span class="hljs-string">""</span>) {
            adj[u].pb(v);
            visited[u] = <span class="hljs-literal">false</span>;
            visited[v] = <span class="hljs-literal">false</span>;
        }<span class="hljs-keyword">else</span> {
            adj[u];
            visited[u] = <span class="hljs-literal">false</span>;
        }
    }

    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-built_in">string</span>&gt; tSort;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">tsort</span> <span class="hljs-params">(<span class="hljs-built_in">string</span> u)</span> </span>{
        visited [u] = <span class="hljs-literal">true</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : adj[u]) {
            <span class="hljs-keyword">if</span> (visited[i] == <span class="hljs-literal">false</span>) {
                tsort(i);
            }
        }
        tSort.insert(tSort.begin(), u);
    }

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printTopology</span> <span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : visited) {
            i.second = <span class="hljs-literal">false</span>;
        }
        tSort.clear();
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"choose as source by order: "</span>;
        <span class="hljs-keyword">while</span> (tSort.size() &lt; nodes) {
            <span class="hljs-built_in">string</span> source = <span class="hljs-string">""</span>;
            <span class="hljs-keyword">int</span> flag = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : visited) {
                <span class="hljs-keyword">if</span> (i.second == <span class="hljs-literal">false</span>) {
                    source = i.first;
                    flag = <span class="hljs-number">1</span>;
                    <span class="hljs-keyword">break</span>;
                }
            }
            <span class="hljs-keyword">if</span> (flag) {
                <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"'"</span> &lt;&lt; source &lt;&lt; <span class="hljs-string">"' "</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
                tsort(source);
            }<span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">break</span>;
            }
        }
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">endl</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Topological order: "</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
        <span class="hljs-keyword">int</span> c= <span class="hljs-number">1</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i: tSort) {
            <span class="hljs-built_in">cout</span> &lt;&lt; c &lt;&lt;<span class="hljs-string">". "</span> &lt;&lt; i &lt;&lt; <span class="hljs-built_in">endl</span>;
            c++;
        }

        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i: visited) {
            i.second = <span class="hljs-literal">false</span>;
        }
    }
    <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">pair</span> &lt;ll, <span class="hljs-built_in">string</span>&gt; &gt; single_source_shortest_path_dag(<span class="hljs-built_in">string</span> s) {
        printTopology();
        <span class="hljs-built_in">map</span> &lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">pair</span>&lt;ll, <span class="hljs-built_in">string</span>&gt;&gt; res;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : tSort ) {
            res[i] = <span class="hljs-built_in">make_pair</span>(INF, <span class="hljs-string">"NULL"</span>);
        }
        res[s] = <span class="hljs-built_in">make_pair</span>(<span class="hljs-number">0</span>, <span class="hljs-string">"NULL"</span>);
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i: tSort) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> j : adj[i]) {
                <span class="hljs-keyword">if</span> (res[i].first + wgt [<span class="hljs-built_in">make_pair</span>(i,j)] &lt; res[j].first) {
                    res[j].first = res[i].first + wgt [<span class="hljs-built_in">make_pair</span>(i,j)];
                    res[j].second = i;
                }
            }
        }
        <span class="hljs-keyword">return</span> res;
    }

};


<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">solve</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-function">Graph <span class="hljs-title">g</span><span class="hljs-params">(<span class="hljs-number">8</span>)</span></span>;
    g.addEdge(<span class="hljs-string">"socks"</span>, <span class="hljs-string">"shoes"</span>, <span class="hljs-number">3</span>);
    g.addEdge (<span class="hljs-string">"shoes"</span>, <span class="hljs-string">""</span>, <span class="hljs-number">0</span>);
    g.addEdge(<span class="hljs-string">"underwear"</span>, <span class="hljs-string">"socks"</span>, <span class="hljs-number">2</span>);
    g.addEdge(<span class="hljs-string">"underwear"</span>, <span class="hljs-string">"pants"</span>, <span class="hljs-number">6</span>);
    g.addEdge(<span class="hljs-string">"pants"</span>, <span class="hljs-string">"belt"</span>, <span class="hljs-number">5</span>);
    g.addEdge(<span class="hljs-string">"pants"</span>, <span class="hljs-string">"shoes"</span>, <span class="hljs-number">4</span>);
    g.addEdge(<span class="hljs-string">"shirt"</span>, <span class="hljs-string">"belt"</span>, <span class="hljs-number">7</span>);
    g.addEdge(<span class="hljs-string">"shirt"</span>, <span class="hljs-string">"tie"</span>, <span class="hljs-number">8</span>);
    g.addEdge(<span class="hljs-string">"tie"</span>, <span class="hljs-string">"jacket"</span>, <span class="hljs-number">9</span>);
    g.addEdge(<span class="hljs-string">"jacket"</span>, <span class="hljs-string">""</span>, <span class="hljs-number">0</span>);
    g.addEdge(<span class="hljs-string">"socks"</span>, <span class="hljs-string">"shoes"</span>, <span class="hljs-number">3</span>);

    <span class="hljs-keyword">auto</span> ans = g.single_source_shortest_path_dag(<span class="hljs-string">"underwear"</span>);
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"shortest path list: "</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : ans) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i.first &lt;&lt; <span class="hljs-string">": "</span> &lt;&lt; i.second.first &lt;&lt; <span class="hljs-string">", "</span> &lt;&lt; i.second.second &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">endl</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"shortest path from each destination: "</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : ans)  {
        <span class="hljs-built_in">cout</span> &lt;&lt; i.first &lt;&lt; <span class="hljs-string">"("</span> &lt;&lt; i.second.first &lt;&lt; <span class="hljs-string">"): "</span>;
        <span class="hljs-built_in">stack</span> &lt;<span class="hljs-built_in">pair</span>&lt;<span class="hljs-built_in">string</span>, ll&gt;&gt; st;
        <span class="hljs-keyword">if</span> (i.second.first == INF) {
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"N/A"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
            <span class="hljs-keyword">continue</span>;
        } 
        <span class="hljs-keyword">if</span> (i.second.first == <span class="hljs-number">0</span>) {
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"No route needed"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
            <span class="hljs-keyword">continue</span>;
        }
        <span class="hljs-built_in">string</span> route = i.second.second;
        <span class="hljs-keyword">while</span> (ans[route].second != <span class="hljs-string">"NULL"</span>) {
            st.push(<span class="hljs-built_in">make_pair</span>(route, ans[route].first));
            route = ans[route].second;
        }
        st.push(<span class="hljs-built_in">make_pair</span>(route, ans[route].first));
        <span class="hljs-keyword">while</span> (!st.empty()) {
            <span class="hljs-built_in">cout</span> &lt;&lt; st.top().first &lt;&lt; <span class="hljs-string">"("</span> &lt;&lt; st.top().second&lt;&lt; <span class="hljs-string">") -&gt; "</span>;
            st.pop();
        }
        <span class="hljs-built_in">cout</span> &lt;&lt; i.first &lt;&lt; <span class="hljs-string">"("</span> &lt;&lt; i.second.first &lt;&lt; <span class="hljs-string">")"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    }

}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span> </span>{
    solve ();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<h3 id="heading-problem-2">Problem 2</h3>
<p><a target="_blank" href="http://www.spoj.com/problems/TOPOSORT/">http://www.spoj.com/problems/TOPOSORT/</a></p>
<h3 id="heading-problem-3">Problem 3</h3>
<p><a target="_blank" href="https://onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=60">https://onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=60</a></p>
<h3 id="heading-problem-4">Problem 4</h3>
<p><a target="_blank" href="https://onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=136">UVA 200 - Rare Order [difficulty: easy]</a></p>
<h3 id="heading-problem-5">Problem 5</h3>
<p><a target="_blank" href="https://cses.fi/problemset/task/1679">https://cses.fi/problemset/task/1679</a></p>
]]></content:encoded></item><item><title><![CDATA[Various Methods of Finding the Root of an Equation Using Numerical Method]]></title><description><![CDATA[Linear functions are trivial to solve, as are quadratic functions if you memorize the quadratic formula. However, polynomials of higher degrees and non-polynomial functions are much more difficult to solve. The simplest technique for solving these ty...]]></description><link>https://blog.khandokeranan.com/numerical-method-program-1</link><guid isPermaLink="true">https://blog.khandokeranan.com/numerical-method-program-1</guid><category><![CDATA[numerical computing]]></category><category><![CDATA[#LinearAlgebra #Mathematics #DataScience #MachineLearning #ArtificialIntelligence #Statistics #Math #Matrix #Vector #Eigenvalues #Eigenvectors #LinearEquations #LinearSystems #MathematicalFoundations #Algebra #NumericalMethods]]></category><category><![CDATA[Mathematics]]></category><category><![CDATA[bisection method]]></category><category><![CDATA[newton-raphson]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Fri, 05 May 2023 10:39:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683174964692/dd2de987-fc22-4602-99df-d8fff8e43ccc.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Linear functions are trivial to solve, as are quadratic functions if you memorize the quadratic formula. However, polynomials of higher degrees and non-polynomial functions are much more difficult to solve. The simplest technique for solving these types of equations is to use an <strong>iterative root-finding technique</strong>.</p>
<p>We will try out the following techniques using the function:</p>
<p>$$f(x) = x^3 -x -1$$</p><p>Graph of the equation for further outcome validation:</p>
<iframe src="https://www.desmos.com/calculator/wprb274obd?embed" width="300" height="300" style="border:1px solid #ccc"></iframe>

<p>There are several methods to find the root of the polynomial equations. They are-</p>
<ol>
<li><p>Bisection method,</p>
</li>
<li><p>False position method,</p>
</li>
<li><p>Newton-Raphson method,</p>
</li>
<li><p>Secant method and</p>
</li>
<li><p>Newton's Method</p>
</li>
</ol>
<h3 id="heading-bisection-method"><strong>Bisection Method</strong></h3>
<p>The bisection method is the simplest root-finding technique. The algorithm for bisection is similar to binary search. To use this method, we need to know about the <strong>intermediate value theorem</strong> (if f is a continuous function whose domain contains the interval [a, b], then it takes on any given value between f(a) and f(b) at some point within the interval). One of the outcomes of this theorem is "If a continuous function has values of opposite sign inside an interval, then it has a <a target="_blank" href="https://en.wikipedia.org/wiki/Zero_of_a_function">root</a> in that interval" (<strong>Bolzano's theorem</strong>)</p>
<p>Algorithms for Bisection Method:</p>
<pre><code class="lang-bash">1. start
2. Define <span class="hljs-keyword">function</span> f(x)
3. Choose initial guesses <span class="hljs-string">'x0'</span> and <span class="hljs-string">'x1'</span> such that f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x1'</span>) &lt; 0
4. Choose pre-specified tolerable error <span class="hljs-string">'e'</span>.
5. Calculate new approximated root as <span class="hljs-string">'x2'</span> = (<span class="hljs-string">'x0'</span> + <span class="hljs-string">'x1'</span>)/2
6. Calculate f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>)
    a. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) &lt; 0 <span class="hljs-keyword">then</span> <span class="hljs-string">'x1'</span> = <span class="hljs-string">'x2'</span>
    b. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) &gt; 0 <span class="hljs-keyword">then</span> <span class="hljs-string">'x0'</span> = <span class="hljs-string">'x2'</span>
    c. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) = 0 <span class="hljs-keyword">then</span> goto (8)
7. <span class="hljs-keyword">if</span> |f(<span class="hljs-string">'x2'</span>)| &gt; <span class="hljs-string">'e'</span> <span class="hljs-keyword">then</span> goto (5) otherwise goto (8)
8. Display <span class="hljs-string">'x2'</span> as root.
9. Stop
</code></pre>
<p>Code for Bisection Method in C++:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> pb push_back</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">F</span> <span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
    <span class="hljs-comment">// define the polynomial function here</span>
    <span class="hljs-keyword">return</span> x*x*x -x - <span class="hljs-number">1</span>;
}

<span class="hljs-comment">// Showing Each Iteration Function</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">draw</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt;&amp; v)</span> </span>{
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"id\t\tx1\t\tx2\t\tx3\n"</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; v.size()/<span class="hljs-number">3</span>; i++) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i+<span class="hljs-number">1</span> &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[<span class="hljs-number">3</span>*i] &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[<span class="hljs-number">3</span>*i+<span class="hljs-number">1</span>] &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt; v[<span class="hljs-number">3</span>*i+<span class="hljs-number">2</span>] &lt;&lt; <span class="hljs-string">'\n'</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">solve</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">double</span> x1,x2;
    <span class="hljs-built_in">cin</span> &gt;&gt; x1 &gt;&gt; x2;
    <span class="hljs-keyword">if</span> (F(x1) * F(x2) &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"No solution\n"</span>;
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">double</span> E = <span class="hljs-number">0.0001</span>;
    <span class="hljs-keyword">double</span> x3 = (x1+x2)/<span class="hljs-number">2</span>;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt; v;
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">abs</span>(F(x3)) &gt; E) {
        x3 = (x1+x2)/<span class="hljs-number">2</span>;
        <span class="hljs-keyword">if</span> (F(x1) * F(x3) &lt; <span class="hljs-number">0</span>) x2 = x3;
        <span class="hljs-keyword">else</span> x1 = x3;
        v.pb(x1);
        v.pb(x2);
        v.pb(x3);
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; x3 &lt;&lt; <span class="hljs-string">'\n'</span>;
    draw(v);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span> </span>{
    solve();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>The output of the program:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683176825442/5a984c6b-79c0-4210-a807-1d3f9c5b1019.png" alt class="image--center mx-auto" /></p>
<h4 id="heading-convergence">Convergence</h4>
<p>The bisection method has linear convergence with a constant of 1/2.</p>
<h4 id="heading-drawbacks">Drawbacks</h4>
<p>The bisection method cannot solve for the root of x<strong>²</strong>, as it never becomes negative.</p>
<h3 id="heading-false-position-method"><strong>False Position Method</strong></h3>
<p>The False position method works on the principle of the x-interception (C,0) of the straight line between two (x1, f(x1) ) and (x2, f(x2) ) points.</p>
<p>We know the the slope of straight line A { (x1, f(x1) ), (x2, f(x2) ) } and straight line B { ( (x1, f(x1) ), (C, f(C) ) or (x2, f(x2)) , (C, f(C) ) ) } is same.</p>
<p>$$\dfrac{f(x2)-f(x1)}{x2-x1} = \dfrac{0-f(x1)}{C-x1}$$</p><p>$$\therefore C = x1 - \dfrac{(x2-x1)*f(x1)}{f(x2)-f(x1)}$$</p><p>Visual representation of moving to the closer of the root of the equation by one of the iterations:</p>
<iframe src="https://www.desmos.com/calculator/mxgiy75mrw?embed" width="300" height="300" style="border:1px solid #ccc"></iframe>

<p>The working procedure is the same as the bisection method except for the approximate root formula.</p>
<p>Algorithm for False Position method:</p>
<pre><code class="lang-bash">1. start
2. Define <span class="hljs-keyword">function</span> f(x)
3. Choose initial guesses <span class="hljs-string">'x0'</span> and <span class="hljs-string">'x1'</span> such that f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x1'</span>) &lt; 0
4. Choose pre-specified tolerable error <span class="hljs-string">'e'</span>.
5. Calculate new approximated root as <span class="hljs-string">'x2'</span> = = <span class="hljs-string">'x0'</span>- (f(<span class="hljs-string">'x0'</span>) (<span class="hljs-string">'x1'</span>-<span class="hljs-string">'x0'</span>) ) / (f (<span class="hljs-string">'x1'</span>) – f (<span class="hljs-string">'x0'</span>))
6. Calculate f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>)
    a. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) &lt; 0 <span class="hljs-keyword">then</span> <span class="hljs-string">'x1'</span> = <span class="hljs-string">'x2'</span>
    b. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) &gt; 0 <span class="hljs-keyword">then</span> <span class="hljs-string">'x0'</span> = <span class="hljs-string">'x2'</span>
    c. <span class="hljs-keyword">if</span> f(<span class="hljs-string">'x0'</span>)f(<span class="hljs-string">'x2'</span>) = 0 <span class="hljs-keyword">then</span> goto (8)
7. <span class="hljs-keyword">if</span> |f(<span class="hljs-string">'x2'</span>)| &gt; <span class="hljs-string">'e'</span> <span class="hljs-keyword">then</span> goto (5) otherwise goto (8)
8. Display <span class="hljs-string">'x2'</span> as root.
9. Stop
</code></pre>
<p>Code for false position method in C++:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> pb push_back</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">F</span> <span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
    <span class="hljs-comment">// define the polynomial function here</span>
    <span class="hljs-keyword">return</span> x*x*x -x - <span class="hljs-number">1</span>;
}

<span class="hljs-comment">// Showing Each Iteration Function</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">draw</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt;&amp; v)</span> </span>{
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"id\t\tx1\t\tx2\t\tx3\n"</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; v.size()/<span class="hljs-number">3</span>; i++) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i+<span class="hljs-number">1</span> &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[<span class="hljs-number">3</span>*i] &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[<span class="hljs-number">3</span>*i+<span class="hljs-number">1</span>] &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt; v[<span class="hljs-number">3</span>*i+<span class="hljs-number">2</span>] &lt;&lt; <span class="hljs-string">'\n'</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">solve</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">double</span> x1,x2;
    <span class="hljs-built_in">cin</span> &gt;&gt; x1 &gt;&gt; x2;
    <span class="hljs-keyword">if</span> (F(x1) * F(x2) &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"No solution\n"</span>;
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">double</span> E = <span class="hljs-number">0.0001</span>;
    <span class="hljs-keyword">double</span> x3 = (x1+x2)/<span class="hljs-number">2</span>;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt; v;
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">abs</span>(F(x3)) &gt; E) {
        x3 = x1- (F(x1) * (x2-x1) ) / (F (x2) - F (x1));
        <span class="hljs-keyword">if</span> (F(x1) * F(x3) &lt; <span class="hljs-number">0</span>) x2 = x3;
        <span class="hljs-keyword">else</span> x1 = x3;
        v.pb(x1);
        v.pb(x2);
        v.pb(x3);
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; x3 &lt;&lt; <span class="hljs-string">'\n'</span>;
    draw(v);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span> </span>{
    solve();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>The output of the program:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683268901771/dfee6e8e-0a1e-4813-9797-e0e13424ec90.png" alt class="image--center mx-auto" /></p>
<p><strong>Convergence:</strong></p>
<p>The rate of convergence may vary widely depending on the function and the initial interval used.</p>
<p><strong>Drawbacks:</strong></p>
<p>Potential for oscillations: In some cases, the false position method can oscillate between two endpoints without converging to the root. This can happen when the function has a steep gradient near the root.</p>
<h3 id="heading-the-newton-raphson-method">The Newton-Raphson Method</h3>
<p>Newton-Raphson's method is based on the working principle of function derivatives because it updates the approximate root variable with the y-axis-intercept value of the tangent of the equation that touches the previous approximate root value.</p>
<p>The visual representation of Newton-Raphson's Method:</p>
<iframe src="https://www.desmos.com/calculator/ka8c5bgwfs?embed" width="300" height="300" style="border:1px solid #ccc"></iframe>

<p>To use this method, we require an initial guess and the 1st derivatives of the function so that we can form a tangent line and update the approximate value closer to the root.</p>
<p>$$y - F(X_{n-1}) = F'(X_{n-1})* (X_{n}- X_{n-1})$$</p><p>$$\therefore X_{n} = X_{n-1} - \dfrac{F(X_{n-1})}{F'(X_{n-1})}$$</p><p>Algorithm for Newton-Raphson's Method:</p>
<pre><code class="lang-bash">1. Start
2. Define the F(x)
3. Define the derivative of the F1(x)
4. Choose pre-specified tolerable error <span class="hljs-string">'e'</span>.
6. Set initial variable <span class="hljs-string">'x0'</span> via input
6. While <span class="hljs-keyword">function</span>(x0) &gt; <span class="hljs-string">'e'</span>:
7. <span class="hljs-string">'x0'</span> = <span class="hljs-string">'x0'</span> - F(<span class="hljs-string">'x0'</span>)/F1(<span class="hljs-string">'x0'</span>)
8. Display <span class="hljs-string">'x0'</span> as root
9. Stop
</code></pre>
<p>Code for Newton-Raphson's Method in C++:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> pb push_back</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">F</span> <span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
    <span class="hljs-comment">// define the polynomial function here</span>
    <span class="hljs-keyword">return</span> x*x*x -x - <span class="hljs-number">1</span>;
}

<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">F1</span> <span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
    <span class="hljs-comment">// define the derivative of the polynomial function here</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>*x*x - <span class="hljs-number">1</span>;
}

<span class="hljs-comment">// Showing Each Iteration Function</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">draw</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt;&amp; v)</span> </span>{
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"id\t\tXn\n"</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; v.size(); i++) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i+<span class="hljs-number">1</span> &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[i] &lt;&lt; <span class="hljs-string">'\n'</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">solve</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">double</span> x0;
    <span class="hljs-built_in">cin</span> &gt;&gt; x0;
    <span class="hljs-keyword">double</span> E = <span class="hljs-number">0.0001</span>;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt; v;
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">abs</span>(F(x0)) &gt; E) {
        x0 = x0 - F(x0)/F1(x0);
        v.pb(x0);
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; x0 &lt;&lt; <span class="hljs-string">'\n'</span>;
    draw(v);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span> </span>{
    solve();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>The output of the program:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683274726288/a0bd24b6-7f82-4ee8-9e38-13d862808c1b.png" alt class="image--center mx-auto" /></p>
<p><strong>Convergence:</strong></p>
<p>This rapid convergence makes Newton-Raphson's method one of the most popular algorithms for finding roots of nonlinear equations.</p>
<p><strong>Drawbacks:</strong></p>
<ol>
<li><p><strong>Sensitivity to initial guess:</strong> Newton-Raphson's method requires an initial guess of the root. If the initial guess is not close enough to the actual root, the method may fail to converge, converge to the wrong root, or oscillate between different roots. In practice, finding an appropriate initial guess can be difficult, especially when dealing with complex functions.</p>
</li>
<li><p><strong>Need for derivative information:</strong> Newton-Raphson's method requires the derivative of the function to be evaluated at each iteration. This can be computationally expensive, especially when the function is complex or expensive to evaluate. Additionally, the derivative may not be readily available or may be difficult to compute accurately.</p>
</li>
<li><p><strong>Potential for divergence:</strong> Newton-Raphson's method can diverge if the derivative of the function is close to zero or if it changes sign in the neighborhood of the root. This can happen when the function has singularities or sharp turns near the root.</p>
</li>
<li><p><strong>Not guaranteed to converge:</strong> Newton-Raphson's method is not guaranteed to converge to a root in all cases. For example, if the function has an infinite number of roots or if the initial guess is very far from the root, the method may not converge.</p>
</li>
<li><p><strong>Multiple roots:</strong> If the function has multiple roots that are close together, Newton-Raphson's method may converge to a root that is different from the desired one. This can happen if the initial guess is not sufficiently accurate.</p>
</li>
<li><p><strong>Requires smoothness of the function:</strong> Newton-Raphson's method requires the function to be differentiable and smooth in the neighborhood of the root. If the function has discontinuities, singularities, or sharp turns near the root, the method may fail to converge or converge very slowly.</p>
</li>
</ol>
<h3 id="heading-secant-method">Secant Method</h3>
<p>secant method uses the same technique as Newton-Raphson's Method. But sometimes, it's hard to find the derivative of the function analytically. The Secant method overcomes this problem by using the estimating value of the tangent's slope / derivative function.</p>
<p>$$F'(X_{n-1}) \approx \dfrac{ F(X_{n-1}) - F(X_{n-2})}{X_{n-1}-X_{n-2}}$$</p><p>$$X_{n} = X_{n-1} - \dfrac{F(X_{n-1}) * (X_{n-1} - X_{n-2})}{F(X_{n-1}) - F(X_{n-2})}$$</p><p>$$X_{n} = \dfrac{X_{n-2} * F(X_{n-1}) - X_{n-1} * F(X_{n-2})}{F(X_{n-1}) - F(X_{n-2})}$$</p><p>In short, we can use this formula to compute the result</p>
<p>$$\therefore X_3 = \dfrac{x_1F_2 - x_2F_1}{F_2 - F_1}$$</p><p>Code for secant method in C++:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> pb push_back</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">F</span> <span class="hljs-params">(<span class="hljs-keyword">double</span> x)</span> </span>{
    <span class="hljs-comment">// define the polynomial function here</span>
    <span class="hljs-keyword">return</span> x*x*x -x - <span class="hljs-number">1</span>;
}


<span class="hljs-comment">// Showing Each Iteration Function</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">draw</span><span class="hljs-params">(<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt;&amp; v)</span> </span>{
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"id\t\tXn\n"</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; v.size(); i++) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i+<span class="hljs-number">1</span> &lt;&lt; <span class="hljs-string">"\t\t"</span> &lt;&lt; setprecision(<span class="hljs-number">4</span>) &lt;&lt;  v[i] &lt;&lt; <span class="hljs-string">'\n'</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">solve</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">double</span> x1,x2;
    <span class="hljs-built_in">cin</span> &gt;&gt; x1 &gt;&gt; x2;
    <span class="hljs-keyword">double</span> E = <span class="hljs-number">0.0001</span>;
    <span class="hljs-keyword">double</span> f1, f2;
    f1 = F(x1);
    f2 = F(x2);
    <span class="hljs-keyword">double</span> x3 = x1;
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">double</span>&gt; v;
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">abs</span>(F(x3)) &gt; E) {
        x3 = (x1*f2 - x2*f1)/ (f2-f1);
        x1 = x2;
        x2 = x3;
        f1 = F(x1);
        f2 = F(x2);
        v.pb(x3);
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; x3 &lt;&lt; <span class="hljs-string">'\n'</span>;
    draw(v);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span> </span>{
    solve();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>The output of the program:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683282348854/f15223a7-c8ca-47da-9bed-8928e47879d3.png" alt class="image--center mx-auto" /></p>
<p><strong>Convergence:</strong></p>
<p>This method has slower convergence than Newton-Raphson's Method.</p>
<p><strong>Drawbacks:</strong></p>
<p>Same drawbacks as Newton-Raphson's method, except you don't need to find out the derivative of the function.</p>
<p>N.B: Newton's and Modified Bi-section methods are coming soon on this post.</p>
<p><strong>Practice Questions:</strong></p>
<ol>
<li><p>Write a program on the function f(x) = 2x³ + 3x - 1 to output all the possible real roots of the function with a tolerance of 0.0001.</p>
</li>
<li><p>Write a program on the function f(x) = x³ + 9x - 4 with a tolerance of 0.001 to find the root of the function and decide which methods will perform better.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Flutter Login With Google | Apple | Email / Password with Firebase and FlutterFire CLI]]></title><description><![CDATA[This tutorial or guide demonstrates how to implement a login system in a Flutter application using popular authentication methods like Google Sign-In, Apple Sign-In, and traditional Email/Password authentication. The tutorial also showcases how to le...]]></description><link>https://blog.khandokeranan.com/flutter-login-with-google-apple-email-password-with-firebase-and-flutterfire-cli</link><guid isPermaLink="true">https://blog.khandokeranan.com/flutter-login-with-google-apple-email-password-with-firebase-and-flutterfire-cli</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Firebase]]></category><category><![CDATA[firebaseAuth]]></category><category><![CDATA[signinwithapple]]></category><category><![CDATA[flutterfire]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Wed, 19 Apr 2023 01:02:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1681860784943/e1e842f1-a6f3-4142-a7d2-638575a9bcb1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This tutorial or guide demonstrates how to implement a login system in a Flutter application using popular authentication methods like Google Sign-In, Apple Sign-In, and traditional Email/Password authentication. The tutorial also showcases how to leverage Firebase Authentication and FlutterFire CLI to build the authentication logic, including handling user sign-in and sign-out. This comprehensive guide can be useful for Flutter developers looking to integrate a secure and efficient user authentication mechanism in their applications.</p>
<h3 id="heading-demo">Demo</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtube.com/shorts/TtO8PIuA4o0?feature=share">https://youtube.com/shorts/TtO8PIuA4o0?feature=share</a></div>
<p> </p>
<h3 id="heading-list-of-commands">List of commands</h3>
<ol>
<li><p>Download the <a target="_blank" href="https://firebase.google.com/docs/cli#setup_update_cli">Firebase CLI</a></p>
</li>
<li><p>Open VSCode and create a new flutter project using this command:</p>
<pre><code class="lang-apache"> <span class="hljs-attribute">flutter</span> create --org com.khandokeranan -i swift -a kotlin app_name
</code></pre>
</li>
<li><p>Now run VSCode current folder terminal</p>
<pre><code class="lang-apache"> <span class="hljs-attribute">firebase</span> login
 <span class="hljs-attribute">dart</span> pub global activate flutterfire_cli
 <span class="hljs-attribute">flutterfire</span> configure
</code></pre>
</li>
<li><p>FlutterFire automatically configures the project and extra layers of security for you. Now, add this line if you want to use the google_sign_in system to <code>ios/Runner/Info.plist.</code></p>
<pre><code class="lang-apache"> <span class="hljs-section">&lt;key&gt;</span><span class="hljs-attribute">CFBundleURLTypes</span>&lt;/key&gt;
     <span class="hljs-section">&lt;array&gt;</span>
         <span class="hljs-section">&lt;dict&gt;</span>
             <span class="hljs-section">&lt;key&gt;</span><span class="hljs-attribute">CFBundleTypeRole</span>&lt;/key&gt;
             <span class="hljs-section">&lt;string&gt;</span><span class="hljs-attribute">Editor</span>&lt;/string&gt;
             <span class="hljs-section">&lt;key&gt;</span><span class="hljs-attribute">CFBundleURLSchemes</span>&lt;/key&gt;
             <span class="hljs-section">&lt;array&gt;</span>
                 <span class="hljs-comment"># you will find the code in ios/Runner/GoogleService-Info.plist -&gt; REVERSED_CLIENT_ID</span>
                 <span class="hljs-section">&lt;string&gt;</span><span class="hljs-attribute">com</span>.googleusercontent.apps.<span class="hljs-number">128343086777</span>-lo<span class="hljs-number">0</span>d<span class="hljs-number">43</span>u<span class="hljs-number">57</span>fbc<span class="hljs-number">6</span>irsvp<span class="hljs-number">719</span>ne<span class="hljs-number">025</span>ok<span class="hljs-number">8</span>a<span class="hljs-number">2</span>g&lt;/string&gt;
             <span class="hljs-section">&lt;/array&gt;</span>
         <span class="hljs-section">&lt;/dict&gt;</span>
     <span class="hljs-section">&lt;/array&gt;</span>
</code></pre>
</li>
<li><p>Now enable Google or other sign-in methods. Please visit the Firebase console and enable it.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1681861530332/5b3e7e8c-7476-4996-b4ec-0727773ec216.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>We need to add the SHA-1 and SHA-256 string to the Firebase for Android.<br /> Now right-click on gradlew under the android folder of the project folder and click ‘open in terminal’ and run this command:</p>
<pre><code class="lang-bash"> gradlew signingReport
</code></pre>
</li>
<li><p>Now you can implement login features with these plugins:</p>
<pre><code class="lang-yaml">   <span class="hljs-attr">firebase_core:</span> <span class="hljs-string">^2.9.0</span>
   <span class="hljs-attr">firebase_auth:</span> <span class="hljs-string">^4.4.1</span>
   <span class="hljs-attr">google_sign_in:</span> <span class="hljs-string">^6.1.0</span>
</code></pre>
</li>
</ol>
<h3 id="heading-flutter-dart-codes-for-the-signing-flow">Flutter Dart codes for the signing flow</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:firebase_auth/firebase_auth.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:google_sign_in/google_sign_in.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AuthService</span> </span>{

  signInWithGoogle () <span class="hljs-keyword">async</span> {
    <span class="hljs-keyword">final</span> GoogleSignInAccount? user = <span class="hljs-keyword">await</span> GoogleSignIn().signIn();
    <span class="hljs-keyword">if</span> (user == <span class="hljs-keyword">null</span>) <span class="hljs-keyword">return</span>; 
    <span class="hljs-keyword">final</span> GoogleSignInAuthentication auth = <span class="hljs-keyword">await</span> user.authentication;
    <span class="hljs-keyword">final</span> credential = GoogleAuthProvider.credential(
      idToken: auth.idToken,
      accessToken: auth.accessToken
    );
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> FirebaseAuth.instance.signInWithCredential(credential); 
  }

  <span class="hljs-keyword">void</span> userSignOut() {
    FirebaseAuth.instance.signOut();
  }

  <span class="hljs-keyword">void</span> signUserInWithEmailPass(<span class="hljs-built_in">String</span> email, <span class="hljs-built_in">String</span> pass, BuildContext context) <span class="hljs-keyword">async</span>{
    showDialog(context: context, builder: (context) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">const</span> Center(child: CircularProgressIndicator());
    });
    <span class="hljs-keyword">try</span>{
      <span class="hljs-keyword">await</span> FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: pass);
    } <span class="hljs-keyword">catch</span> (e) {
      Navigator.pop(context);
      ScaffoldMessenger.of(context).showSnackBar(<span class="hljs-keyword">const</span> SnackBar(content: Text(<span class="hljs-string">"Please use the correct username/password or login with Google or Apple id."</span>)));
    }
  }


  <span class="hljs-keyword">void</span> signUserUpWithEmailPass(<span class="hljs-built_in">String</span> email, <span class="hljs-built_in">String</span> pass) <span class="hljs-keyword">async</span> {
      showDialog(context: context, builder: (context) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">const</span> Center(child: CircularProgressIndicator());
      });
      <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">final</span> credential = <span class="hljs-keyword">await</span> FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: email,
        password: pass,
      );
    } <span class="hljs-keyword">on</span> FirebaseAuthException <span class="hljs-keyword">catch</span> (e) {
      <span class="hljs-keyword">if</span> (e.code == <span class="hljs-string">'weak-password'</span>) {
        ScaffoldMessenger.of(context).showSnackBar(<span class="hljs-keyword">const</span> SnackBar(content: Text(<span class="hljs-string">"The password provided is too weak."</span>)));
      } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (e.code == <span class="hljs-string">'email-already-in-use'</span>) {
        ScaffoldMessenger.of(context).showSnackBar(<span class="hljs-keyword">const</span> SnackBar(content: Text(<span class="hljs-string">"The account already exists for that email."</span>)));
      }
    } <span class="hljs-keyword">catch</span> (e) {
      ScaffoldMessenger.of(context).showSnackBar(<span class="hljs-keyword">const</span> SnackBar(content: Text(<span class="hljs-string">"Something went wrong. please try again with proper internet connection and strong password"</span>)));
    }
  }

  <span class="hljs-comment">// Before you begin, configure Sign In with Apple and enable Apple as a sign-in provider.</span>
  <span class="hljs-comment">// Next, make sure that your Runner apps have the "Sign in with Apple" capability.</span>
  <span class="hljs-comment">// Link : https://firebase.google.com/docs/auth/ios/apple#configure-sign-in-with-apple</span>
  <span class="hljs-comment">// Link for enabling apple provider: https://firebase.google.com/docs/auth/ios/apple#enable-apple-as-a-sign-in-provider</span>
  signInWithApple() <span class="hljs-keyword">async</span> {
    <span class="hljs-keyword">final</span> appleProvider = AppleAuthProvider();
    <span class="hljs-keyword">await</span> FirebaseAuth.instance.signInWithProvider(appleProvider);
  }
}
</code></pre>
<h3 id="heading-important-read-this-carefully">Important: Read this carefully</h3>
<p><mark>Sign-in with Apple must be implemented if you are using any social media authentication providers to publish the app in Apple App Store.</mark></p>
<p>To use the sign-in with Apple, you need to subscribe Apple Developer Program (PAID) and go through these links to implement the feature:<br /><a target="_blank" href="https://firebase.google.com/docs/auth/ios/apple#configure-sign-in-with-apple"><em>Configure Sign In with Apple</em></a> <em>and</em> <a target="_blank" href="https://firebase.google.com/docs/auth/ios/apple#enable-apple-as-a-sign-in-provider"><em>enable Apple as a sign-in provider</em></a><em>. Make sure that your</em> <code>Runner</code> <em>apps have the "Sign in with Apple" capability.</em>  </p>
<p><em>For Google sign-in flow, make sure that ios/runner has a file named</em> <code>GoogleService-Info.plist</code> and android/app has a file named <code>google-services.json</code></p>
<p>If you are using Github public repository for the source control system, add these lines in <code>gitignore</code> to prevent using the files by others.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Firebase Credentials</span>
/android/app/google-services.json
/ios/Runner/GoogleService-Info.plist
</code></pre>
<h3 id="heading-github-link">Github link</h3>
<p>GitHub link: <a target="_blank" href="https://github.com/anwholesquare/firebase_google_apple_emailpass_auth_flutter_flutterfire">https://github.com/anwholesquare/firebase_google_apple_emailpass_auth_flutter_flutterfire</a></p>
]]></content:encoded></item><item><title><![CDATA[Flutter Fast Implementation: Forms Building,
and Sign Up Page Widgets]]></title><description><![CDATA[Here is the UI that we want to build ultimately in this blog.

Required Flutter packages
Open pubspec.yaml and add these packages under dependencies:
dependencies:
  flutter:
    sdk: flutter
  # add these packages in your flutter project first
  goo...]]></description><link>https://blog.khandokeranan.com/flutter-fast-implementation-part-2</link><guid isPermaLink="true">https://blog.khandokeranan.com/flutter-fast-implementation-part-2</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Signup page]]></category><category><![CDATA[Mobile Development]]></category><category><![CDATA[Flutter Widgets]]></category><category><![CDATA[Flutter Examples]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Thu, 13 Apr 2023 04:09:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1681356163798/9aeb81e2-6098-4b2a-a4ae-9cfaa727c01a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Here is the UI that we want to build ultimately in this blog.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1681357013086/d6a59615-0afb-4dfe-81da-088f0db29d9a.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-required-flutter-packages">Required Flutter packages</h3>
<p>Open pubspec.yaml and add these packages under dependencies:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">dependencies:</span>
  <span class="hljs-attr">flutter:</span>
    <span class="hljs-attr">sdk:</span> <span class="hljs-string">flutter</span>
  <span class="hljs-comment"># add these packages in your flutter project first</span>
  <span class="hljs-attr">google_fonts:</span>
  <span class="hljs-attr">shared_preferences:</span>
  <span class="hljs-attr">intl:</span>
  <span class="hljs-attr">sign_button:</span>
</code></pre>
<p><strong>Google Fonts</strong> is used for custom fonts. <strong>Shared Preferences</strong>, this package is used for storing small data such as current_theme_mode, is_user_logged_in, etc. <strong>Intl</strong> is widely used to convert DateTime to our desired date string. The <strong>Sign_button</strong> package is used to design social media login buttons with proper design guidelines.</p>
<h3 id="heading-import-necessary-assets">Import Necessary Assets</h3>
<p>To use local assets, we must add this line to the pubspec.yaml: assets:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">flutter:</span>
  <span class="hljs-attr">uses-material-design:</span> <span class="hljs-literal">true</span>  
<span class="hljs-comment"># add this line and create a new folder named assets and inside the folder, create another folder named images</span>
  <span class="hljs-attr">assets:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-string">assets/images/</span>
</code></pre>
<p>I have saved a .gif image named 'boring.gif' inside the folder named 'images.' The link to the file is <a target="_blank" href="https://github.com/anwholesquare/uPromise/blob/2547cfccfb85ae555423d1b0661f18b097b871d7/assets/images/boring.gif">HERE</a>.</p>
<h3 id="heading-import-dart-files-to-maindart">Import dart files to main.dart</h3>
<p>For the project, we need these importations inside the <strong>main.dart</strong> file:</p>
<pre><code class="lang-dart"><span class="hljs-comment">// ignore_for_file: prefer_const_constructors</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:google_fonts/google_fonts.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:intl/intl.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:http/http.dart'</span> <span class="hljs-keyword">as</span> http;
<span class="hljs-keyword">import</span> <span class="hljs-string">'dart:convert'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:sign_button/sign_button.dart'</span>;
</code></pre>
<h3 id="heading-widgets-we-need-to-use">Widgets we need to use</h3>
<p>The list of widgets we will use:</p>
<ol>
<li><p>HomePage</p>
<ol>
<li><p>DropdownButtonFormField</p>
</li>
<li><p>TextFormField</p>
</li>
<li><p>showDatePicker()</p>
</li>
<li><p>ElevatedButton</p>
</li>
<li><p>Others: Container, Column, Material, Scaffold, Center, Row, Icon, Text, BoxDecoration, Navigator.push(), SizedBox, Padding, SingleChildListView, Flexible</p>
</li>
</ol>
</li>
<li><p>ThankYouPage</p>
<ol>
<li><p>Image.asset</p>
</li>
<li><p>Text</p>
</li>
<li><p>Scaffold</p>
</li>
</ol>
</li>
</ol>
<h3 id="heading-full-maindart-code">Full main.dart code</h3>
<pre><code class="lang-dart"><span class="hljs-comment">// ignore_for_file: prefer_const_constructors</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:google_fonts/google_fonts.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:intl/intl.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:http/http.dart'</span> <span class="hljs-keyword">as</span> http;
<span class="hljs-keyword">import</span> <span class="hljs-string">'dart:convert'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:sign_button/sign_button.dart'</span>;

<span class="hljs-keyword">void</span> main () {
    runApp(MyApp());
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
  <span class="hljs-keyword">const</span> MyApp({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  State&lt;MyApp&gt; createState() =&gt; _MyAppState();
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_MyAppState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span>&lt;<span class="hljs-title">MyApp</span>&gt; </span>{
  <span class="hljs-built_in">int</span> appColor = <span class="hljs-number">3</span>;
  <span class="hljs-keyword">void</span> update (<span class="hljs-built_in">int</span> status, <span class="hljs-built_in">dynamic</span> data) {
      <span class="hljs-keyword">if</span> (status == <span class="hljs-number">1</span> &amp;&amp; data <span class="hljs-keyword">is</span> <span class="hljs-built_in">int</span>) {
        appColor = data % <span class="hljs-number">4</span>;
        setState(() {});
      }
  }
  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> MaterialApp(
        theme: ThemeData(
            primarySwatch: appColor == <span class="hljs-number">3</span> ? Colors.blue : appColor == <span class="hljs-number">2</span> ? Colors.green : Colors.red,
            fontFamily: GoogleFonts.poppins().fontFamily
        ),
        debugShowCheckedModeBanner: <span class="hljs-keyword">false</span>,
        home: HomePage(updateMyApp: update)

    );
  }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomePage</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
  <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-built_in">Function</span> (<span class="hljs-built_in">int</span>, <span class="hljs-built_in">dynamic</span>)? updateMyApp;
  <span class="hljs-keyword">const</span> HomePage({<span class="hljs-keyword">super</span>.key, <span class="hljs-keyword">required</span> <span class="hljs-keyword">this</span>.updateMyApp});

  <span class="hljs-meta">@override</span>
  State&lt;HomePage&gt; createState() =&gt; _HomePageState();
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_HomePageState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span>&lt;<span class="hljs-title">HomePage</span>&gt; </span>{
  <span class="hljs-built_in">String</span> dob = DateFormat.yMd().format (<span class="hljs-built_in">DateTime</span>.now());
  <span class="hljs-built_in">List</span>&lt;DropdownMenuItem&lt;<span class="hljs-built_in">String</span>&gt;&gt; countries = [DropdownMenuItem(child: Text(<span class="hljs-string">"No Country"</span>))];
  <span class="hljs-built_in">String?</span> selectedCountry;
  <span class="hljs-built_in">bool</span> _isChecked = <span class="hljs-keyword">true</span>;
  <span class="hljs-built_in">int</span> appColor = <span class="hljs-number">3</span>;

  Widget TextBox (<span class="hljs-built_in">String</span> label, <span class="hljs-built_in">String</span> hint, {<span class="hljs-built_in">bool</span> security = <span class="hljs-keyword">false</span>}) =&gt; TextField(
    obscureText: security,
    decoration: InputDecoration (
      labelText: label,
      hintText: hint,
      prefixIcon:  Icon (security ? Icons.pin : Icons.person),
      border: UnderlineInputBorder()
    )
  );

  <span class="hljs-keyword">void</span> getCountries () <span class="hljs-keyword">async</span> {

    <span class="hljs-keyword">final</span> response = <span class="hljs-keyword">await</span> http.<span class="hljs-keyword">get</span>(<span class="hljs-built_in">Uri</span>.parse(<span class="hljs-string">"https://raw.githubusercontent.com/anwholesquare/form-building-json/main/countries.json"</span>));
    countries = [DropdownMenuItem(child: Text(<span class="hljs-string">"No Country"</span>))];
    <span class="hljs-keyword">if</span> (response.statusCode == <span class="hljs-number">200</span>) {
        <span class="hljs-keyword">var</span> cntr = json.decode(response.body);
        <span class="hljs-keyword">for</span> (<span class="hljs-built_in">dynamic</span> k <span class="hljs-keyword">in</span> cntr) {
            <span class="hljs-built_in">String</span> nText = k[<span class="hljs-string">"text"</span>].toString();
            countries.add(DropdownMenuItem(value: k[<span class="hljs-string">"value"</span>], child: Text(nText)));
        }
        setState(() {

        });
    }
  }

  <span class="hljs-meta">@override</span>
  <span class="hljs-keyword">void</span> initState() {
    <span class="hljs-keyword">super</span>.initState();
    getCountries();
  }

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> Material(
      child:SafeArea(
        child: Padding(
          padding: <span class="hljs-keyword">const</span> EdgeInsets.all(<span class="hljs-number">24</span>),
          child: Container(
              width: <span class="hljs-built_in">double</span>.infinity,
              child: SingleChildScrollView(
                  child: Column (children: [
                      Row (children: [
                        Icon(Icons.arrow_back_ios_new_rounded),
                        SizedBox(height:<span class="hljs-number">20</span>),
                        Text(<span class="hljs-string">"Coursido Login"</span>, style: TextStyle(fontSize: <span class="hljs-number">24</span>, fontWeight: FontWeight.bold) )
                       ],),
                       SizedBox(height: <span class="hljs-number">20</span>,),
                       TextBox(<span class="hljs-string">"Email"</span>, <span class="hljs-string">"Enter Email Address"</span>),
                       SizedBox(height: <span class="hljs-number">20</span>,),
                       Row (children: [
                          Flexible (
                            flex:<span class="hljs-number">4</span>,
                            child: 
                          DropdownButtonFormField&lt;<span class="hljs-built_in">String</span>&gt;(
                              items: countries,
                              isExpanded: <span class="hljs-keyword">true</span>,
                              onChanged: (<span class="hljs-built_in">String?</span> ne) {
                                debugPrint(ne);
                              },
                              decoration: InputDecoration(
                                labelText: <span class="hljs-string">"Country"</span>,
                                prefixIcon: Icon(Icons.public_rounded)
                              ),

                          ),),
                          SizedBox(width: <span class="hljs-number">5</span>,),
                          Flexible(
                            flex:<span class="hljs-number">3</span>,
                            child: TextFormField(
                              onTap:() <span class="hljs-keyword">async</span> {
                                  <span class="hljs-keyword">final</span> <span class="hljs-built_in">DateTime?</span> pickDate = <span class="hljs-keyword">await</span> showDatePicker(context: context, initialDate: <span class="hljs-built_in">DateTime</span>.now(), firstDate: <span class="hljs-built_in">DateTime</span>(<span class="hljs-number">1900</span>), lastDate: <span class="hljs-built_in">DateTime</span>.now());

                                  <span class="hljs-keyword">if</span> (pickDate != <span class="hljs-keyword">null</span>) {
                                      setState(() {
                                        dob = DateFormat.yMd().format(pickDate);
                                      });
                                  }
                              },
                              readOnly: <span class="hljs-keyword">true</span>,
                              controller: TextEditingController(text:dob),
                              decoration: InputDecoration(
                                labelText: <span class="hljs-string">"Date of Birth"</span>,
                                prefixIcon: Icon(Icons.calendar_month_sharp),
                                border: UnderlineInputBorder()
                              )
                            )
                          )

                       ],),
                      SizedBox(height:<span class="hljs-number">20</span>),
                      TextBox(security: <span class="hljs-keyword">true</span>, <span class="hljs-string">"PIN"</span>, <span class="hljs-string">"Enter PIN"</span>),
                      SizedBox(height: <span class="hljs-number">20</span>,),
                      Row (children: [
                        Checkbox(
                          value: _isChecked,
                          onChanged: (val) =&gt; setState(() {
                            _isChecked = val!;
                          }),
                        ),
                        SizedBox(width: <span class="hljs-number">10</span>,),
                        Text(<span class="hljs-string">"I agree with terms and conditions"</span>)


                      ],),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          InkWell(
                            onTap: () =&gt; setState(() {
                              appColor = <span class="hljs-number">1</span>; widget.updateMyApp!(<span class="hljs-number">1</span>, appColor);
                            }),
                            child: Container (
                              height: <span class="hljs-number">32</span>,
                            width: <span class="hljs-number">32</span>,
                            decoration: BoxDecoration(shape:BoxShape.circle, color: Colors.red),
                            child: (appColor == <span class="hljs-number">1</span>) ? Icon(Icons.check_rounded, color: Colors.white,) : <span class="hljs-keyword">null</span>,

                            ),
                          ),
                          SizedBox(width: <span class="hljs-number">10</span>,),
                          InkWell(
                            onTap: () =&gt; setState(() {
                              appColor = <span class="hljs-number">2</span>; widget.updateMyApp!(<span class="hljs-number">1</span>, appColor);
                            }),
                            child: Container (
                              height: <span class="hljs-number">32</span>,
                            width: <span class="hljs-number">32</span>,
                            decoration: BoxDecoration(shape:BoxShape.circle, color: Colors.green),
                            child: (appColor == <span class="hljs-number">2</span>) ? Icon(Icons.check_rounded, color: Colors.white,) : <span class="hljs-keyword">null</span>,

                            ),
                          ),
                          SizedBox(width: <span class="hljs-number">10</span>,),
                          InkWell(
                            onTap: () =&gt; setState(() {
                              appColor = <span class="hljs-number">3</span>; widget.updateMyApp!(<span class="hljs-number">1</span>, appColor);
                            }),
                            child: Container (
                              height: <span class="hljs-number">32</span>,
                            width: <span class="hljs-number">32</span>,
                            decoration: BoxDecoration(shape:BoxShape.circle, color: Colors.blue),
                            child: (appColor == <span class="hljs-number">3</span>) ? Icon(Icons.check_rounded, color: Colors.white,) : <span class="hljs-keyword">null</span>,

                            ),
                          ),


                        ]
                      ),
                      SizedBox(height: <span class="hljs-number">20</span>,),
                      Center (child: ElevatedButton(
                        onPressed: () {
                          Navigator.push(context, MaterialPageRoute(builder: (context) =&gt; ThankYouPage()));
                        },
                        child: Text(<span class="hljs-string">"Create a New Account"</span>)
                      ),),
                      SizedBox(height: <span class="hljs-number">20</span>,),
                      Center(child: Text(<span class="hljs-string">"OR"</span>,style: TextStyle(fontSize: <span class="hljs-number">24</span>, fontWeight: FontWeight.bold),),),
                      SizedBox(height: <span class="hljs-number">20</span>,),
                      Center(
                        child: SignInButton(buttonType: ButtonType.google, buttonSize: ButtonSize.small,
                        onPressed: () {
                          Navigator.push(context, MaterialPageRoute(builder: (context) =&gt; ThankYouPage()));
                        },
                        ),
                      )


                  ],)
              ),
            ),
        )
      )

    );
  }
}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThankYouPage</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
  <span class="hljs-keyword">const</span> ThankYouPage({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> Scaffold(
      appBar: AppBar(title: Text(<span class="hljs-string">"Coursido"</span>)),
      body: Center(child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset(<span class="hljs-string">"assets/images/boring.gif"</span>, height: <span class="hljs-number">300</span>,),
          SizedBox(height: <span class="hljs-number">20</span>,),
          Text(<span class="hljs-string">"Thank you for the registration"</span>, style: TextStyle(fontWeight: FontWeight.bold),)

      ],)),
    );
  }
}
</code></pre>
<h3 id="heading-youtube-fast-flutter-implementation">Youtube Fast Flutter Implementation</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=BshcEvl3pzw">https://www.youtube.com/watch?v=BshcEvl3pzw</a></div>
<p> </p>
<h3 id="heading-project-github-link">Project GitHub link</h3>
<p><a target="_blank" href="https://github.com/anwholesquare/flutter-testdesign/tree/680d8d52a2a71ee1daec099bc5629073e71a62c7">https://github.com/anwholesquare/flutter-testdesign/tree/680d8d52a2a71ee1daec099bc5629073e71a62c7</a></p>
]]></content:encoded></item><item><title><![CDATA[Flutter: A Developer's Guide to Building High-Quality Apps]]></title><description><![CDATA[Flutter is a popular open-source mobile application development framework enabling developers to build beautiful, high-performance, natively compiled mobile applications for Android and iOS platforms using a single codebase. In this blog, we will gui...]]></description><link>https://blog.khandokeranan.com/flutter-part-1</link><guid isPermaLink="true">https://blog.khandokeranan.com/flutter-part-1</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Mobile Development]]></category><category><![CDATA[app development]]></category><category><![CDATA[mobile application design]]></category><category><![CDATA[Cross Platform App Development. ]]></category><dc:creator><![CDATA[khandoker anan]]></dc:creator><pubDate>Fri, 07 Apr 2023 01:39:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1680829998904/cdd3f35e-f0b0-431f-869e-35436715d53d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Flutter is a popular open-source mobile application development framework enabling developers to build beautiful, high-performance, natively compiled mobile applications for Android and iOS platforms using a single codebase. In this blog, we will guide you through the process of learning Flutter development from scratch.</p>
<h3 id="heading-prerequisites">Prerequisites:</h3>
<ol>
<li><p>Before you can start building Flutter apps, you must set up your development environment. This includes installing the <strong>Flutter SDK, Android Studio, and VS Code, XCode, and Cocoapods</strong> you might need. If you haven't already installed Flutter, follow the instructions on the official Flutter website to download and install it on your computer.</p>
</li>
<li><p>Flutter uses Dart as its programming language. Dart is a modern, object-oriented programming language with a syntax that is easy to learn and understand. You should spend some time learning Dart programming, including its <strong>syntax</strong>, <strong>data types</strong>, <strong>variables</strong>, <strong>functions</strong>, and <strong>control structures</strong>.</p>
</li>
</ol>
<h3 id="heading-dart-basics">Dart Basics:</h3>
<p>Here's a quick reference of important things to keep in mind when using Dart for Flutter:</p>
<p><strong>Data Structures and Variables:</strong></p>
<pre><code class="lang-dart"><span class="hljs-comment">// Importing core libraries</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'dart:math'</span>;
<span class="hljs-comment">// Importing libraries from external packages</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:test/test.dart'</span>;
<span class="hljs-comment">// Importing files</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'path/to/my_other_file.dart'</span>;

<span class="hljs-keyword">void</span> main(){
    <span class="hljs-comment">// This is a normal, one-line comment.</span>

    <span class="hljs-comment">/// <span class="markdown">This is a documentation comment, used to document libraries,</span></span>
    <span class="hljs-comment">/// <span class="markdown">classes, and their members. Tools like IDEs and dartdoc treat</span></span>
    <span class="hljs-comment">/// <span class="markdown">doc comments specially.</span></span>

    <span class="hljs-built_in">int</span> x = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">var</span> p = <span class="hljs-number">5</span>;
    <span class="hljs-built_in">dynamic</span> z = <span class="hljs-number">8</span>;
    z = <span class="hljs-string">"cool"</span>;
    <span class="hljs-keyword">final</span> email = <span class="hljs-string">"temid@gmail.com"</span>; 
    <span class="hljs-keyword">const</span> qty = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">var</span> multilist = [];
    multilist.add([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>]);
    multilist.add([<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span>,<span class="hljs-number">9</span>]);
    <span class="hljs-keyword">for</span> (<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; i <span class="hljs-keyword">in</span> multilist) {
        <span class="hljs-built_in">print</span> (<span class="hljs-string">"list: <span class="hljs-subst">$i</span>"</span>);
        i.sort((x,y) {
            <span class="hljs-keyword">return</span> x&gt;y ? x : y;
        });
        <span class="hljs-built_in">print</span>(<span class="hljs-string">"sorted list: <span class="hljs-subst">${i.toString()}</span>"</span>);
    }


   <span class="hljs-keyword">var</span> person = {};
   person[<span class="hljs-string">"name"</span>] = <span class="hljs-string">"Khandoker Kafi Anan"</span>;
   person[<span class="hljs-string">"age"</span>] = <span class="hljs-number">22</span>;
   person[<span class="hljs-string">"socials"</span>] = {
     <span class="hljs-string">"facebook"</span>: <span class="hljs-string">"https://facebook.com/khandoker.anan"</span>,
     <span class="hljs-string">"website"</span> : <span class="hljs-string">"https://khandokeranan.com"</span>,
     <span class="hljs-string">"linkedin"</span>: <span class="hljs-string">"httpsL//linkedin.com/in/khandokeranan"</span>
   };
   person[<span class="hljs-string">"favorite_numbers"</span>] = [<span class="hljs-number">6</span>,<span class="hljs-number">5</span>,<span class="hljs-number">11</span>,<span class="hljs-number">28</span>,<span class="hljs-number">101</span>,<span class="hljs-number">7</span>,<span class="hljs-number">3</span>];

   person.forEach( (key,val) {
     <span class="hljs-built_in">print</span> (<span class="hljs-string">"<span class="hljs-subst">$key</span> : <span class="hljs-subst">$val</span>"</span>);
   });

}
</code></pre>
<p><strong>Class, Inheritance, mixin, and Polymorphism:</strong></p>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Analytics</span> </span>{
    <span class="hljs-built_in">int</span> totalViewsCount = <span class="hljs-number">0</span>;
    <span class="hljs-built_in">int</span> blogId = <span class="hljs-number">0</span>;
    Analytics (<span class="hljs-built_in">int</span> blogId, <span class="hljs-built_in">int</span> totalViewsCount) {
      <span class="hljs-keyword">this</span>.blogId = blogId;
      <span class="hljs-keyword">this</span>.totalViewsCount = totalViewsCount;
    }
    <span class="hljs-keyword">void</span> increaseView () {
      totalViewsCount++;
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Audio</span> </span>{
    <span class="hljs-keyword">void</span> playSound (<span class="hljs-built_in">String</span> filename) {
        <span class="hljs-built_in">print</span> (<span class="hljs-string">"Playing <span class="hljs-subst">$filename</span>"</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BlogWriter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Analytics</span> <span class="hljs-title">with</span> <span class="hljs-title">Audio</span></span>{
    <span class="hljs-built_in">String?</span> title, description, author;
    BlogWriter( <span class="hljs-keyword">super</span>.blogId, <span class="hljs-keyword">super</span>.totalViewsCount, { <span class="hljs-keyword">required</span> <span class="hljs-keyword">this</span>.title, <span class="hljs-keyword">this</span>.description, <span class="hljs-keyword">required</span> <span class="hljs-keyword">this</span>.author});

    <span class="hljs-keyword">void</span> showDetails () {
        description ??= <span class="hljs-string">"No description found"</span>;
        <span class="hljs-built_in">print</span> (<span class="hljs-string">"Title: <span class="hljs-subst">$title</span>\nDescription: <span class="hljs-subst">$description</span>\nAuthor: <span class="hljs-subst">$author</span>\nBlog ID: <span class="hljs-subst">$blogId</span>\nTotal Views: <span class="hljs-subst">$totalViewsCount</span>"</span>);
    }

    <span class="hljs-meta">@override</span> 
    <span class="hljs-keyword">void</span> increaseView () {
      totalViewsCount+=<span class="hljs-number">2</span>;
    }
}

<span class="hljs-keyword">void</span> main () {

    BlogWriter bw = BlogWriter(<span class="hljs-number">11</span>, <span class="hljs-number">2011</span>, title: <span class="hljs-string">"Anan's Sight"</span>, description: <span class="hljs-string">"This is a blog"</span>, author: <span class="hljs-string">"Khandoker Anan"</span>);
    bw.increaseView ();
    bw.showDetails();
    bw.playSound(<span class="hljs-string">"C:/app/anan.mp3"</span>);

}
</code></pre>
<p><strong>Optional Function Passing in a Function:</strong></p>
<pre><code class="lang-dart"><span class="hljs-built_in">int</span> maxVal (<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; v) {
    <span class="hljs-built_in">int</span> maxv = <span class="hljs-number">-999999999999</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-built_in">int</span> i=<span class="hljs-number">0</span>; i&lt;v.length; i++) {
        maxv = (maxv &lt; v[i]) ? v[i] : maxv;
    }
    <span class="hljs-keyword">return</span> maxv;
}
<span class="hljs-keyword">void</span> deleteMaxVal(<span class="hljs-keyword">var</span> v, {<span class="hljs-built_in">Function</span> max = maxVal}) {
  <span class="hljs-keyword">var</span> maxValue = max(v);
  <span class="hljs-built_in">bool</span> check = v <span class="hljs-keyword">is</span> <span class="hljs-built_in">List</span> &amp;&amp; v.isNotEmpty &amp;&amp; maxValue.runtimeType.toString() == v[<span class="hljs-number">0</span>].runtimeType.toString();
  <span class="hljs-keyword">if</span> (check) {
    v.remove(maxValue); 
    <span class="hljs-built_in">print</span> (v);
  }

}
<span class="hljs-keyword">void</span> main () {
  <span class="hljs-keyword">var</span> v = [<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">11</span>,<span class="hljs-number">33</span>,<span class="hljs-number">55</span>,<span class="hljs-number">1</span>,<span class="hljs-number">21</span>];
  <span class="hljs-built_in">print</span> (maxVal (v));
  deleteMaxVal(v, max: maxVal);
}
</code></pre>
<h3 id="heading-flutter-project-creation-and-source-control">Flutter Project Creation and Source Control:</h3>
<p><strong>HOW TO CREATE A FLUTTER PROJECT</strong></p>
<p>To create a flutter project, we need to run this command on the specific folder's terminal/CMD:</p>
<pre><code class="lang-apache"><span class="hljs-attribute">flutter</span> create --org com.khandokeranan -i swift -a kotlin testdesign
</code></pre>
<p>"com.khandokeranan" represents the organization's name and type, and "-i swift" uses for using swift for iOS development, and "-a kotlin" uses for using kotlin for android development.</p>
<p><strong>FLUTTER DEFAULT PROJECT STRUCTURE</strong></p>
<p>using the <code>flutter create</code> command, it generates a default project structure with several directories and files. Here's an overview of the main directories and files in a typical Flutter project:</p>
<ol>
<li><p><code>android</code>: This directory contains the Android-specific configuration files, such as the Gradle build scripts, AndroidManifest.xml, and other resources required for building and running the Flutter app on Android.</p>
</li>
<li><p><code>ios</code>: This directory contains the iOS-specific configuration files, such as the Xcode project, Info.plist, and other resources required for building and running the Flutter app on iOS.</p>
</li>
<li><p><code>lib</code>: This directory is the heart of your Flutter app, and it contains the Dart source code for your app. It's where we'll write our app's main logic and UI code using Flutter widgets.</p>
</li>
<li><p><code>test</code>: This directory is used for writing tests for your Flutter app. It can contain unit tests, integration tests, and widget tests to ensure the quality and correctness of your app.</p>
</li>
<li><p><code>assets</code>: This directory is used for storing static assets, such as images, fonts, and other files, that are bundled with your app. You can access these assets in your Flutter app using the <code>AssetBundle</code> class.</p>
</li>
<li><p><code>lib/main.dart</code>: This is the entry point of your Flutter app. It contains the <code>main()</code> function, which is the starting point of your app's execution.</p>
</li>
<li><p><code>pubspec.yaml</code>: This is the configuration file for our Flutter app, written in YAML format. It contains your app's dependencies, assets, and other settings, such as the app name, version, and description.</p>
</li>
<li><p><code>pubspec.lock</code>: Flutter automatically generates this file and contains the locked versions of the dependencies used in your app, ensuring that the same dependencies are used across different builds.</p>
</li>
<li><p><code>build</code>: This directory is automatically generated by Flutter, and it contains the compiled output of your app, including the APK (Android) and IPA (iOS) files, as well as intermediate build artifacts.</p>
</li>
</ol>
<p><strong>ACTIVATING GITHUB REPOSITORY WITH THE PROJECT INSIDE VS CODE</strong></p>
<p>Make sure <strong>Git</strong> is installed. VS Code will use your machine's Git installation (at least version <code>2.0.0</code>), so you need to <a target="_blank" href="https://git-scm.com/download">install Git</a> first before you get these features to publish the project directly to GitHub. By following these two steps to publish the repository to a public or private remote repository to GitHub without using commands, we can commit to the project, open new branches for production, development, android-specific, etc, manage branches and commits and collaborate with other developers.</p>
<p><a target="_blank" href="https://www.c-sharpcorner.com/article/manage-github-repository-using-visual-studio-code/"><img src="https://f4n3x6c5.stackpathcdn.com/article/manage-github-repository-using-visual-studio-code/Images/pc4.png" alt class="image--center mx-auto" /></a></p>
<p>To know more about it, click the image for more references.</p>
<h3 id="heading-the-first-hello-world-flutter-app-from-scratch"><strong>The First Hello World Flutter App from Scratch:</strong></h3>
<p>The git repo: <a target="_blank" href="https://github.com/anwholesquare/flutter-testdesign/tree/13d9c28909d9f2a30216952273726d2e1b38461e">https://github.com/anwholesquare/flutter-testdesign/tree/13d9c28909d9f2a30216952273726d2e1b38461e</a></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=t7VrP9etadA">https://www.youtube.com/watch?v=t7VrP9etadA</a></div>
<p> </p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">void</span> main () {
  runApp(<span class="hljs-keyword">const</span> MyApp());
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
  <span class="hljs-keyword">const</span> MyApp({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue
      ),
      home: <span class="hljs-keyword">const</span> Material (
        child: Center(
          child: Text(<span class="hljs-string">"Hello World"</span>, 
            style: TextStyle(
              fontSize: <span class="hljs-number">24</span>,
              fontWeight: FontWeight.bold
            ),
          ),
        ),
      )

    );
  }
}
</code></pre>
<p>The rest of the blog is to be continued.</p>
]]></content:encoded></item></channel></rss>