C C++ Programming
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Thursday, April 27, 2017
Tuesday, April 18, 2017
C++ code for Derivative using Newton Backward Difference Formula
This is the solution for finding Derivative using Newton's Backward Difference Formula in C++
#include <iostream> #include <conio.h> using namespace std; class DerivativeBackward { public: void askN(); void askX(); void askF(); void askXX(); void forwardTable(); void calcd1(); void calcd2(); void findH(); void solve(); void fillDelF(); void findS(); private: double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2; int n; double h,s; }; void DerivativeBackward::askX() { cout << endl; for(int i = 0; i<n; i++ ) { cout << "ENter X[" << i << "] : "; cin >> x[i]; } cout << endl; } void DerivativeBackward::askF() { for(int j = 0; j<n; j++ ) { cout << "ENter F[" << j << "] : "; cin >> f[0][j]; } cout << endl; } void DerivativeBackward::askXX() { cout << "Enter X for which the value is to be found: "; cin >> XX; } void DerivativeBackward::forwardTable() { for(int i = 1; i < n; i++) { for(int j = 0; j< n-i;j++) { f[i][j] = (f[i-1][j+1]-f[i-1][j]); if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0) { f[i][j] = 0; } } } cout << endl; cout << "Sn\tXi\tf(Xi)\t"; for(int i = 0; i <n-1;i++) { cout << i+1 << " diff\t"; } cout << endl; for(int i = 0; i < n; i++) { cout <<i+1 <<"\t" << x[i]<< "\t"; for(int j = 0; j< n-i;j++) { cout << f[j][i] << "\t"; } cout << endl; } } void DerivativeBackward::findH() { h = x[1] - x[0]; cout << "h = " <<h; } void DerivativeBackward::findS() { s = (XX - x[n-1])/h; cout << "s = " <<s; } void DerivativeBackward::solve() { findH(); findS(); fillDelF(); calcd1(); calcd2(); cout <<endl << "The value of f1 :" << f1; cout <<endl << "The value of f2 :" << f2; cout << endl << endl; } void DerivativeBackward::fillDelF() { for(int i = 1;i<10;i++) { if(i<n) delF[i] = f[i][n-i-1]; else delF[i] = 0; } for(int i = 1;i<10;i++) { cout<< delF[i]; } } void DerivativeBackward::calcd1() { f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s +1 ) * delF[2] + 1 / (6.0) * (3*s*s + 6 *s +2)*delF[3] + 1 /(24.0) *( 4*s*s*s+18*s*s+22*s+6)*delF[4]); } void DerivativeBackward::calcd2() { f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s+6) * delF[3] + 1/24.0 *(12*s*s+36*s+22)*delF[4]); } void DerivativeBackward::askN() { cout << "Enter the number of values: "; cin >> n; } int main() { DerivativeBackward d1; d1.askN(); d1.askX(); d1.askF(); d1.askXX(); d1.forwardTable(); d1.solve(); }
C++ code for Derivative using Newton Forward Difference Formula
This is the solution for finding Derivative using Newton Forward Difference Formula in C++.
#include <iostream> #include <conio.h> using namespace std; class DerivativeForward { public: void askN(); void askX(); void askF(); void askXX(); void forwardTable(); void calcd1(); void calcd2(); void findH(); void solve(); void fillDelF(); void findS(); private: double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2; int n; double h,s; }; void DerivativeForward::askX() { cout << endl; for(int i = 0; i<n; i++ ) { cout << "ENter X[" << i << "] : "; cin >> x[i]; } cout << endl; } void DerivativeForward::askF() { for(int j = 0; j<n; j++ ) { cout << "ENter F[" << j << "] : "; cin >> f[0][j]; } cout << endl; } void DerivativeForward::askXX() { cout << "Enter X for which the value is to be found: "; cin >> XX; } void DerivativeForward::forwardTable() { for(int i = 1; i < n; i++) { for(int j = 0; j< n-i;j++) { f[i][j] = (f[i-1][j+1]-f[i-1][j]); if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0) { f[i][j] = 0; } } } cout << endl; cout << "Sn\tXi\tf(Xi)\t"; for(int i = 0; i <n-1;i++) { cout << i+1 << " diff\t"; } cout << endl; for(int i = 0; i < n; i++) { cout <<i+1 <<"\t" << x[i]<< "\t"; for(int j = 0; j< n-i;j++) { cout << f[j][i] << "\t"; } cout << endl; } } void DerivativeForward::findH() { h = x[1] - x[0]; } void DerivativeForward::findS() { s = (XX - x[0])/h; } void DerivativeForward::solve() { findH(); findS(); fillDelF(); calcd1(); calcd2(); cout <<endl << "The value of f1 :" << f1; cout <<endl << "The value of f2 :" << f2; cout << endl << endl; } void DerivativeForward::fillDelF() { for(int i = 1;i<10;i++) { if(i<n) delF[i]=f[i][0]; else delF[i] = 0; } } void DerivativeForward::calcd1() { f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s -1 ) * delF[2] + 1 / (6.0) * (3*s*s - 6 *s +2)* delF[3] + 1 /(24.0) *( 4*s*s*s-18*s*s+22*s-6)*delF[4]); } void DerivativeForward::calcd2() { f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s-6) * delF[3] + 1/24.0 *(12*s*s-36*s+22)*delF[4]); } void DerivativeForward::askN() { cout << "Enter the number of values: "; cin >> n; } int main() { DerivativeForward d1; d1.askN(); d1.askX(); d1.askF(); d1.askXX(); d1.forwardTable(); d1.solve(); }
C++ code showing Hierarchical Inheritance to calculate Area
This is the solution for T.U.2068 Q.N.3
#include <iostream> using namespace std; class Shape { protected: int A; public: Shape() { A=0; } void show(); }; class Triangle:public Shape { int h, b; public: void Geth(); void Getb(); void Showhb(); void CalTriA(); }; class Rectangle:public Shape { int l, b; public: void Getl(); void Getb(); void Showlb(); void CalRecA(); }; void Shape::show() { cout << endl << "Area=" << A << endl ; } void Triangle::Geth() { cout << endl << "Enter height of the triangle:" ; cin >> h ; } void Triangle::Getb() { cout << endl << "Enter base of the triangle:" ; cin >> b ; } void Triangle::Showhb() { cout << endl << "Triangle:" << endl ; cout << "Height=" << h << endl << "Base=" << b << endl ; } void Triangle::CalTriA() { A = (h*b)/2; } void Rectangle::Getl() { cout << endl << "Enter length of Rectangle:" ; cin >> l ; } void Rectangle::Getb() { cout << endl << "Enter breadth of Rectangle:" ; cin >> b ; } void Rectangle::Showlb() { cout << endl << "Rectangle" << endl ; cout << "Length=" << l << endl << "Breadth=" << b << endl ; } void Rectangle::CalRecA() { A = l*b ; } int main() { Triangle T; T.Geth(); T.Getb(); Rectangle R; R.Getl(); R.Getb(); T.Showhb(); T.CalTriA(); T.show(); R.Showlb(); R.CalRecA(); R.show(); return 0; }
C++ code showing Inheritance and Data Conversion
This is the solution for T.U. 2067 Q.N.3
#include <iostream> using namespace std; class clock { protected: int hr, min, sec ; public: clock() { hr = min = sec = 0; } clock(int x) { hr = min = sec = x ; } void Show(); }; class wall_clock:public clock { int TotalTime; public: wall_clock():clock() { TotalTime = 0 ; } wall_clock(int x):clock(x) { } void Add(int, int, int); void ShowTime(); }; void clock::Show() { cout << endl << "Initially" << endl ; cout << endl << hr << ":" << min << ":" << sec << endl ; } void wall_clock::Add(int a, int b, int c) { hr+=a; min+=b; sec+=c; } void wall_clock::ShowTime() { if(sec>60) { min += sec/60; sec = sec%60; } if(min>60) { hr += min/60 ; min = min%60; } cout << endl << hr << ":" << min << ":" << sec << endl ; } int main() { wall_clock WC1,WC2; WC1 = 0 ; WC2 = 0 ; WC1.Show(); WC2.Show(); WC1.Add(1,17,65); WC2.Add(4,45,49); WC1.ShowTime(); WC2.ShowTime(); return 0; }
C++ code showing Hierarchical Inheritance
This is the solution for T.U. 2066 Q.N. 3
#include <iostream> using namespace std; class Student { protected: float avg; public: void Show(); }; class ComputerScience:public Student { float OOPL, OS, NM ; public: void GetMarks1(); void ShowMarks1(); void CalAvg1(); }; class Mathematics:public Student { float Calculus, LA, Geometry ; public: void GetMarks2(); void ShowMarks2(); void CalAvg2(); }; void Student::Show() { cout << endl << "Average=" << avg << endl ; } void ComputerScience::GetMarks1() { cout << endl << "Enter marks of:" << endl ; cout << endl << "OOPL:" ; cin >> OOPL ; cout << endl << "NM:" ; cin >> NM ; cout << endl << "OS:" ; cin >> OS ; } void ComputerScience::ShowMarks1() { cout << endl << "Computer Science:" << endl ; cout << endl << "OOPL=" << OOPL << endl << "NM=" << NM << endl << "OS=" << OS << endl ; } void ComputerScience::CalAvg1() { avg = (OOPL + NM + OS)/3 ; } void Mathematics::GetMarks2() { cout << endl << "Enter Marks of:" << endl ; cout << endl << "Calculus:" ; cin >> Calculus ; cout << endl << "LA:" ; cin >> LA ; cout << endl << "Geometry:" ; cin >> Geometry ; } void Mathematics::ShowMarks2() { cout << endl << "Mathematics:" << endl ; cout << endl << "Calculus=" << Calculus << endl << "LA=" << LA << endl << "Geometry=" << Geometry << endl ; } void Mathematics::CalAvg2() { avg = (Calculus + LA + Geometry)/3 ; } int main() { ComputerScience CS; CS.GetMarks1(); CS.CalAvg1(); Mathematics M; M.GetMarks2(); M.CalAvg2(); CS.ShowMarks1(); CS.Show(); M.ShowMarks2(); M.Show(); return 0; }
Monday, April 17, 2017
C++ code for Unary Operator Overloading(--) using Friend Function
This is the solution that increases an integer value by 1 by overloaded operator using Friend Function (T.U. 2072)
#include <iostream> using namespace std; class Decrement { int a; public: Decrement() { a=10; } Decrement(int x) { a=x; } friend operator++(Decrement &I,int); //Use & while using Friend Function friend operator++(Decrement &I); void print(); }; operator++(Decrement &I,int) { I.a--; } operator++(Decrement &I) { --I.a; } void Decrement::print() { cout << endl << "a=" << a << endl ; } int main() { Decrement D1; D1.print(); D1--; D1.print(); Decrement D2(100); D2.print(); --D2; D2.print(); return 0; }
Subscribe to:
Posts (Atom)