Q1) WAP to define a structure of name marks and add three data members of int type.
Store
3 different subject marks in it. Calculate average and percentage and display it.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct marks
{
int m1, m2, m3;
};
int _tmain(int argc, _TCHAR* argv[])
{
marks std;
float avg=0.0;
float sum=0.0;
float per=0.0;
cout<<"Enter english marks: ";
cin>>std.m1;
cout<<"Enter urdu marks: ";
cin>>std.m2;
cout<<"Enter maths marks: ";
cin>>std.m3;
sum = std.m1 + std.m2 + std.m3;
avg = sum/3;
per = (sum /300) *100;
cout<<"Average is "<<avg<<endl;
cout<<"Percentage is "<<per;
_getch();
return 0;
}
Q2) WAP to define structure named of Book and define 3 data members (char name, int
price, int no. of pages) and then display the details of most costly book.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct book
{
char name[50];
int price;
int page;
};
int _tmain(int argc, _TCHAR* argv[])
{
book b1;
book b2;
cout<<"Enter details of first book\n";
cout<<"Enter name of the book \n";
cin>>[Link];
cout<<"Enter price of the book \n";
cin>>[Link];
cout<<"Enter no. of pages of the book \n";
cin>>[Link];
cout<<"\nEnter details of second book\n";
cout<<"Enter name of the book \n";
cin>>[Link];
cout<<"Enter price of the book \n";
cin>>[Link];
cout<<"Enter no. of pages of the book \n";
cin>>[Link];
if([Link] > [Link]){
cout<<"\nMost costly book \n";
cout<<[Link]<<endl<<[Link]<<endl<<[Link]<<"$";
}
else
{
cout<<"\nMost costly book \n";
cout<<[Link]<<endl<<[Link]<<endl<<[Link]<<"$";
}
_getch();
return 0;
}
Q3) WAP to define a structure named of country and create 3 objects of it. And input name and
population of countries then find the country of largest population.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct country
{
char name[50];
long pop;
};
int _tmain(int argc, _TCHAR* argv[])
{
country c[3];
for (int i =0; i<3; i++){
cout<<"Enter name of country: ";
cin>>c[i].name;
cout<<"Enter population of country: ";
cin>>c[i].pop;
}
int max=c[0].pop;
for(int i=0; i<3; i++)
{
if(c[i].pop > max){
max = c[i].pop;
cout<<"Most populated country is "<<c[i].name<<" of "<<c[i].pop<<"
population";
}
}
_getch();
return 0;
}
Q 4) WAP to represent the example or nested structure.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct PhoneBook
{
char name[50];
char houseno[60];
};
struct PhoneNo
{
int mobileno;
int ptclno;
PhoneBook obj1;
};
int _tmain(int argc, _TCHAR* argv[])
{
PhoneNo obj2;
cout<<"Enter values \n";
cout<<"Enter mobile number: "; cin>>[Link];
cout<<"Enter PTCL number: "; cin>>[Link];
cout<<"Enter name of a person: "; cin>>[Link];
cout<<"Enter house number: "; cin>>[Link];
cout<<"\nValues are \n";
cout<<"Name: "<<[Link]<<endl<<" House No "<<[Link]<<endl<<"
Mobile number "<<[Link]<<endl<<"PTCL "<<[Link]<<endl;
_getch();
return 0;
}
Q5) Create 3 nested structure. One is PhoneBook, second is PhoneNo, third is mobilNo. Input record
from the user. And then print the first four records, even index records and odd index records.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct PhoneBook
{
char name[50];
char houseno[60];
};
struct PhoneNo
{
int ptclno;
PhoneBook obj1[10];
};
struct MobileNo
{
int zong;
int ufone;
int mobilink;
PhoneNo obj2[10];
};
int _tmain(int argc, _TCHAR* argv[])
{
MobileNo obj3[10];
cout<<"Enter values: \n";
for(int i= 0; i<10; i++)
{
cout<<"Enter zong number :"; cin>>obj3[i].zong;
cout<<"Enter ufone number :"; cin>>obj3[i].ufone;
cout<<"Enter mobilink number :"; cin>>obj3[10].mobilink;
cout<<"Enter ptcl number :"; cin>>obj3[i].obj2[i].ptclno;
cout<<"Enter house number :"; cin>>obj3[i].obj2[i].obj1[i].houseno;
cout<<"Enter name :"; cin>>obj3[i].obj2[i].obj1[i].name;
}
cout<<"Record of first four entries \n";
for (int i=0; i<=4; i++)
{
cout<<"Name :"<<obj3[i].obj2[i].obj1[i].name<<endl;
cout<<"House Number :"<<obj3[i].obj2[i].obj1[i].houseno<<endl;
cout<<"PTCL Number :"<<obj3[i].obj2[i].ptclno<<endl;
cout<<"Zong Number :"<<obj3[i].zong<<endl;
cout<<"Ufone Number :"<<obj3[i].ufone<<endl;
cout<<"Mobilink Number :"<<obj3[i].mobilink<<endl;
}
cout<<"\nRecord of even indexes\n";
for(int i=0; i<10; i++)
{
if(i%2 == 0)
{
cout<<"Name :"<<obj3[i].obj2[i].obj1[i].name<<endl;
cout<<"House Number :"<<obj3[i].obj2[i].obj1[i].houseno<<endl;
cout<<"PTCL Number :"<<obj3[i].obj2[i].ptclno<<endl;
cout<<"Zong Number :"<<obj3[i].zong<<endl;
cout<<"Ufone Number :"<<obj3[i].ufone<<endl;
cout<<"Mobilink Number :"<<obj3[i].mobilink<<endl;
}
else
{
cout<<"Name :"<<obj3[i].obj2[i].obj1[i].name<<endl;
cout<<"House Number :"<<obj3[i].obj2[i].obj1[i].houseno<<endl;
cout<<"PTCL Number :"<<obj3[i].obj2[i].ptclno<<endl;
cout<<"Zong Number :"<<obj3[i].zong<<endl;
cout<<"Ufone Number :"<<obj3[i].ufone<<endl;
cout<<"Mobilink Number :"<<obj3[i].mobilink<<endl;
}
}
_getch();
return 0;
}
Q6) WAP of “Fruit” structure and creates 5 objects of it. Input name and price of each fruit. Display the
most costly fruit.
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct Fruit
{
char name[50];
int price;
};
int _tmain(int argc, _TCHAR* argv[])
{
Fruit f[5];
for (int i=0; i<5; i++)
{
cout<<"Name: "; cin>>f[i].name;
cout<<"Price: "; cin>>f[i].price;
}
int max=0, g=0;
for (int i=0; i<5; i++)
{
if(max < f[i].price){
max = f[i].price;
g = i;
}
}
cout<<"Most costly fruit is "<<f[g].name<<" of "<<max<<" price";
_getch();
return 0;
}