Professional Documents
Culture Documents
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;
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.
struct Book
{
char author_name[30];
char book_name[30];
int pages;
int price;
};
int main()
{
Book b1;
Book b2;
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
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: