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++ > Algorithms Code Examples

It evaluates the value of any polynomial of any degree, adds two poly

/* It evaluates the value of any polynomial of any degree, adds two poly and also multiplies them */ /*evaluates, adds ,multiplies two polynomials p & f to evaluate poly of deg > 20 manipulate const in size */ #include <iostream.h> using namespace std ; void polyadd(float*a,int dega,float*b,int degb,float*sum) {int i ; if(dega>=degb) {for (i=0;i<=dega;i++) sum[i]=a[i]+b[i] ;} if(degb >dega) {for(i=0;i<=degb;i++) sum[i]= a[i]+b[i] ; } } //******************** /*void polymult(float*a,int dega,float*b,int degb,float*mult) {int i,j; if(dega<=degb) for(i=0;i<=degb;i++) {for(j=0;j<=i;j++) mult[i]= mult[i] + a[j]*b[i-j] ; mult[i] = mult[i] +a[i+1]*b[i+1] ; } //mult[i] = mult[i] - a[0]*b[0] ; } if (degb<dega) {for(i=0;i<=dega;i++) for(j=0;j<=i;j++) mult[i]= mult[i] +a[j]*b[i-j]; mult[i] = mult[i] +a[i+1]*b[i+1] ;} }*/ //********************** double power(float*a,int n,float x) {float val=0,prod=1 ; int i ; for(i=0;i<=n;i++) {val = val+a[i]*prod; prod = prod*x ;} return val ; } //****************** double power(float*a,int n,float x); main() {const int size=20 ; int deg,j ; float a[size], x ; for(j=0;j<=size-1;j++) a[j] = 0.0 ; double ans ; cout<<"give the degree of the polynomiala "<<endl ; cin>>deg ; if(deg<0) cout<<"go learn ur textbook"<<endl ; if(deg>=0) { cout<<"give the value of the coefficients"<<endl ; for (j=0;j<=deg;j++) cin>>a[j] ; cout<<"give the value x"<<endl ; cin>>x ; ans = power(a,deg,x); cout<<"the value of p("<<x<<") = "<<ans <<endl ; } float b[size],sum[size],mult[size],y ; for(j=0;j<=size-1;j++) b[j] = sum[j] = mult[j] = 0 ; int dega,degb ; dega = deg ; /*cout<<"give the value of the coefficients"<<endl ; for (j=0;j<=dega;j++) cin>>a[j]; */ cout<<" give in the degree of poly b"<<endl ; cin>>degb ; cout<<"enter the values of the coefficient of polynomialb"<<endl ; for(j=0;j<=degb;j++) cin>>b[j] ; cout<<"give the value to be substituted in polynomialb"<<endl ; cin>>y ; ans = power(b,degb,y) ; cout<<"the value of f("<<y<<") ="<<ans<< endl ; cout<<"the value of f("<<x<<") ="<<power(b,degb,x)<<endl ; string decision ; cout<<"to you want to add or multiply a and b(type a or m) "<<endl ; cin>>decision ; if(decision =="a") { polyadd(a,dega,b,degb,sum); if (dega>=degb) {ans= power(sum,dega,x); cout<<"the answer after addition of p("<<x<<") +f("<<x<<") ="<<ans<<endl ;} if(degb>dega) {ans = power(sum,degb,x) ; cout<<"the answer after addition of p("<<x<<") +f("<<x<<") ="<<ans<<endl ; }} if (decision=="m") //{polymult(a,dega,b,degb,mult); //if (dega>=degb) {ans= power(a,dega,x)*power(b,degb,y) ; //cout<<"the answer of multiplication"<<ans<<endl ;} //if (degb>dega) //{ ans = power(mult,degb,x) ; cout<<"the answer after multiplication of p("<<x<<") *f("<<y<<") ="<<ans<<endl ;} return 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.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

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 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.

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++.

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.

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.

Relational operators for string. Performs the appropriate comparison operation between the string objects lhs and rhs. The functions use string::compare for the comparison. These operators are overloaded in header <string>. If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

#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.