/ C Program to execute Gauss Jacobi Iteration Method #include <stdio.h> #include <conio.h> #include <math.h> #include<windows.h> /* declaration of gotoxy function */ void gotoxy(short x, short y); /* definition of gotoxy function */ void gotoxy(short x, short y) { COORD pos ={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } float f(float x,float y,float z) { return ((4-2*z+y)/10); //x=(4-2*z+y)/10 } float s(float x,float y,float z) { return ((3-x+z)/10); //y=(3-x+z)/10 } float t(float x,float y,float z) { return ((7-2*x-3*y)/20); //z=(7-2*x-3*y)/20 } int main() { float x0,y0,z0,x1=0,y1=0,z1=0,tempx,tempy,tempz,error,e1,e2,e3; int iteration=0; printf("\t\t\tGauss Jacobi Method\n\n"); printf("Enter Tolerable Error -> "); scanf("%f",&error); printf("\nEnter initial values:\n"); printf("\nEnter First value -> "); scanf("%f",&x0); printf("Enter Second value -> "); scanf("%f",&y0); printf("Enter Third value -> "); scanf("%f",&z0); printf("\n******************"); /* Creating Header of the table */ printf("\nCount"); gotoxy(12,10); printf("x"); gotoxy(28,10); printf("y"); gotoxy(44,10); printf("z\n"); do { tempx=x1; tempy=y1; tempz=z1; /Putting initial values in the Equations/ x1=f(x0,y0,z0); y1=s(x0,y0,z0); z1=t(x0,y0,z0); /* Printing the values in table */ printf("%d\t%f\t%f\t%f\n",iteration,x1,y1,z1); /* Error */ e1 = fabs(tempx-x1); e2 = fabs(tempy-y1); e3 = fabs(tempz-z1); iteration++; /* Set values for next iteration */ x0=x1; y0=y1; z0=z1; } while(fabs(e1)>error && fabs(e2)>error && fabs(e3)>error); printf("\n\n*****************\n"); printf("--------------------"); printf("\nSolution:\n"); printf("\nx=%f\ny=%f\nz=%f\n",x1,y1,z1); printf("Iteration=%d\n",iteration); printf("--------------------"); getch(); }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}