Applications of C Programming in Numerical ...

1 downloads 0 Views 2MB Size Report
Jun 13, 2016 - 4.2.3 Simpson's Rule Formula : To Calculate Integral Value . . . 26 ... C Program does not allow punctuation characters such as @, $, and % ..etc within ... 1. Primary Data types Size. (i). Character. 1byte a single character. (ii). .... to keep large volume of data, it is time consuming to enter the entire data again.
Applications of C Programming in Numerical Techniques and Cryptography

by : Kalika Prasad M.Sc Tech. Mathematics (II-Semester, 2015mstm012) Department of Mathematics Central University of Rajasthan June 13, 2016

Certificate Certified that the Summer Internship project report “Applications of C Programming in Numerical Techniques and Cryptography” is the work of “Kalika Prasad”, 1st Year, MSc.Tech Mathematics student at Central University of Rajasthan, Bandar Sindari(Ajmer, Rajasthan) DST INSPIRE Reg No. 4406/2012, carried out under my supervision during May 13, 2016 to Jun 11, 2016. Place: IIT (BHU), Varanasi Date: Jun 13, 2016 Prof. Sanjay Kumar Pandey Supervisor Professor, Indian Institute of Technology, BHU, Varanasi 210 005

1

Contents I

Part I : Basic Introduction of C programming

1 Overview 1.1 Abstract . . 1.2 Introduction 1.3 Algorithms . 1.4 Termology . 1.5 Pointers . .

4 . . . . .

5 5 5 5 6 6

2 Data types & Operators 2.1 Data types in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Operators in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7 7 8

3 File 3.1 3.2 3.3 3.4

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Handling(file I/O) Introduction : . . . . . . . . . . . . . . . . . Creating or Opening a file . . . . . . . Closing a File . . . . . . . . . . . . . . . . . Input/Output operations on a file . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . . .

. . . .

. . . .

11 11 11 14 14

II Aplication in Numerical Techniques and Number Theory 18 4 Application in Numerical Techniques 4.1 Root - Finding Method . . . . . . . . . . . . . 4.1.1 BISECTION METHOD : . . . . . . . 4.1.2 NEWTON-RAPHSON METHOD : . . 4.1.3 SECANT METHOD : . . . . . . . . . 4.2 Numerical Calculation . . . . . . . . . . . . . 4.2.1 Gauss–Seidel Method . . . . . . . . . . 4.2.2 Trapezoidal Rule . . . . . . . . . . . . 4.2.3 Simpson’s Rule Formula : To Calculate

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integral Value

. . . . . . . .

. . . . . . . .

. . . . . . . .

19 19 19 20 20 25 25 25 26

5 Application in Number Theory 5.1 Construction of a programming . . . . . . 5.2 Cryptography : Encryption & Decryption 5.2.1 Introduction . . . . . . . . . . . . . 5.2.2 Ceaser Cipher Method . . . . . . .

. . . .

. . . .

. . . .

. . . .

32 32 33 33 33

2

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

5.2.3 Vigen`ere Cipher Method . . . . . . 5.2.4 Some other cryptographic methods Acknowledgement . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . .

3

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

37 37 40 41

Part I Part I : Basic Introduction of C programming

4

Chapter 1 Overview 1.1

Abstract

Any numerical/mathematical problems may be solved by using programming. In this paper we will discuss basics of C program to solve numerical problems(like calculating roots, numerical aproximaion, Integral value,...etc) and other mathematical problems(like Encryption, Decryption, coding...etc) using programings like C-Programming. For this we need basic knowledge of programming. In Part II, We will discuss how to solve problems using C-programming and write code for solving problems. Here, we will develop simple C programs using numerical algorithms to solve the problems of Numerical analysis and Number theory. This is a report of the DST INSPIRE PG Summer Project that I did with Prof S.K Pandey at Indian Institute of Techonology BHU, Varanasi.

1.2

Introduction

The C programming language is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. C program is a powerful, flexible, portable and elegantly structure lannguage. Since C combines the features of high-level Language with the elements of the assembler, it is suitable for both systems and application programming. Programs written in C are efficent and fast(this is due to its variety of data types and powerful operators). Keywords : Array, compiler, Input/output, default, library, functions, pointers, variables;

1.3

Algorithms

To investigate physical problems we develop mathematical models and then attempt to solve the equations. Often this cannot be done analytically and instead we must use a computer. But how do we use a computer programme to solve a mathematical problem ? 5

First we will develop an algorithm for the given problem and then implementing that algorithm we develop a programme. Then we must test our programme, and finally we can run the programme to generate the results we need.

1.4

Termology

1. Tokens : A token is either a keyword, an identifier, a constant, a string, or a symbol. A C program consists of various tokens. 2. Identifiers : A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore followed by zero or more letters, underscores, and digits (0 to 9). C Program does not allow punctuation characters such as @, $, and % ..etc within identifiers. C is a case sensitive programming language. Thus, Example and example are two different identifiers in C. Here are some examples of valid identifiers: klka, abc, move name, a 123, myname50, temp, j, a23b9, retVal 3. Keywords & identifier There are 32 common keyword in C. All keywords have fixed meanings and these meanings cannot be changed. These reserved words may not be used as constant or variable or any other identifier names. auto break case char const continue default do

1.5

double else enum extern float for if goto

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

Pointers

A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. Pointer variable is declared as: int *pointer; • Operator ’&’ is called Address Operator and gives address of variable. If v is a variable then, &v is the address of v in memory. • Operator(*) is called dereference Operator. It gives the value that is stored in the memory location pointed by the pointer Pointer variables : It is a special types of variables that holds memory address rather than data. NOTE: We must associate a pointer to a particular data type: We can’t assign the address of a short int to a long int. 6

Chapter 2 Data types & Operators Objective In this section we are only going to be discussing a short introduction of data types and Operators in C for further use in application part. The first thing we need to know is that how we can create variables to store values in it. A variable is just a named area of storage that can hold a single value (numeric or character). In C programming, variables or memory locations should be declared before it can be used. Similarly, a function also needs to be declared before use. Data types simply refers to the type and size of data associated with variables and functions.

2.1

Data types in C

Primary Data types (Basic or fundamental) Integer(int) float(float) double(double) character(char) void

Data types Secondary data types ( derived Data Types) Array pointer structure enum ..etc

1. Primary Data types Size (i). Character 1byte (ii). Integer(int) 2bytes (iii). float(float) 4bytes (iv). double(double) 8bytes (v). void

a single character. a whole number. real number. a double-precision floating point value. valueless special purpose types.

2. Modifieers It changes size/range of deafult data type, it means it increases or decreases the size of default data type. (1). sign 7

(a)signed (2). size (a). short

(b). unsigned (b). long

3. Size int a 2bytes short int a 2bytes long int a 4 bytes long double a 10bytes Note : we don’t use size modifiers with char and float data types 4. Sign Signed modifiers are used with integer and character data types. Example : int a; or signed int a; signed char ch; unsigned int b; //It stores only positive value Difference between float and double : The size of float (single float data type) is 4 bytes. And the size of double (double float data type) is 8 bytes. Floating point variables has of precision 6 digits whereas the precision of double is 14 digits.

2.2

Operators in C

An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition. C programming has wide range of operators to perform various operations. C Operators can be classfied into the followings categories : 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Conditional Operators 5. Assignment Operators 6. Increment and Decrement Operators 7. Bitwise Operators 8. Special Operators Short description about above operators 8

1. Arithmetic Operators An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables). Operators : +, -, *, %, /; 2. Relational Operators A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops. Operators : =, ==, ! =; 3. Logical Operators An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. Operators : AND(&&), OR(||), NOT(!) 4. Conditional Operators A conditional operator is a ternary operator, that is, it works on 3 operands. Conditional Operator Syntax : conditional expr ? expr1 : expr2 The conditional operator works as follows: • The first expression conditional expr is evaluated at first. This expression results to 1 if it’s true and results to 0 if it’s false. • If conditional expr is true, expr1 is evaluated. • If conditional expr is false, expr2 is evaluated. 5. Assignment Operators An assignment operator is used for assigning a value to a variable. The most common assignment operator is = Operator Example M eaning = a=b a=b += a+ = b a = a + b −+ a − +b a = a − b ∗= a∗ = b a=a∗b /= a/ = b a = a/b %= a% = b a = a%b 6. Increment and Decrement Operators C programming has two operators increment ++ and decrement −− to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement −− decreases the value by 1. These two operators are unary operators, means they only operate on a single operand. 7. Bitwise Operators In processor, mathematical operations like: addition, subtraction, addition and division are done in bit-level which makes processing faster and saves 9

power. Bitwise erations. Operators & | ∧ ∼ >

operators are used in C programming to perform bit-level opMeaning of operators Bitwise AND Bitwise OR Bitwise exclusive OR Bitwise complement Shift left Shift right

8. Special Operators Comma Operator : Comma operators are used to link related expressions together. sizeof() : The sizeof is an unary operator which returns the size of data (constant, variables, array, structure etc).

10

Chapter 3 File Handling(file I/O) 3.1

Introduction :

Why files are needed ? As previously, When the program is terminated, the entire data is lost. If we want to keep large volume of data, it is time consuming to enter the entire data again and again. But, if file is created, these information can be accessed using few commands. File is created for permanent storage of data. So here, we use file handling. High level file I/O functions can be categorized as: 1. Text file 2. Binary file

Basic File Operations are : 1. 2. 3. 4. 5.

Creating a new file Opening an existing file Reading data from a file Writing data(information) to a file and Closing a file

How it work We need to declare a pointer of type FILE. This declaration is needed for communication between file and program. (declaration as : FILE *ptr;)

3.2

Creating or Opening a file

If we want to store data in the secondry memory(on computer), then first we create a file and then we put data in that file. Opening/creating a file is performed using library function fopen(). The syntax for opening a file in standard I/O is FILE *fp,*f1; fp = fopen(”filename”,”mode”); For Example: fp = fopen(”program01.txt”,”w”); // for writing purpose f1 = fopen(”program02.txt”,”r”); // for reading purpose If the filename ”program.txt” is present in the corresponding folder then it will be 11

opened, else a file with name ”program.txt is created there.

Opening modes in Standard I/O and their meaning Mode Meaning During Inexistence of file r Open for reading. If the file does not exist, fopen() returns NULL w Open for writing If the file exists, its contents are overwritten. If the file does not exist, it will be created. a Open for append. i.e, If the file does not exists, it will be created. Data is added to end of file. r+ Open for both reading If the file does not exist, fopen() returns and writing. NULL. w+ Open for both reading If the file exists, its contents are overwritten. and writing. If the file does not exist, it will be created. a+ Open for both reading If the file does not exists, it will be created. and appending. High level I/O function name

function name

Description

fopen() fclose() getc() putc() getw() putw() fprintf() fscanf() fseek() ftell()

Creates a new file or opens a existing file for use Closes a file which has been opend for use Reads a character from a file Writes a character to a file Reads an integer from a file Writes an integer to a file Writes a set of data to a file. Reads a set of data from a file. Set the position to desire point in the file Gives the current position in the file(in terms of bytes from the start) Set the position to the begining point of file

rewind()

Difference between Append(a) and Write Mode(w) Both are used to write in a file. In both the modes, new file is created if it doesn’t exists already. The only difference they have is, when you open a file in the write mode, the file is reset, resulting in deletion of any data that already present in the file. While in append mode this will not happen. Append mode is used to append or add data to the existing data of file(if any). Hence, when we open a file in Append(a) mode, the cursor is positioned at the end of the present data in the file.. An example of all three modes are on next page..

12

Writing(mode w) to a file When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does #include int main() { int n; FILE *fp; fp=fopen("program.txt","w"); //to create or open a file if(fp==NULL) { printf("Error !, file cannot be opened"); exit(1); } printf("\n\t Enter value of n : "); scanf("%d",&n); fprintf(fp,"%d",n); //writing data to file fclose(fp); //closing the opened file return 0; }

After compile and run this program, we can see a text file ‘program.txt’ created in C drive of our computer. When you open that file, we may see the entered data.

Reading(mode r) from file Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL #include int main() { int n; FILE *fp; if ((fp=fopen("program.txt","r"))==NULL) { printf("Error! opening file"); exit(1); /* Program exits if file pointer returns NULL. */ } fscanf(fp,"%d",&n); printf("Value of n = %d",n); fclose(fp); return 0; }

While the above program file has written successfully, then by running second program we can get back the entered data..

Appending (mode a) When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. If it does exist, the entered data will be positioned at the end of the present data. #include int main() { FILE *fp file = fopen("program.txt","a"); fprintf(fp,"%s","This is just an example : "); /*append some text*/ fclose(fp); return 0; }

3.3

Closing a File

The file must be closed after all operations on it have been completed. Closing a file is performed using library function fclose(). General syntax fclose(file ptr); //ptr is the file pointer associated with file to be closed. For Example FILE *p1 *p2; p1=fopen (“program01.txt” , ”w”); p2=fopen (“program02.txt” , ”r”); .... ... . fclose(p1); fclose(p2); Here fclose() function closes the file and returns zero on success, or EOF(endof-file) if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h.

3.4

Input/Output operations on a file

1. The getc() and putc() functions. The getc() and putc() are simplest functions used to read and write individual characters to a file(similar to getchar() and putchar() that handle one character at a time). 2. The getw() and putw() functions. These are integer-oriented functions. They are similar to getc() and putc() functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data 3. The fprintf() and fscanf() functions. The fprintf() and fscanf() functions are identical to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used. The general form of fprintf is fprintf(fp,”control string”, list); 4. The fseek() function. fseek() - It is used to moves the reading control to desired location within the file. The general format of fseek function is a s follows: fseek(file pointer, offset, position); The position can take one of three values(either 0, 1 or 2). The offset may be positive(for moving forwards) or negative(for moving backwards)

14

Example 1: Reading and writing using getc() / putc() #include #include main() { FILE *fp; char ch; fp = fopen("one.txt", "w"); printf("Enter any message : "); while( (ch = getchar())!= EOF) { putc(ch,fp); } fclose(fp); printf("\n\t=====OUTPUT=====\n"); fp = fopen("one.txt", "r"); while( (ch = getc(fp)! = EOF) printf("%c",ch); fclose(fp); }

Output :

Example 2: Reading and writing using getw() / putw() #include #include main() { FILE *f1,*f2,*f3; int num,i; printf("\nProgram for separating odd & even using getw/putw\n"); f1=fopen("data.txt","w"); //Create data.txt file printf("\n\tEnter 20 Numbers : "); //Input integer from keyboard for(i=1;i0) printf("\n\tProvided Initial data is unstable"); else { printf("\n\t-----------------------------------------------------------------------------------------"); printf("\n\t\ta \t f(a) \t\tb \t\tf(b) \t\tx\t\tf(x)\n"); fprintf(fp,"\n\t\ta \t f(a) \t\tb \t\tf(b) \t\tx\t\tf(x)\n"); printf("\n\t-----------------------------------------------------------------------------------------"); while(fabs((b-a)/b)>eps) { x=(a+b)/2; i++; if((f(x)*f(a))>0) { a=x; } else { b=x; } fprintf(fp,"\n\t %f\t%f\t%f \t%f\t %f\t %f",a,f(a),b,f(b),x,f(x)); printf("\n\ta = %f f(a) = %f b = %f f(b) = %f x = %f f(x) = %f",a,f(a),b,f(b),x,f(x)); } } printf("\n\n\tSolution converse to a root and No. of Iteration = % d",i); printf("\n"); printf("\n\tValue of a, b & x \n\t a = %f\n\tb = %f\n\tx = %f",a,b,x); fprintf(fp,"\n\n\tSolution converse to a root and No. of Iteration = % d",i); fprintf(fp,"\n\tValue of a, b & x\n\ta = %f\n\tb = %f\n\tx = %f",a,b,x); fclose(fp); getch(); }

Output :

Program 2: C Code for Newton-Raphson Method #include #include #include float f1(float x) { return(pow(x,3)-2*x-5); //Given a function f1 = f(x) = x3 – 2x-5 } float f2(float x) { return(3*pow(x,2)-2); //First derivative of f1 = f2(say) } int main() { FILE *fp; float a,x1,x2; int i; fp=fopen("newton.txt","w"); printf("\n\t========NEWTON-RAPHSON METHOD========== \n"); printf("\n\tEnter Initial Value of s : "); scanf("%f",&a); fprintf(fp,"\n\tEnter Initial Value of x : %f\n",a); x1=a; x2=x1-(f1(x1))/(f2(x1)); printf("\n\tValue of iterations are \n"); printf("\n\t----------------------------------------------"); printf("\n\t\tx(i) \t\tf(x(i))\n"); fprintf(fp,"\n\t\tx(i) \t\tf(x(i))\n");

printf("\n\t----------------------------------------------"); printf("\n\tx(1) = %f\t\tf(x(i)) = %f\n\tx(2) = %f\tf(x(i)) = %f",x1,x2,f1(x1),f1(x2)); fprintf(fp,"\n\t %f\t\t %f\n\t%f\t\t%f",x1,x2,f1(x1),f1(x2)); ///for appending data in file for(i=1;x1!=x2;i++) { printf("\n\tx(%d) = %f\tf(x(i)) = %f",i+2,x2,f1(x2)); fprintf(fp,"\n\t%f\t\t %f",x2,f1(x2)); ///for saving data in created file x1=x2; x2=x1-(f1(x1)/f2(x1)); //fprintf(fp,"%d %d",x1,x2); } printf("\nThe root is %f and Iteration is approximately %d\n",x2,i); fprintf(fp,"\n\nThe root is %f and Iteration is approximately %d\n",x2,i); fclose(fp); getch(); }

Output :

Program 3: C Code for Secant Method #include #include #include float f(float x) { return(pow(x,3)-5*x+1); } int main() { FILE *fp;

//Given a function f(x) = x3 – 5x+1

float a, b, x0, x1, x2 ; int i; printf("\n\t========SECANT METHOD==========\n"); fp=fopen("secant.txt","w"); //Creating file on secondary memeory printf("\n\tEnter Value of a and b : "); scanf("%f%f",&a,&b); fprintf(fp,"\n\tEnter Value of a = %f \tb = %f\n",a,b); x0=a; x1=b; x2=x1-(((x1-x0)/(f(x1)-f(x0)))*f(x1)); printf("\n\tIterations are as follows \n"); fprintf(fp,"\n\tIterations are as follows \n"); //Far adding in opened file printf("\n\t----------------------------------------------"); printf("\n\t x(i) \t\t\tf(x(i))\n"); fprintf(fp,"\n\t x(i) \t\t\tf(x(i))\n"); //Far adding in opened file printf("\n\t----------------------------------------------"); printf("\n\tx(1) = %f \t %f \n\tx(2) = %f\t %f",x1,x2,f(x1),f(x2)); fprintf(fp,"\n\tx(1) = %f \t %f \n\tx(2) = %f\t %f",x1,x2,f(x1),f(x2)); //Far adding in opened for(i=1;x1!=x2;i++) { printf("\n\tx(%d) = %f\t %f",i+2,x2,f(x2)); fprintf(fp,"\n\tx(%d) = %f\t %f",i+2,x2,f(x2)); x0=x1; x1=x2; x2=x1-(((x1-x0)/(f(x1)-f(x0)))*f(x1)); } printf("\n\n\t Root is %f and Iteration is %d\n",x2,i+1); fprintf(fp,"\n\n\t Root is %f and Iteration is %d\n",x2,i+1); //Far adding in opened file fclose(fp); getch(); } OUTPUT :

4.2

Numerical Calculation

Suppose we want to find the definite integral,

A=

Rb

f (x)dx

—–(1) Perhaps the integral cannot be performed analytically, or perhaps f (x) is some numerical data stored in a table. Then we need to calculate A using a computer programme. Here, at first we need an algorithm to make a programe to find integral value. Since, A is just the area under the curve f (x) from x = a to x = b. Then to find the area, we could draw f (x) on graph paper and count the squares underneath the curve. Here we will develop a computer programme which will essentially do this for us. The key to the method is to split up the unknown area A into smaller sections of area that we can calculate. Using this technique, we devloped a C program in section(3.2.2) In this section, We take some example of numerical calculation and devolop their C Program. For exampl, Gauss-Seidel Method for solving system of linear equation, Trapezoidal rule for finding integral value...etc.

4.2.1

a

Gauss–Seidel Method

Example 4: Write a C program to solve a linear system of equations using GaussSeidel Method. Suppose that given system of linear equation are X1 (x2 , x3 ) = (17 − 20x2 + 2x3 )/20 X2 (x1 , x3 ) = (−18 − 3x1 + x3 )/20 X3 (x1 , x2 ) = (25 − 2x1 + 3x2 )/20 Note: Gauss–Seidel Method is an iterative method that used to solve a linear system of equations with unknown x. Also, It can be applied to any matrix with non-zero elements on the diagonals. See the Program 4 on next page for C code.

4.2.2

Trapezoidal Rule

Example 5: Write a C program to find the integral of a function using Trapezoidal 1 rule. Suppose that f (x) = 1 + x2 Note : Trapezoidal rule is a technique for approximating the definite integral

Rb

a f (x)dx. It works by approximating the region under the graph of the function f (x) as a trapezoid and calculating its area. It follows that Rb (f (a) + f (b)) f (x)dx ' (b − a) a 2 See the Program 5 on next page for C code.

25

4.2.3

Simpson’s Rule Formula : To Calculate Integral Value

Example 6: Write a program to find the integral value of a function using Simp1 son’s 1/3rd and 3/8th rule. Suppose hat f (x) = . 1 + x2 Note : Simpson’s Rule Formula is used to calculate the integral value of any function. It calculates the value of the area under any curve over a given interval by dividing the area into equal parts. It follows the method similar to integration by parts. In order to setup code in C Program to integrate any function f (x) in the interval (a, b), we follow the following steps : 1. Selecting a value of n (to devide interval in n parts). 2. Now Calculating the width, h =

b−a . n

3. Now Calculating the values of x0 to xn as x0 = a, x1 = x0 + h, .....xn−1 = xn−2 + h, xn = b. 4. Now let y = f (x). Now finding the values of y(y0 to yn ) for the corresponding x(x0 to xn ) values. 5. Now Substituting all the above found values in the Simpson’s Rule Formula to calculate the integral value. Using above algorithm, Here we are constructing a C program for Simpson’s 1/3rd rule and 3/8th rule See the Program 6 on next page for C code.

26

Program 4: C program to solve a linear system of equations using Gauss-Seidel Method. #include #include #include #define eps 0.0001 #define X1(x2,x3) (17-20*x2+2*x3)/20 #define X2(x1,x3) (-18-3*x1+x3)/20 #define X3(x1,x2) (25-2*x1+3*x2)/20 int main() { FILE *fp; fp=fopen("gauss.txt","w"); double x1,x2,x3,y1,y2,y3; int i=0; printf("\n\t===========GAUSS SEIDEL METHOD============"); printf("\n\t"); printf("\n\tEnter Value of x1, x2 and x3 : "); scanf("%lf%lf%lf",&x1,&x2,&x3); printf("\n\t------------------------------------------"); printf("\n\tx1 \t\tx2 \t\tx3"); printf("\n\t------------------------------------------"); fprintf(fp,"\n\tEnter Value of x1, x2 and x3 : %lf %lf %lf\n",x1,x2,x3); printf("\n\t%lf %lf %lf",x1,x2,x3); do { y1=X1(x2,x3); y2=X2(y1,x3); y3=X3(y1,y2); if(fabs(y1-x1)