You are on page 1of 9

Page 1 of 9

TASK #1:
What are structures? What is the purpose of using structures?
SOLUTION:
Structure is a user-defined datatype in C/C++ language which allows us to combine data of different types
together. Structure helps to construct a complex data type which is more meaningful. It is somewhat like
an Array, but an array holds data of similar type only.
Task #2:
What are benefits of using the structures and what are the applications?
SOLUTION:
Structures provide better performance when we have small collections of value-types that you want to
group together. A structure is used to represent information about something more complicated than a
single number. structure can store different data types element like double, int, float, char.
TASK #3:
Write a program that declares a structure to store Name, Roll_no, marks and
section of a student. The program should define a structure variable, input values of
two students and then display their values.
SOLUTION: (Source Code is Copy-able) Screen Shot of Source Code
#include <iostream>
using namespace std;

struct student
{
char name[50];
string roll;
float marks;
char section;
};
int main()
{
student s1;
student s2;
cout << "Enter information of 1st Student," << endl;
cout << "Enter name: ";
cin.get(s1.name, 50);
cout << "Enter roll number: ";
cin >> s1.roll;
cout << "Enter marks: ";
cin >> s1.marks;
cout << "Enter Section: ";
cin >> s1.section;

cout << "\nEnter information of 2nd Student," << endl;


cout << "Enter name: ";
cin>>s2.name;
cout << "Enter roll number: ";
cin >> s2.roll;
cout << "Enter marks: ";
cin >> s2.marks;
cout << "Enter Section: ";
cin >> s2.section;

cout << "\nDisplaying Information," << endl;


cout<<"*****************************"<<endl;

cout << "\t\t1st Student Information," << endl;


cout << "Name: " << s1.name << endl;
cout << "Roll: " << s1.roll << endl;
cout << "Marks: " << s1.marks << endl;
cout << "Section: " << s1.section << endl;

cout << "\t\t2nd Student Information," << endl;

cout << "Name: " << s2.name << endl;


cout << "Roll: " << s2.roll << endl;
cout << "Marks: " << s2.marks << endl;
cout << "Section: " << s2.section << endl;
return 0;
}
Page 2 of 9

OUTPUT SCREEN SHOT

TASK #4:
Write a program that declares a structure to store book author, pages, and its cost. It defines
two structure variables. The program displays the book author and the pages of costly book.

SOLUTION: (SOURCE CODE CAN BE COPIED )


#include<iostream>
using namespace std;

struct Book
{
char author_name[30];
char book_name[30];
int pages;
int price;
};

int main()
{
Book b1;
Book b2;

cout<<"\n*** Enter Details of Book 1 ***\n";


cout<<"-----------------------------------------";
cout<<"\nEnter Author Name: ";
cin>>b1.author_name;
cout<<"Enter Book Name: ";
cin>>b1.book_name;
cout<<"Enter Book Pages: ";
cin>>b1.pages;
cout<<"Enter Book Price: ";
cin>>b1.price;
cout<<endl<<endl;

cout<<"\n*** Enter Details of Book 2 ***\n";


cout<<"-----------------------------------------";
cout<<"\nEnter Author Name: ";
cin>>b2.author_name;
cout<<"Enter Book Name: ";
cin>>b2.book_name;
cout<<"Enter Book Pages: ";
cin>>b2.pages;
Page 3 of 9

cout<<"Enter Book Price: ";


cin>>b2.price;
cout<<endl<<endl;

cout<<"\n\t\t\t*** Book details is as follows ***"<<endl;


cout<<"------------------------------------------------------------------------------------------\n";
cout<<"Author_Name\t\tBook_Name\t\tPages\t\t\tPrice"<<endl;
cout<<"------------------------------------------------------------------------------------------\n";
cout<<b1.author_name<<"\t\t\t"<<b1.book_name<<"\t\t\t"<<b1.pages<<"\t\t\t"<<b1.price<<endl;
cout<<b2.author_name<<"\t\t\t"<<b2.book_name<<"\t\t\t"<<b2.pages<<"\t\t\t"<<b2.price<<endl;

cout<<"\n\t\t\t*** Most costly book detail is ***\n";


cout<<"------------------------------------------------------------------------------------------\n";
cout<<"Author_Name\t\tBook_Name\t\tPages\t\t\tPrice"<<endl;
cout<<"------------------------------------------------------------------------------------------\n";

if(b1.price > b2.price)


cout<<b1.author_name<<"\t\t\t"<<b1.book_name<<"\t\t\t"<<b1.pages<<"\t\t\t"<<b1.price<<endl;
else
cout<<b2.author_name<<"\t\t\t"<<b2.book_name<<"\t\t\t"<<b2.pages<<"\t\t\t"<<b2.price<<endl;
}

Source Code (Screen-shot)


Page 4 of 9

Output Console (Screen-shot)

TASK #5:
Write a program that declares a structure to store book author, pages, and its cost. It defines
a structure array of size five. The program displays the book author and the pages of costly
book.
SOLUTION: (SOURCE CODE CAN BE COPIED)
#include<iostream>
#include<string>
using namespace std;
struct Book
{
char author_name[30];
char book_name[30];
int pages;
int price;
};

int main()
{
Book b[5];
cout<<"--- Enter details of 5 books ---\n";
cout<<"-----------------------------------------";
for(int i=0;i<5;i++)
{
cout<<"\n*** Enter Details of Book "<<(i+1)<<" ***";
cout<<"\nEnter Author Name: ";
cin>>b[i].author_name;
cout<<"Enter Book Name: ";
cin>>b[i].book_name;
cout<<"Enter Book Pages: ";
Page 5 of 9

cin>>b[i].pages;
cout<<"Enter Book Price: ";
cin>>b[i].price;
cout<<endl;
}
cout<<"\n\t\t\t*** Book details is as follows ***"<<endl;
cout<<"------------------------------------------------------------------------------------------\n";
cout<<"Author_Name\t\tBook_Name\t\tPages\t\t\tPrice"<<endl;
cout<<"------------------------------------------------------------------------------------------\n";
for(int i=0;i<5;i++)
{
cout<<b[i].author_name<<"\t\t\t"<<b[i].book_name<<"\t\t\t"<<b[i].pages<<"\t\t\t"<<b[i].price<<endl;
}
int i ;
int x=0;
for(int i=0;i<5;i++)
if(b[0].price < b[i].price)
x=i;
cout<<"\n\t\t\t*** Most costly book detail is ***\n";
cout<<"------------------------------------------------------------------------------------------\n";
cout<<"Author_Name\t\tBook_Name\t\tPages\t\t\tPrice"<<endl;
cout<<"------------------------------------------------------------------------------------------\n";
cout<<b[x].author_name<<"\t\t\t"<<b[x].book_name<<"\t\t\t"<<b[x].pages<<"\t\t\t"<<b[x].price<<endl;
}
(SOURCE CODE SCREEN-SHOT)
Page 6 of 9

(OUTPUT CONSOLE SCREEN-SHOT)


Page 7 of 9

TASK #6:
Write a structure declaration to hold the following data about a savings account:
Account number
Account balance
Interest rate
Average monthly balance
SOLUTION:
struct Account
{
string Account_number;
double Account_balance;
double Interest_rate;
double Average_monthly_balance;
};

TASK #7:
Write a definition statement for a variable of the structure you declared in Previous question.
Initialize the members with the following data:
Account number=19157900255639
Account balance=100000
Interest rate=4%
Average monthly balance=42157
SOLUTION:
Account savings = {"19157900255639",100000,0.04,42157 };
Page 8 of 9

TASK #8:
Write a function that accept a rectangle structure as its argument and displays the
structure’s contents on the screen.
SOLUTION:
Page 9 of 9

TASK #9:
Write a function that uses a rectangle structure variable as its parameters and stores the
user input in the structure’s members.
SOLUTION:

Prepared By: Khawar Khalil

You might also like