Thursday, 28 April 2016

Array Of Objects With Conditions

#include<iostream.h>
#include<conio.h>
class city
{
int pop;
char name[20];
public:
void accept()
{
cout<<"enter name and population"<<endl;
cin>>name>>pop;
}
void display();
}c[5];
void city::display()
{
int max=0,i;
for(i=0;i<5;i++)
{
if(max<c[i].pop)
max=c[i].pop;
}
for(i=0;i<5;i++)
{
if(max==c[i].pop)
{
cout<<"Max Populated city="<<c[i].name<<endl;
cout<<"Population="<<max;
}
}
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
c[i].accept();
}
c[i].display();
getch();
}
/*Output:
enter name and population
qqq 123                                                                      
enter name and population
ddd 456
enter name and population
ttt 677
enter name and population
uuu 876
enter name and population
ttg 667
Max Populated city=uuu
Population=876 */

No comments:

Post a Comment