Stats

Sunday, 23 December 2012

INHERITANCE PROGRAM EXAMPLE


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class student
{
protected:
int roll_no;
char name[20];
public:
void details()
{
cout<<"enter roll number and name of the student:\n";
cin>>roll_no>>name;
}
};
class subject: public student
{
protected:
int thm,prm;
public:
void tp()
{
cout<<"\ntheory marks: ";
cin>>thm;
cout<<"\npractical marks: ";
cin>>prm;
}
};
class sports
{
protected:
int spm;
public:
void sp()
{
cout<<"\nsports marks: ";
cin>>spm;
}
};
class result:public subject,public sports
{
protected:
int total;
public:
int t()
{
total=thm+prm+spm;
return total;
}
void res()
{
cout<<"\n\nName"<<"      "<<"Roll No."<<"   "<<"Theory"<<"   "
<<"Practical"<<"   "<<"Total"<<endl;
cout<<name<<"    "<<roll_no<<"         "<<thm<<"         "
<<prm<<"         "<<total;
}
};
void main()
{
result r;
r.details();
r.tp();
r.sp();
r.t();
r.res();
getch();
}

Output

24 Namrata

theory marks: 89

practical marks: 45

sports marks: 23


Name      Roll No.   Theory   Practical   Total
Namrata    24         89         45         157




Friday, 7 September 2012

Gauss Elimination Method


# include<stdio.h>
# include<conio.h>
void main()
{
 int i,j,k,n;
 float a[10][10],x[10];
 float s,p; 
 printf("Enter the number of equations : ");
 scanf("%d",&n) ;
 printf("\nEnter the co-efficients of the equations :\n\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("a[%d][%d]= ",i+1,j+1);
   scanf("%f",&a[i][j]);
  }
  printf("d[%d]= ",i+1);
  scanf("%f",&a[i][n]);
 }
 for(k=0;k<n-1;k++)
 {
  for(i=k+1;i<n;i++)
  {
   p = a[i][k] / a[k][k] ;
   for(j=k;j<n+1;j++)
    a[i][j]=a[i][j]-p*a[k][j];
  }
 }
 x[n-1]=a[n-1][n]/a[n-1][n-1];
 for(i=n-2;i>=0;i--)
 {
  s=0;
  for(j=i+1;j<n;j++)
  {
   s +=(a[i][j]*x[j]);
   x[i]=(a[i][n]-s)/a[i][i];
  }
 }
 printf("\nThe result is:\n");
 for(i=0;i<n;i++)
  printf("\nx[%d]=%f",i+1,x[i]);
 getch();

}

gauss seidel method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.01

void main() {
  int i, j, k, n;
  float a[10][10], x[10];
  float sum, temp, error, big;
  printf("Enter the number of equations: ");
  scanf("%d", & n);
  printf("Enter the co-efficients of the equations: \n");
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n + 1; j++) {
      printf("a[%d][%d]= ", i, j);
      scanf("%f", & a[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    x[i] = 0;
  }
  do {
    big = 0;
    for (i = 1; i <= n; i++) {
      sum = 0;
      for (j = 1; j <= n; j++) {
        if (j != i) {
          sum = sum + a[i][j] * x[j];
        }
      }
      temp = (a[i][n + 1] - sum) / a[i][i];
      error = fabs(x[i] - temp);
      if (error > big) {
        big = error;
      }
      x[i] = temp;
      printf("\nx[%d] =%f", i, x[i]);
    }
    printf("\n");
  }
  while (big >= e);
  printf("\n\nconverges to solution");
  for (i = 1; i <= n; i++) {
    printf("\nx[%d]=%f", i, x[i]);
  }
  getch();
}

Enter the number of equations: 3
Enter the co-efficients of the equations:
a[1][1]= 2
a[1][2]= 1
a[1][3]= 1
a[1][4]= 5
a[2][1]= 3
a[2][2]= 5
a[2][3]= 2
a[2][4]= 15
a[3][1]= 2
a[3][2]= 1
a[3][3]= 4
a[3][4]= 8

x[1] =2.500000
x[2] =1.500000
x[3] =0.375000

x[1] =1.562500
x[2] =1.912500
x[3] =0.740625

x[1] =1.173437
x[2] =1.999688
x[3] =0.913359

x[1] =1.043477
x[2] =2.008570
x[3] =0.976119

x[1] =1.007655
x[2] =2.004959
x[3] =0.994933

x[1] =1.000054
x[2] =2.001995
x[3] =0.999474


converges to solution
x[1]=1.000054
x[2]=2.001995
x[3]=0.999474

Thursday, 2 August 2012

C program for electricity bill


#include<iostream>
using namespace std;
#define cost1 0.60
#define cost2 0.80
#define cost3 0.90
#define min 50
#include<conio.h>
void main()
{
char name[10][20];
float amount[10];
int units[10],n;

cout<<"Enter Number of Customers: ";
cin>>n;

cout<<"Enter Names and Number of Units Consumed\n";
for(int i=0;i<n;i++)
{
cout<<"-------------------------\n";
cout<<"Enter Customer Name: ";
cin>>name[i];
cout<<"Enter Number of units= ";
cin>>units[i];
cout<<"-------------------------\n";

if(units[i]<=100)
{
amount[i]=(units[i]*cost1)+min;
}
else if(units[i]>100&&units[i]<=300)
{
amount[i]=(((units[i]-100)*(cost2))+(100*cost1))+min;

}
else if(units[i]>300)
{
amount[i]=(((units[i]-300)*(cost3))+(200*cost2)+(100*cost1))+min;
amount[i]=amount[i]+(amount[i]*(15.0/100.0));
}
}

cout<<"**************BILL**************\n";
cout<<"Name\t\t"<<"Units\t\t"<<"Amount\n";
for(int i=0;i<n;i++)
{
cout<<name[i]<<"\t\t"<<units[i]<<"\t\t"<<"Rs."<<amount[i];
cout<<"\n";
}
getch();
}









C program to implement Newton Raphson method


1)F(x)=x^2-3x+2

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define e 0.001
#define F(x) (2*x)-3
float frac(float a)
{
       float f1;
       f1=a*a-3*a+2;
       return f1;
}
int main()
{
       float x1,x2,f1=0,f2,er,d;
       printf("F(x) = x^2-3x+2\n\n");
       printf("Enter the value of x1: ");
       scanf("%f",&x1);
       printf("\nx1 = %f",x1);
       printf("\n________________________________________________________________________________\n");
        
       printf("     x1      |       x2      |      f1       |       f'1      |  |(x2-x1)/x2|  |  \n");
       printf("--------------------------------------------------------------------------------\n");
       do
       {
              f1=frac(x1);
              d=F(x1);
              x2=x1-(f1/d);
              er=fabs((x2-x1)/x2);
           printf("  %f   |    %f   |     %f  |     %f  |      %f  |   \n",x1,x2,f1,d,er);
              x1=x2;
       }
       while(er>e);
       printf("--------------------------------------------------------------------------------\n\n");
       printf("\n  Root of the equation is: %f",x2);
       getch();
}


Sunday, 29 July 2012


1) F(x) = X^3 – X - 1

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.001
float frac(float a)
{
       float f1;
       f1=a*a*a-a-1;
       return f1;
}
void main()
{
       float x1,x2,x0,f0,f1,f2;
       do
       {
       printf("Enter values of x1 and x2\n");
       scanf("%f%f",&x1,&x2);
       printf("\nx1 = %f\n",x1);
       printf("x2 = %f\n",x2);
       f1=frac(x1);
       f2=frac(x2);
       }
       while((f1*f2)>0);
       printf("_______________________________________________________________________________________\n");
        
        printf("     x1      |      x2     |      x0     |      f1       |      f2      |      f0     |\n");
        printf("---------------------------------------------------------------------------------------\n");
        
       do
       {
              f1=frac(x1);
              f2=frac(x2);
              x0=(x1+x2)/2;
              f0=frac(x0);
              if((f1*f0)<0)
              {
                     printf("  %f   |  %f   |  %f   |  %f    |  %f    |   %f  | \n",x1,x2,x0,f1,f2,f0);
                     x2=x0;
                     f2=f0;
              }
              else
              {
                     printf("  %f   |  %f   |  %f   |  %f    |  %f    |  %f  | \n",x1,x2,x0,f1,f2,f0);
                     x1=x0;
                     f1=f0;
                    
              }
       }
       while(fabs(x2-x1)>e);
       printf("---------------------------------------------------------------------------------------\n\n");
       printf("\n  Root of the equation is: %f",x0);
       getch();
}