Categories
- A ll Codes (310)
- ACM (222)
- ACM Technique (18)
- ACM-ICPC (57)
- Adhoc/Brute Force (3)
- Algorithm (83)
- Algo Unsolved (3)
- Asymptotic Notation (3)
- Geometry (2)
- Graph (6)
- All Pair Shortest Path (1)
- BFS (1)
- DFS (1)
- Single Source Shortest Path (2)
- Bellman Ford (1)
- Dijkstra (1)
- Topological Sort (1)
- Number Theory (6)
- Euler GCD (1)
- GCD (1)
- LCM (1)
- Prime factorization (1)
- Sieve of Erastosthenes (1)
- Trial Division (1)
- Recursion (3)
- Fibonacci (1)
- Tower of Hanoi (1)
- Searching (3)
- Binary Search (2)
- Linear Search (1)
- Sorting (13)
- Bubble Sort (1)
- Bucket Sort (1)
- Counting Sort (1)
- Heap Sort (1)
- Insertion Sort (1)
- Merge Sort (3)
- Quicksort (2)
- Radix Sort (1)
- Selection Sort (3)
- Tree (6)
- AVL Tree (1)
- B+ Tree (1)
- Binary Search Tree (4)
- Competitive Programming (1)
- Data Structure (7)
- Linked List (1)
- Queue (2)
- Circular Queue (1)
- Stack (1)
- Future Reference (1)
- Problem Solution (101)
- Codeforces (3)
- DevSkill (1)
- HackerEarth (17)
- HackerRank (4)
- Lightoj (2)
- SPOJ (1)
- Unsolved (1)
- URI Online Judge (34)
- UVa (18)
- Programming Contest (2)
- Programming Problem Solving (18)
- Challenge (1)
- CS Courses (165)
- Art of Effective Living (1)
- Artificial Intelligence (13)
- Assembly Language (3)
- Compiler Design (1)
- Computer Architecture (3)
- Data Communication (1)
- Data Mining (23)
- WEKA (2)
- Database (10)
- SQL (8)
- Digital Image Processing (5)
- Embedded Systems (2)
- Arduino (2)
- Games Development (1)
- Graphics (9)
- OpenGL (7)
- Mathematics (26)
- Numerical Method (18)
- Bisection Method (4)
- Numerical Method (18)
- Microprocessor (1)
- OOP (53)
- Operating System (11)
- Bash/Shell Scripting (7)
- CPU Scheduling (1)
- Simulation and Modelling (1)
- Web Engineering (1)
- Experience (3)
- Events (1)
- Food Review (1)
- Travel (1)
- Financial Knowledge (1)
- Fitness (15)
- Body Transformation (2)
- Bodybuilding (7)
- Cycling (2)
- Fat Loss (2)
- Fitness Motivation (5)
- Health (2)
- Mental Health (1)
- My Gym (2)
- My Life Chart (3)
- My Life Style (3)
- Weight loss (1)
- Graphics Design (1)
- Photoshop (1)
- HigherStudy (86)
- EducationUSA (2)
- English Learning (9)
- Pronunciation (3)
- German Learning (28)
- Germany (9)
- Grammar (10)
- GRE (17)
- IELTS (26)
- Handwriting Improvement (1)
- Listening (4)
- Reading (1)
- Speaking (2)
- Writing Task (3)
- TOEFL (2)
- HTML/CSS (1)
- Job (56)
- CSE (2)
- CV/Resume (4)
- Interview (26)
- Fresher/Intern (3)
- Java (14)
- Software Engineering (3)
- Masters of Science (37)
- Advanced Formal Modelling (3)
- Cloud Computing (2)
- Data Mining (14)
- Implementation of Database (1)
- Introductory Data Analysis(IDA) (9)
- Excel (3)
- LaTeX (1)
- Machine Learning (3)
- Pattern Oriented Software Architecture(POS) (2)
- Real Time Computing (1)
- MSSQL (3)
- Stored Procedures (1)
- T-SQL (1)
- Open Source (6)
- Linux (4)
- Ubuntu 15.04 (1)
- Ubuntu 18.04 (1)
- Ubuntu14.04 (1)
- Ubuntu (2)
- Linux (4)
- Philosophy of Life (170)
- Learning (2)
- Life (10)
- Life Motivation (126)
- Business/Money (6)
- Career/Earning (12)
- Depression (3)
- Fearless Motivation (1)
- Life Quotes (2)
- Life Story/Journey (1)
- Passion (1)
- People Skill/Networking (14)
- Stress (2)
- Study Motivation (58)
- Success Hacks (74)
- Healthy Habit (15)
- Never Quit (36)
- Patience (23)
- Social Skills/Communicative Skills (5)
- Time Management (4)
- Life Productivity (11)
- Life Rules (5)
- Life Skills (4)
- Lifefact (4)
- Lifelessons (8)
- Lifestyle (1)
- Love (8)
- Motivation (17)
- Motivational Posts (7)
- Music (5)
- Poems (2)
- Love (1)
- Poetry (1)
- Ramadan (1)
- Romance (4)
- Sadness (2)
- Sarcasm (2)
- Thoughts (2)
- Pore Abar Bujhbo (8)
- Presentation (8)
- Programming (336)
- Language (305)
- C (152)
- Array (24)
- C library function (3)
- C Refresh (106)
- Function (17)
- logic (1)
- Logic Logic (4)
- Logics (1)
- Memory Management (1)
- Pointer (19)
- String (21)
- Structured Programming (1)
- C# (40)
- C++ (35)
- Vector (1)
- Java (46)
- Javascript (12)
- PHP (18)
- Python (17)
- C (152)
- R (1)
- R programming (3)
- Language (305)
- Project (2)
- Project Report (1)
- Research (53)
- BigData (3)
- Data Science (15)
- Machine Learning (23)
- Natural Language Processing(NLP) (5)
- Plagiarism Checker (2)
- Research Papers (5)
- Research Topic (3)
- Statistics (4)
- Sentiment Analysis (3)
- Software Development (84)
- .NET (34)
- Xamarin (1)
- Android (10)
- ASP.NET (10)
- Debate (2)
- Design Patterns Implementations (7)
- Laravel (4)
- Object Notation (4)
- Regex (2)
- Software Development Life Cycle(SDLC) (4)
- Training (9)
- LICT (9)
- unity3d (1)
- Version Control/SVN (4)
- XML (1)
- .NET (34)
- Tech Tips (21)
- Desktop (1)
- Fix (1)
- Laptop (1)
- Laptop(ল্যাপটপ) (1)
- HP (1)
- Techie Talk (15)
- Tech Debate (2)
- VPS (2)
- Uncategorized (62)
- University Life (4)
- 11th semester (1)
- 7th semester (1)
- 9th semester (1)
- Web Links (1)
- বাংলা (10)
- অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং (1)
- রিসার্চ (1)
- সি প্রোগ্রামিং (2)
- হার্ডডিস্ক পার্টিশান (1)
Random Quote
Random Quotes
-
Tag Archives: bangladesh
What Bodybuilders See ! :D
Posted in Bodybuilding, Fitness, Fitness Motivation
Tagged bangladesh, fitness motivation
Sorting Algorithm : Insertion Sort
From this two video I tried to re learn and implement the algorithm with pseudo code.
https://www.youtube.com/watch?v=i-SKeOcBwko
Pseudocode:
1 2 3 4 5 6 7 8 9 10 11 12 |
InsertionSort(A,n) for i=1 to Array.length(n) do key=A[i] j=i-1 while( j>=0 and A[j]>key ) do A[j+1]=A[j] j=j-1 done A[j+1]=key done |
Implemented Code in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<stdio.h> int main() { int i,j,n,key,ary[30]; printf("Enter total elements:"); scanf("%d",&n); printf("Enter %d elements: ",n); for(i=0;i<n;i++){ scanf("%d",&ary[i]); } //Ascending order for(i=1;i<n;i++) { key=ary[i]; j=i-1; while((j>=0)&&(ary[j]>key)){ ary[j+1]=ary[j]; j=j-1; } ary[j+1]=key; } printf("After Sorting Ascending Order: "); for(i=0;i<n;i++) { printf(" %d",ary[i]); } //Descending Order for(i=1;i<n;i++) { key=ary[i]; j=i-1; while((j>=0)&&(ary[j]<key)){ ary[j+1]=ary[j]; j=j-1; } ary[j+1]=key; } printf("\nAfter Sorting Descending Order: "); for(i=0;i<n;i++) { printf(" %d",ary[i]); } return 0; } |
Here ary[] means array 🙂
Simulation:(Wikipedia)
Harvard CS50 course:
http://cs50.tv/2016/fall/
GeeksForGeeks Algo Implement(Not Worked Yet):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<cstdio> #include<iostream> using namespace std; void insertionSort(int arr[], int n){ int i,key,j; for(i=1; i<=n; i++){ key=arr[i]; j=i-1; while(j>=0 && arr[j]>key){ arr[j+1]=arr[j]; j=j-1; } arr[j+1]=key; } } void printInsertion(int arr[],int n){ int i; for(i=0;i<n;i++) { cout<<arr[i]<<endl; } } int main(){ int arr[101]; int n=sizeof(arr)/sizeof(arr[0]); for(int i=0; i<=n; i++){ cin>>arr[n]; } insertionSort(arr, n); printInsertion(arr, n); return 0; } |
http://quiz.geeksforgeeks.org/insertion-sort/
Practice Problem:
http://www.practice.geeksforgeeks.org/problem-page.php?pid=700148
https://www.hackerearth.com/practice/algorithms/sorting/insertion-sort/practice-problems/
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A
CLRS Theory:
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/insertionSort.htm
Simple Implementation of Insertion Sorting(Idea Sanfoundry):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<cstdio> #include<iostream> using namespace std; int main(){ int a[101]; int n,i,j,p,key; cin>>n; for(p=0;p<n;p++) { cin>>a[p]; } for(i=0; i<n; i++){ key=a[i]; j=i-1; while(j>=0 && a[j]>key){ a[j+1]=a[j]; j=j-1; } a[j+1]=key; } for(p=0;p<n;p++){ cout<<" "<<a[p]<<endl;; } return 0; } |
It is helpful: http://www.sanfoundry.com/cpp-program-sort-array/
Time complexity of Insertion Sort:
- Worst case complexity : O(n^2)
- Best case complexity : O(n)
- Average case complextiy: O(n^2)Reference:
http://bigocheatsheet.com/
How to calculate complexity there is a example available at the upper video “Lecture 7”
and there are some links
http://stackoverflow.com/questions/19827193/time-complexity-of-insertion-sort
Here a good presentation about this sorting
http://www.cs.mcgill.ca/~cs203/lectures/lecture7/sld006.htm
Reference: http://www.cquestions.com/2009/09/insertion-sort-algorithm.html
For descending order I took help from:
http://cboard.cprogramming.com/c-programming/73433-sorting-descending-order.html
Here they change the comparison value for descending at while loop condition
Another good website:
Time complexity of insertion sort when there are O(n) inversions?
For some theory revise this video is good:
https://www.youtube.com/watch?v=olaM9mqOmlI
Posted in A ll Codes, ACM-ICPC, Algorithm, C++, Insertion Sort
Tagged algorithm, ascending, bangladesh, Codes, descending, easy implementation, Insertion Sort
C code for bisection method
The Bisection Method is a numerical method for estimating the roots of a polynomial f(x). It is one of the simplest and most reliable but it is not the fastest method.
Problem: Here we have to find root for the polynomial x^3+x^2-1
Solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<stdio.h> #include<math.h> #define TOL 0.0001 float func(double x) { return (pow(x,3)+pow(x,2)-1); } int main() { double a,b; int iteration,i; printf("Root finding of function x^3+x^2-1 using bisection method.\n"); printf("Enter the first approximation of the root:\n"); scanf("%lf",&a); printf("Enter the second approximation of the root:\n"); scanf("%lf",&b); printf("Enter the iteration you want to perform :"); scanf("%d",&iteration); i=1; double a1=a; double b1=b; double root,f1,f2,f3; if(func(a1)==0) root=a1; else if(func(b1)==0) root=b1; else{ while(i<iteration) { root=(a1+b1)/2; f1=func(a1); f2=func(root); f3=func(b1); if(f2==0) { root=f2; break; } printf("The root after %d iteration is %lf\n",i,root); if(f1*f2<0) b1=root; else if(f2*f3<0) a1=root; i++; } } printf("The approximation of the root is %lf",root); return 0; } |
Algorithm:
- Start
- Read a1, b1, TOL
*Here a1 and b1 are initial guesses
TOL is the absolute error or tolerance i.e. the desired degree of accuracy* - Compute: f1 = f(a1) and f3 = f(b1)
- If (f1*f3) > 0, then display initial guesses are wrong and goto step 11
Otherwise continue. - root = (a1 + b1)/2
- If [ (a1 – b1)/root ] < TOL , then display root and goto step 11
* Here [ ] refers to the modulus sign. *
or f(root)=0 then display root - Else, f2 = f(root)
- If (f1*f2) < 0, then b1=root
- Else if (f2*f3)<0 then a1=root
- else goto step 5
*Now the loop continues with new values.* - Stop
Output:
Another Problem Solving Code:
Problem: Here we have to find root for the polynomial x^3+x^2-1 upto 4D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<stdio.h> #include<math.h> #define f(y) (pow(x,3)+x*x-1); int main() { double a,b,m=-1,x,y; int n=0,k,i; printf("Enter the value of a: "); scanf("%lf",&a); printf("Enter the value of b: "); scanf("%lf",&b); printf("How many itteration you want: "); scanf("%d",&k); printf("\n n a b xn=a+b/2 sign of(xn)\n"); printf("-------------------------------------------------------------\n"); for(i=1;i<=k;i++) { x=(a+b)/2; y=f(x); if(m==x) { break; } if(y>=0) { printf(" %d %.5lf %.5lf %.5lf +\n",i,a,b,x); b=x; } else if(y<0) { printf(" %d %.5lf %.5lf %.5lf -\n",i,a,b,x); a=x; } m=x; } printf("\nThe approximation to the root is %.4lf which is upto 4D",b); return 0; } |
C code for bisection method
The Bisection Method is a numerical method for estimating the roots of a polynomial f(x). It is one of the simplest and most reliable but it is not the fastest method.
Problem: Here we have to find root for the polynomial x^3+x^2-1
Solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<stdio.h> #include<math.h> #define TOL 0.0001 float func(double x) { return (pow(x,3)+pow(x,2)-1); } int main() { double a,b; int iteration,i; printf("Root finding of function x^3+x^2-1 using bisection method.\n"); printf("Enter the first approximation of the root:\n"); scanf("%lf",&a); printf("Enter the second approximation of the root:\n"); scanf("%lf",&b); printf("Enter the iteration you want to perform :"); scanf("%d",&iteration); i=1; double a1=a; double b1=b; double root,f1,f2,f3; if(func(a1)==0) root=a1; else if(func(b1)==0) root=b1; else{ while(i<iteration) { root=(a1+b1)/2; f1=func(a1); f2=func(root); f3=func(b1); if(f2==0) { root=f2; break; } printf("The root after %d iteration is %lf\n",i,root); if(f1*f2<0) b1=root; else if(f2*f3<0) a1=root; i++; } } printf("The approximation of the root is %lf",root); return 0; } |
Algorithm:
- Start
- Read a1, b1, TOL
*Here a1 and b1 are initial guesses
TOL is the absolute error or tolerance i.e. the desired degree of accuracy* - Compute: f1 = f(a1) and f3 = f(b1)
- If (f1*f3) > 0, then display initial guesses are wrong and goto step 11
Otherwise continue. - root = (a1 + b1)/2
- If [ (a1 – b1)/root ] < TOL , then display root and goto step 11
* Here [ ] refers to the modulus sign. *
or f(root)=0 then display root - Else, f2 = f(root)
- If (f1*f2) < 0, then b1=root
- Else if (f2*f3)<0 then a1=root
- else goto step 5
*Now the loop continues with new values.* - Stop
Output:
Another Problem Solving Code:
Problem: Here we have to find root for the polynomial x^3+x^2-1 upto 4D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<stdio.h> #include<math.h> #define f(y) (pow(x,3)+x*x-1); int main() { double a,b,m=-1,x,y; int n=0,k,i; printf("Enter the value of a: "); scanf("%lf",&a); printf("Enter the value of b: "); scanf("%lf",&b); printf("How many itteration you want: "); scanf("%d",&k); printf("\n n a b xn=a+b/2 sign of(xn)\n"); printf("-------------------------------------------------------------\n"); for(i=1;i<=k;i++) { x=(a+b)/2; y=f(x); if(m==x) { break; } if(y>=0) { printf(" %d %.5lf %.5lf %.5lf +\n",i,a,b,x); b=x; } else if(y<0) { printf(" %d %.5lf %.5lf %.5lf -\n",i,a,b,x); a=x; } m=x; } printf("\nThe approximation to the root is %.4lf which is upto 4D",b); return 0; } |
C code for bisection method
The Bisection Method is a numerical method for estimating the roots of a polynomial f(x). It is one of the simplest and most reliable but it is not the fastest method.
Problem: Here we have to find root for the polynomial x^3+x^2-1
Solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<stdio.h> #include<math.h> #define TOL 0.0001 float func(double x) { return (pow(x,3)+pow(x,2)-1); } int main() { double a,b; int iteration,i; printf("Root finding of function x^3+x^2-1 using bisection method.\n"); printf("Enter the first approximation of the root:\n"); scanf("%lf",&a); printf("Enter the second approximation of the root:\n"); scanf("%lf",&b); printf("Enter the iteration you want to perform :"); scanf("%d",&iteration); i=1; double a1=a; double b1=b; double root,f1,f2,f3; if(func(a1)==0) root=a1; else if(func(b1)==0) root=b1; else{ while(i<iteration) { root=(a1+b1)/2; f1=func(a1); f2=func(root); f3=func(b1); if(f2==0) { root=f2; break; } printf("The root after %d iteration is %lf\n",i,root); if(f1*f2<0) b1=root; else if(f2*f3<0) a1=root; i++; } } printf("The approximation of the root is %lf",root); return 0; } |
Algorithm:
- Start
- Read a1, b1, TOL
*Here a1 and b1 are initial guesses
TOL is the absolute error or tolerance i.e. the desired degree of accuracy* - Compute: f1 = f(a1) and f3 = f(b1)
- If (f1*f3) > 0, then display initial guesses are wrong and goto step 11
Otherwise continue. - root = (a1 + b1)/2
- If [ (a1 – b1)/root ] < TOL , then display root and goto step 11
* Here [ ] refers to the modulus sign. *
or f(root)=0 then display root - Else, f2 = f(root)
- If (f1*f2) < 0, then b1=root
- Else if (f2*f3)<0 then a1=root
- else goto step 5
*Now the loop continues with new values.* - Stop
Output:
Another Problem Solving Code:
Problem: Here we have to find root for the polynomial x^3+x^2-1 upto 4D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<stdio.h> #include<math.h> #define f(y) (pow(x,3)+x*x-1); int main() { double a,b,m=-1,x,y; int n=0,k,i; printf("Enter the value of a: "); scanf("%lf",&a); printf("Enter the value of b: "); scanf("%lf",&b); printf("How many itteration you want: "); scanf("%d",&k); printf("\n n a b xn=a+b/2 sign of(xn)\n"); printf("-------------------------------------------------------------\n"); for(i=1;i<=k;i++) { x=(a+b)/2; y=f(x); if(m==x) { break; } if(y>=0) { printf(" %d %.5lf %.5lf %.5lf +\n",i,a,b,x); b=x; } else if(y<0) { printf(" %d %.5lf %.5lf %.5lf -\n",i,a,b,x); a=x; } m=x; } printf("\nThe approximation to the root is %.4lf which is upto 4D",b); return 0; } |
C code for bisection method
The Bisection Method is a numerical method for estimating the roots of a polynomial f(x). It is one of the simplest and most reliable but it is not the fastest method.
Problem: Here we have to find root for the polynomial x^3+x^2-1
Solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<stdio.h> #include<math.h> #define TOL 0.0001 float func(double x) { return (pow(x,3)+pow(x,2)-1); } int main() { double a,b; int iteration,i; printf("Root finding of function x^3+x^2-1 using bisection method.\n"); printf("Enter the first approximation of the root:\n"); scanf("%lf",&a); printf("Enter the second approximation of the root:\n"); scanf("%lf",&b); printf("Enter the iteration you want to perform :"); scanf("%d",&iteration); i=1; double a1=a; double b1=b; double root,f1,f2,f3; if(func(a1)==0) root=a1; else if(func(b1)==0) root=b1; else{ while(i<iteration) { root=(a1+b1)/2; f1=func(a1); f2=func(root); f3=func(b1); if(f2==0) { root=f2; break; } printf("The root after %d iteration is %lf\n",i,root); if(f1*f2<0) b1=root; else if(f2*f3<0) a1=root; i++; } } printf("The approximation of the root is %lf",root); return 0; } |
Algorithm:
- Start
- Read a1, b1, TOL
*Here a1 and b1 are initial guesses
TOL is the absolute error or tolerance i.e. the desired degree of accuracy* - Compute: f1 = f(a1) and f3 = f(b1)
- If (f1*f3) > 0, then display initial guesses are wrong and goto step 11
Otherwise continue. - root = (a1 + b1)/2
- If [ (a1 – b1)/root ] < TOL , then display root and goto step 11
* Here [ ] refers to the modulus sign. *
or f(root)=0 then display root - Else, f2 = f(root)
- If (f1*f2) < 0, then b1=root
- Else if (f2*f3)<0 then a1=root
- else goto step 5
*Now the loop continues with new values.* - Stop
Output:
Another Problem Solving Code:
Problem: Here we have to find root for the polynomial x^3+x^2-1 upto 4D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<stdio.h> #include<math.h> #define f(y) (pow(x,3)+x*x-1); int main() { double a,b,m=-1,x,y; int n=0,k,i; printf("Enter the value of a: "); scanf("%lf",&a); printf("Enter the value of b: "); scanf("%lf",&b); printf("How many itteration you want: "); scanf("%d",&k); printf("\n n a b xn=a+b/2 sign of(xn)\n"); printf("-------------------------------------------------------------\n"); for(i=1;i<=k;i++) { x=(a+b)/2; y=f(x); if(m==x) { break; } if(y>=0) { printf(" %d %.5lf %.5lf %.5lf +\n",i,a,b,x); b=x; } else if(y<0) { printf(" %d %.5lf %.5lf %.5lf -\n",i,a,b,x); a=x; } m=x; } printf("\nThe approximation to the root is %.4lf which is upto 4D",b); return 0; } |