Thursday, 6 July 2017

12C-Prg-1-Write a Program to define a class myarray.

12C-Prg-1-Write a Program  to define a class myarray. which reads n elements of int type.  Write
4 member functions
1. input();
2.output();
3.large();  //  largest element logic
4.small(); // smallest element logic

#include<iostream.h>
#include<conio.h>

class myarray
{ int a[50],l,i,n,s;

  public:
  void input();
  void large();
  void small();
  void output();
};
void myarray::input()
{
  cout<<"enter no. of elements=";
  cin>>n;
  cout<<"\nenter the elements";
  for(i=0;i<n;i++)
  cin>>a[i];
}
void myarray::large()
{
  l=a[0];
  for(i=1;i<n;i++)
  { if(l<a[i])
    l=a[i];
  }
}
void myarray::small()
{
  s=a[0];
  for(i=1;i<n;i++)
  { if(s>a[i])
    s=a[i];
  }
}
void myarray::output()
{
 cout<<"\nlargest ="<<l;
 cout<<"\nsmallest="<<s;
}

void main()
{
clrscr();
myarray obj1;
obj1.input();
obj1.large();
obj1.small();
obj1.output();
getch();
}

No comments:

Post a Comment