Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Mathematics Code Examples

Jacobi itterative and gauss seidal method to solve roots

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/* Jacobi itterative and gauss seidal method to solve roots this is a program from numerical to calculate the root of the given system ,it will check its conditions and then perform the operation on that system,esle it will tell u that system is not diagonally dominent ,,,,in this program the functions used can be used in other program ,, */ #include<iostream.h> #include<conio.h> #include<math.h> #include<iomanip.h> float a1[4],a2[4],a3[4]; /* Array declaration */ void show(); /* function declaration */ void getdata(float [],float [],float []); /* // // // */ void display(float [],float [],float []); /* // // // */ int diagonally(); void swap(float [],float []); /* // // // */ void jacobi(float [],float [],float []); /* // // // */ void gauss(float [],float [],float []); void answer(); /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^* / /*....................MAIN FUNCTION OF METHOD............*/ void main() /* main function definition */ { int count=4; /* { main function body} */ clrscr(); cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^"<<endl; cout<<"..................THIS IS VALID ONLY FOR JACOBI ITTERATIVE METHOD............."<<endl; cout<<"___________________________________________________________________ ___________"<<endl; show(); /* function calling */ getdata(a1,a2,a3); /* function calling */ clrscr(); cout<<endl<<endl; display(a1,a2,a3); /* function calling */ count=diagonally(); /* function calling */ switch (count) { case 0: { answer(); /* function calling */ break; } case 2: {display(a1,a2,a3); answer(); /* function calling */ break; } default: { cout<<"SORRY;YOUR SYSTEM IS NOT DIAGONALLY DOMINENT"; break; } } getch(); } ////////////////////////Function To Check Diagonality//////////////////////// int diagonally() { int f=4,g=4 ,h=4; int count=0; float temp[4]; if(fabs(a1[0])<(fabs(a1[1])+fabs(a1[2]))) {count++; f=1;} if(fabs(a2[1])<(fabs(a2[0])+fabs(a2[2]))) {count++; g=2;} if(fabs(a3[2])<(fabs(a3[0])+fabs(a3[1]))) {count++; h=3;} if(f==1&&g==2&&h==4) swap(a1,a2); /* function calling */ if(f==1&&h==3&&g==4) swap(a1,a3); /* function calling */ if(g==2&&h==3&&f==4) swap(a2,a3); /* function calling */ return(count); } ////////////////////////////Function for jacobi itterative method///////////////////////// void jacobi(float a[],float b[],float c[]) /*function definition */ { float temp[3]; long float j1,j2,j3; cout<<endl<<"please enter the initial guess:"<<endl; cout<<endl<<"X(1) ="; cin>>j1; cout<<endl<<"X(2) ="; cin>>j2; cout<<endl<<"X(3) ="; cin>>j3; cout<<"------------------------------------------------------------------- ----"; cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::"; cout<<"___________________________________________________________________ ____"; cout<<endl<<endl<<"iterrations #"<<" "<<" X(1)"<<" X(2)"<<" X(3)"; cout<<endl<<" 0"<<setw(17)<<j1<<setw(15)<<j2<<setw(14)<<j3; cout<<endl; for(int s=1;s<=20;s++) { temp[0]=j1;temp[1]=j2;temp[2]=j3; j1=(a[3]-a[1]*temp[1]-a[2]*temp[2])/a[0]; j2=(b[3]-b[0]*temp[0]-b[2]*temp[2])/b[1]; j3=(c[3]-c[0]*temp[0]-c[1]*temp[1])/c[2]; cout<<" "<<s<<setw(17)<<j1<<setw(15)<<j2<<setw(14)<<j3<<endl; if(j1==temp[0]&&j2==temp[1]&&j3==temp[2]) break; } } //////////////////////////Function Of Swaping//////////////////////////////////// void swap(float a[],float b[]) /* function definition */ { float temp[4]; cout<<"------------------------------------------------------------------- -------------"<<endl; cout<<".....................Your System Is Not Diagonally Dominent....................."<<endl; cout<<"___________________Now It Have To Become Diagonally Dominent As__________________"<<endl; for(int i=0;i<4;i++) { temp[i]=a[i]; a[i]=b[i]; b[i]=temp[i]; } } //////////////////////Function To Show Equations Form//////////////////////// void show() /* function definition */ { cout<<endl; cout<<"__________________Your Equations Will Be Of The Form Like This_________________"<<endl; cout<<endl<<endl<<endl; cout<<" a(11)X1 + a(12)X2 + a(13)X3= b(1)"<<endl <<" a(21)X1 + a(22)X2 + a(23)X3= b(2)"<<endl <<" a(31)X1 + a(32)X2 + a(33)X3= b(3)"<<endl; } //////////////////////Function To Get Data From User///////////////////////// void getdata(float a[],float b[],float c[]) /* function definition */ { for(int i=0;i<3;i++) { cout<<"a(1"<<i+1<<")="; cin>>a[i]; cout<<endl; } cout<<"b(1) ="; cin>>a[3]; cout<<endl; for(int j=0;j<3;j++) { cout<<"a(2"<<j+1<<")="; cin>>b[j]; cout<<endl; } cout<<"b(2) ="; cin>>b[3]; cout<<endl; for(int k=0;k<3;k++) { cout<<"a(3"<<k+1<<")="; cin>>c[k]; cout<<endl; } cout<<"b(3) ="; cin>>c[3]; cout<<endl; } ///////////////////////////Function To display Equations///////////////////// void display(float a[],float b[],float c[]) /* function definition */ { cout<<"------------------------------------------------------------------- -------"; cout<<" ::::::::::::::::Your Given System Is Like This:::::::::::::::: "<<endl<<endl; cout<<" "<<a[0]<<"X(1) + "<<a[1]<<"X(2) + "<<a[2]<<"X(3) = "<<a[3]<<endl <<" "<<b[0]<<"X(1) + "<<b[1]<<"X(2) + "<<b[2]<<"X(3) = "<<b[3]<<endl <<" "<<c[0]<<"X(1) + "<<c[1]<<"X(2) + "<<c[2]<<"X(3) = "<<c[3]; cout<<endl<<endl; } ///////////////////////////Function of Gauss Seidal Method//////////////////// void gauss(float a[],float b[],float c[]) /*function definition */ { float temp[3]; long float j1,j2,j3; cout<<endl<<"please enter the initial guess:"<<endl; cout<<endl<<"X(1) ="; cin>>j1; cout<<endl<<"X(2) ="; cin>>j2; cout<<endl<<"X(3) ="; cin>>j3; cout<<"------------------------------------------------------------------- ----"; cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::"; cout<<"___________________________________________________________________ ____"; cout<<endl<<endl<<"iterrations #"<<" "<<" X(1)"<<" X(2)"<<" X(3)"; cout<<endl<<" 0"<<setw(17)<<j1<<setw(15)<<j2<<setw(14)<<j3; cout<<endl; for(int s=1;s<=20;s++) { temp[0]=j1;temp[1]=j2;temp[2]=j3; j1=(a[3]-a[1]*j2-a[2]*j3)/a[0]; j2=(b[3]-b[0]*j1-b[2]*j3)/b[1]; j3=(c[3]-c[0]*j1-c[1]*j2)/c[2]; cout<<" "<<s<<setw(17)<<j1<<setw(15)<<j2<<setw(14)<<j3<<endl; if(j1==temp[0]&&j2==temp[1]&&j3==temp[2]) break; } } ///////////////// FUNCTION OF TAKING GAUSS OR JACOBI ////////////////// void answer() { char option; do { cout<<"PRESS [G] FOR GAUSS SEIDAL METHOD: "<<endl <<"AND"<<endl<<"PRESS [J] FOR JACOBI ITTERATIVE METHOD:"<<endl; option=getche(); switch (option) { case 'j': { cout<<setw(25)<<"----------BY JACOBI ITTERATIVE METHOD----------"<<endl; jacobi(a1,a2,a3); /* function calling */ break; } case 'g': { cout<<setw(25)<<"----------BY GAUSS SEIDAL METHOD----------"<<endl; gauss(a1,a2,a3); /* function calling */ break; } default: { cout<<"-------------YOUR OPTION IS NOT CORRECT TRY AGAIN-------------"<<endl; break; } } }while(getche()!=' '); }

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Set field width. Sets the field width to be used on output operations. The C++ function std::setw behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). It is used to sets the field width to be used on output operations. This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set. This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.

Compute absolute value. Returns the absolute value of x: |x|. The C or C++ library function double fabs(double x) returns the absolute value of x. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double (defined for T being any integral type). Function returns the absolute value of x.

This function waits for any character input from keyboard. And, it will also echo the input character on to the output screen. The getch() function is very useful if you want to read a character input from the keyboard. Like getch(), getche() is also character input functions. It is unformatted input function meaning it does not allow user to read input in their format. Difference between getch() and getche() is that getche() echoes pressed character. getche() also returns character pressed like getch(). It is also defined in header file conio.h.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

Construct Binary Search tree for the unsorted data array. 'Search the element' starting from the Root of the Tree. Proceed with the search by comparing an element to the data of node

Heap sort is comparison based algorithm. It's selection sort sample. The time complexity is O(n*log(n)). Build a max heap using the given data element. And then Delete the root node

'C++ Program' to find the minimum spanning tree of the given graph. Kruskal's algorithm is a greedy algorithm in graph theory that finds a "Minimum Spanning Tree" for a connected

Algorithm finds the median of 2 sorted arrays using binary search approach. Takes the input of 'n' Data Elements of both the arrays. Using decrease, conquer method find the combined