program to find average of an array using pointer

/*Write a program that reads a group of numbers from the user
and places them in an array of type float.
Once the numbers are stored in the array,
the program should average them and print the result.
Use pointer notation wherever possible*/

/*
first of all we enter the num of elements and then user will enter elements value,and then calculate average using pointer*/

#include <iostream>
using namespace std;
class Avg
{
public:
int n,i;
float arr[50],sum=0,average;
void get()
{
cout<<"enter the number of elements:";
cin>>n;
cout<<"enter elements:\n";
for(i=0;i<n;i++)
{
cin>>arr[i];
}

}
void show()
{
for(i=0;i<n;i++)
{
sum+= arr[i];
}
average=sum/n;
cout<<"average of the above entered number is:"<<average;
}
};
int main()
{
Avg *obj;
obj->get();
obj->show();
return 0;
}
Output

c++ program for addition and multiplication of two matrices using classes and object using operator overloading

c++ program for addition and multiplication of  two matrices using classes and object using operator overloading:

addittion and multiplication of two matrixes involves for building a software wich can add or  multiply two matrices and give the result: program is given below


solution

#include <iostream>
using namespace std;
class function
{
public:
int a[10][10],i,j,n,m,k;

void input()
{
cout<<"enter order of matrix in m by n:";
cin>>m>>n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
function operator +(function obj2)
{
function temp;
temp.n=n;
temp.m=m;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
temp.a[i][j]=a[i][j]+obj2.a[i][j];
}
}
return temp;
}
function operator *(function obj2)
{
function temp;
temp.n=n;
temp.m=m;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
temp.a[i][j]=0;
for(k=0;k<n;k++)
temp.a[i][j]+=a[i][k]*obj2.a[k][j];
}
return temp;
}
void show()
{
for(i=0;i<m;i++)
{
for( j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
};
int main()
{
function obj,obj2,result;
obj.input();
obj2.input();
result=obj+obj2;
cout<<"matrix 1 is:\n";
obj.show();
cout<<"matrix 2 is:\n";
obj2.show();
cout<<"sum of matrix :\n";
result.show();
result=obj*obj2;
cout<<"product of matrixes is:\n";
result.show();
return 0;
}

output:

Create a class account that stores customer name, account number and type of account.

Create a class account that stores customer name, account number and type of account. Fromthis derive the classes cur_acct and sav_acct to make them more specific to theirrequirements. Include necessary member functions in order to achieve the following tasks:a. Accept deposit from a customer and update the balance.b. Display the balance.c. Compute and deposit interest.d. Permit withdrawal and update the balance.e. Check for the minimum balance, impose penalty, necessary, and update the balance.


#include<iostream>
#include<math.h>
using namespace std;
class account
{
public:
char cust_name[50],type;
long acc_no,balance;
void get()
{
cout<<"\nenter customer name: ";
cin>>cust_name;
cout<<"enter account number:";
cin>>acc_no;
cout<<"enter types s for saving and c for current:";
cin>>type;
cout<<"enter balance:";
cin>>balance;
}
void display()
{
cout<<"\n Customer Name: "<<cust_name;
cout<<"\n Account Number: "<<acc_no;
cout<<"\n Type: "<<type;
cout<<"\n Balance: "<<balance;
}
void deposit()
{
int amt;
cout<<"\nenter the amount which you want to deposit:";
cin>>amt;
balance+=amt;
}
};
class sav_acct:public account
{
public:
int intr;
void comp_intrest()
{
int t1,r1=10;
intr=balance*pow(1+r1/12,t1);
cout<<intr;
balance+=intr;
}
void withdraw()
{
int amt;
cout<<"\nenter the amount which do you want to withdraw:";
cin>>amt;
if(balance>amt)
balance-=amt;
else
cout<<"\namount can't be withdrawn due to insufficient balance";
}
};
class curr_acct:public account
{
public:
void min_bal()
{
if(balance<500)
{
cout<<"\npenality imposed: \nnew balance is:"<<balance-50;
}
else
{
cout<<"\nno penalty imposed";
}
}
void withdraw()
{
int amt;
cout<<"\nenter amount to be withdrawn:";
cin>>amt;
if(amt>balance)
{
cout<<"amount can't be withdrawn due to insufficient balance";
}
else
{
balance-=amt;
}
}
};
int main()
{
sav_acct s1;
curr_acct c1;
c1.get();
c1.display();
c1.deposit();
c1.display();
c1.withdraw();
c1.display();
c1.min_bal();
s1.get();
s1.display();
s1.deposit();
s1.comp_intrest();
s1.display();
s1.withdraw();
s1.display();
}

output:



cpp progrom to check pallindrome of a string

cpp progrom to check entered string is pallindrome or not:

solution:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str;
cout<<"enter the string to check it is pallindrome or not:";
cin>>str;
int flag=0;
int l =str.size();
int i;
for(i=0;i<l;i++)
{
if(str[i]!=str[l-i-1])
flag =1;
}
if(flag==0)
{
cout<<"string is pallindrome";

}
else
{
cout<<"string is not pallindrome";
}
}

output:


program to check that string entered are anagram or not in cpp

program to check that string entered are anagram or not in cpp:

solution:

#include <iostream>
#include<string>
using namespace std;
int main() 
{
  char a[100],b[100];
  int flag=0;
  cout<<"enter ist string:";
  cin>>a;
  cout<<"enter 2nd string:";
  cin>>b;
  
    int first[26]={0},second[26]={0},i=0;
    while (a[i]!='\0')
    {
      first [a[i]-'a']++;
      i++;

    }
    i=0;
    while (b[i]!='\0')
    {
      second[b[i]-'a']++;
      i++;
    }
    for(i=0;i<26;i++)
    {
      if (first[i]!=second[i])
      {
       flag=1;
      }
    }

  if(flag==0)
  {
    cout<<"string entered are anagram";

  }
  else
  {
    cout<<"not anagram";
  }

}

Output:


Program to understand the concept of new and delete operator. in cpp

/*Program to understand the concept of new and delete operator.*/


#include <iostream>
using namespace std;
class opp
{
  public:
  opp()
  {
    cout<<"new called";
  }
  ~opp()
  {
    cout<<"\ndelete called";
  }
};
int main()
{
   opp *obj=new opp();
   delete (obj);
   return 0;
}

Output:


wap for batsman and its score displaying in cpp


wap for batsman and its score displaying in cpp



#include <iostream>
using namespace std;
class person
{
  protected:
  char name[50];
  int age;
  public:
  void get_person_data()
  {
    cin>>name;
    cin>>age;
  }
  void show_person_data()
  {
    cout<<name<<"\t";
    cout<<age<<"\t";
  }
};
class batsman:public person

{
  protected:
   int runs_taken;
   float batting_avg;
   int odi;
   int no_of_half_century;
   int no_of_century;
   float strike_rate;
   public:
   void get_details();
   void show_details();

};
void batsman::get_details()
{
  get_person_data();
  cin>>runs_taken>>batting_avg>>odi>>no_of_half_century>>no_of_century>>strike_rate;


}
void batsman::show_details()
{
  show_person_data();
 cout<<runs_taken<<"\t"<<batting_avg<<"\t"<<odi<<"\t"<<no_of_half_century<<"\t"<<no_of_century<<"\t"<<strike_rate;
}


int main()
{
   batsman b;
   b.get_details();
   b.show_details();
   return 0;
}

Output:





c++ program to find mean of two number

c++ program to find mean of two number

#include <iostream>
using namespace std;
class mean
{
  private:
  int a;
  int b;
  friend float avg(mean&m1);
  public:
  void get()
  {
    cin>>a>>b;
  }
};
float avg(mean&m1)
{
  return((m1.a+m1.b)/2);
}
int main()
{
  mean m1;
  m1.get();
  cout<<"mean is:"<< avg(m1);
}

Output:



wap to swap two number in cpp

#include <iostream>
using namespace std;

void swap(int&, int&);

int main()
{
    int a = 1, b = 2;
    cout << "Before swapping \n";
    cout << "a = " << a  << "\nb = " << b;
   
    swap(a, b);
    cout << "\nAfter swapping\n"<< "a = " << a << "\nb = " << b << endl;
   
   return 0;
}

void swap(int& n1, int& n2) {
    int temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
}

Output:

order of constructor and destructor in multilevel inheritance




#include <iostream> using namespace std; class base { public: base() { cout<<"base 1 constructor called\n"; } ~base() { cout<<"base 1 destructor called\n"; } }; class base2 :public base { public: base2() { cout<<"base 2 constructor called\n"; } ~base2() { cout<<"base 2 destructor called\n"; } }; class base3 :public base2 { public: base3() { cout<<"base 3 constructor called\n"; } ~base3() { cout<<"base 3 destructor called\n"; } }; int main() { base3 obj; }

Output:


concept of virtual inheritance in c++ :

concept of virtual inheritance in c++ :

Q.input students roll number name and marks and display total marks:

solution:

#include <iostream>
using namespace std;
class student
{
  protected :
  int roll;
  char name[25];
  public:
  void get()
  {
    cin>>roll>>name;
  }
  void show()
  {
    cout<<roll<<name;
  }
};
class test:virtual public student
{
  protected:
  int marks[3],i;
  public:
  void getmarks()
  {
    for(i=0;i<3;i++)
    {
      cin>>marks[i];
    }
  }
  int total()
{
  return(marks[0]+marks[1]+marks[2]);
}
};
class activities:virtual public student
  protected:
  int act[2],i;
  public:
  void getact()
  {
    for(i=0;i<2;i++)
    cin>>act[i];
  }
  int totalact()
  {
    return(act[0]+act[1]);
  }
};
class result:public test,public activities
{
  public:
  void show_total()
  {
    cout<<"result is:"<<total()+totalact();
  }
};

int main() 
{
  result r;
  r.get();
  r.getmarks();
  r.getact();
  r.show();
  r.show_total();
  return 0;

}

output:



Design a C++ Project on Travel Agency Travel management

Q. Design a C++ Project on Travel Agency Travel management
a) The main objective of the project travelling agency is to make avail to the customers all sorts of travelling services.
b) A host of services such as registration, display, search, modify etc are provided. In the registration step, the client has to provide his personal details.
c) In the option of display all the client information is read like name, phone, cost etc. in the search tab, if information of a particular client is required, then that be obtained. 


Solution:


#include <iostream>
#include<string.h>
using namespace std;
int main()
{
  char x,d,l,name[50],sname[50];
  long int phone;
  int i,opt,cost,day;
  int comp;
  cout<<"you are not a customer of our company!!\n do you want to register? enter y or n\n";
  cin>>x;
  if (x=='y')
  {
    cout<<"enter your name:";
    cin>>name;
    cout<<"enter your phone number:";
    cin>>phone;
    cout<<"registration successfull!!\n";   
  }
  cout<<"welcome "<<name;
  cout<<"\ndo you want to modify your info reply y or n:";
  cin>>d;
  if(d=='y')
  {
     cout<<"\nenter your name:";
    cin>>name;
    cout<<"\nenter your phone number:";
    cin>>phone;
    cout<<"\nmodification successfull!!\n";
   
  }
       cout<<"\nwhat type of car do you want? delux? or super delux?\ncharges for delux will be 5000 per day while that of delux is 10000 per day \nreply 1 for delux and 2 for super delux";
    cin>>opt;
    cout<<"\nenter number of days do you want to travel:";
    cin>>day;
    if(opt==1)
  {
    cost=day*5000;
  }
  else if(opt==2)
  {
    cost=day*10000;
 
  }
  cout<<"do you want to search reply y or n:";
  cin>>l;
  if(l=='y')
  {
    cout<<"enter name:";
    cin>>sname;
    comp=strcmp(name,sname);
    if(comp==0)
    {
      cout<<"match found";
    }
    else
    {
      cout<<"match not found";
    }

  }
  cout<<"displaying result:\n";
  cout<<"name:"<<name;
  cout<<"\nphone number:"<<phone;
  cout<<"\ntotal cost you have to pay:"<<cost;

}

Output:


cube of an integer and float using operator oerloading in cpp

cube of an integer and float using operator oerloading in cpp



#include <iostream>
using namespace std;
int cube(int);
float cube(float);
int main()
{
int a,x;
float b,y;
cout<<"enter an intege";
cin>>a;
x=cube(a);
cout << "cube of integer: " << x << endl;
cout << "Enter a float number\n";
   cin >>b;
y=cube(b);
 cout << "cube of floats: " << y << endl;
 
   return 0;
}
int cube(int x)
{
  int m;
  m=x*x*x;
  return m;

}
float cube(float y)
{
  float n;
  n=y*y*y;
  return n;

}

Output:


subtract of complex number using operator overloading

subtract of  complex number as well as integer using operator overloading in a single program

Solution:

#include <iostream>
using namespace std;
class sub
{
  public:
  int x;
 
  int A,B;
   int n=2;  //n=1 for numbers and n=2 for imaginary num
 
    void input1()
    {
 
      if(n==1)
      {
        cout<<"enter num";
        cin>>A;
      }
      if(n==2)
      {
        cout<<"enter real and imaginary parts:";
      cin>>A>>B;
      }
    }
     sub operator -(sub a2)
     {
        sub temp;
        temp.n=n;
       if(n==1)
       {
       
      temp.A=A-a2.A;

       }
       if (n==2)
       {
      temp.A=A-a2.A;
      temp.B=B-a2.B;
       }
      return temp;
     }
     void output1()
     {
       if(n==1)
       {
         cout<<"sub of two number:"<<A;
       }
       if(n==2)
       {
         cout<<"sub of two imaginary numbers:"<<A<<"+i"<<B;
       }
     }
 
};
int main()
{

  sub a1,a2,result;
 
  a1.input1();
  a2.input1();
  result=a1-a2;
  result.output1();
 
  return 0;
}

Output:




wap to subtract digonals elements of a matrix in cpp hackerrank program

subtract of digonals elements of a matrix in cpp hackerrank program:

solution:


#include<iostream>
using namespace std;
int main()
{
    int n,i,j,k,l,arr[100][100],sum=0,sum1=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>arr[i][j];
        }
    }
     for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
         
          if(i==j)
          {
              sum+=arr[i][j];
             
          }
           if(i+j==n-1)
          {
            sum1+=arr[i][j];
          }
        
         
       
        }
    }
  
   if(sum1>sum)
   {
       cout<<sum1-sum;
   }
   else
   {
       cout<<sum-sum1;   }

}


Output:

(5+34+4) - (9+34+9)=9;



Output:



cpp program to check prime number

here is a a program for checking that the number entered is prime number or not? 

i basically use repl.it for online coding you can also use it is very simple to use and very user friendly thanks and have a look


#include <iostream>
using namespace std;
int main()
{
  int n,count,j;
  cin>>n;
  for(j=2;j<n;j++)
    {
    
     
      if(n%j==0)
      {
      
        count=1;
        break;
      }
    }
    if(count==0)
    {
      cout<<"prime number";
    }
    else
    {
      cout<<"not prime";
    }
}

Output 
check that the number entered is a prime number or not?

exception handling of devide by zero in cpp

Q.exception handling of devide by zero in cpp:


#include <iostream>
using namespace std;
int main()
{
  int a,b;
  cin>>a>>b;
  try{
    if(b==0)
    {
      throw "devided by zero";
    }
    else
    {
      cout<<a/b;
    }
  }
  catch(const char *msg)
  {
    cout<<msg;
  }

}