0% found this document useful (0 votes)
220 views3 pages

LAB 3 Solution

The document contains 3 tasks that involve writing C++ code to work with structures. Task 1 has code snippets declaring structures like Time with fields and calling functions to work with structure pointers. Task 2 involves writing a full program to work with a Point structure, including getting input, adding points, and displaying results. Task 3 defines a Rectangle structure using two Point structures and writes code to get input, calculate the rectangle area using a function, and display the result.

Uploaded by

shahzad nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views3 pages

LAB 3 Solution

The document contains 3 tasks that involve writing C++ code to work with structures. Task 1 has code snippets declaring structures like Time with fields and calling functions to work with structure pointers. Task 2 involves writing a full program to work with a Point structure, including getting input, adding points, and displaying results. Task 3 defines a Rectangle structure using two Point structures and writes code to get input, calculate the rectangle area using a function, and display the result.

Uploaded by

shahzad nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB 3

TASK 1: Write C++ code fragments for the following.

1. Declare a structure Time with fields hour, minute and second.

ANS. Struct time


{ int hour,minute,sec; };

2. Declare a variable of type Time.

ANS. Time x;

3. Declare a variable of type Time, a pointer to Time and an array of 10 Times. Assign the
Address of the variable to the pointer.

ANS. time t1,*tp,a[10]; for(int


i=0;i<10;i++)
{ tp=&a[i]; }

4. Using pointer variable set the value of hour to 5, minute to 10 and second to 50.

ANS. In main we will write.


time a,*p; P=&a;
*p.hour=5;
*p.minute=10;
*p.sec=50;

5. Declare a structure Date. Declare another structure Project with fields: ID, startDate and
endDate. (Use nested structures).

Ans. struct date {


int month; int
day; int year;
}; struct project
{ int id;
date startdate,enddate; };

6. How would you call the function void printTime(Time *) ?


ANS. printtime(t)
TASK 2: Write a complete C++ program with the following features. a.
Declare a structure Point with two integer members x and y.
b. Define a function getInput(),which accepts a Point by reference. Get user input for a Point in this
function.
c. Define a function addPoints(), which accepts two Points p1 and p2. The function adds their
respective members, and returns a Point which is the sum of two. For example if one point is (2,3), the
other is (4,5), the function should return a Point (6,8).
d. In the main(), declare two variables of type Point. Call the function getInput() twice to get the
values of these Points from user. Add the two Points using the function addPoints() and display the x and
y values of the result returned by the function

PROGRAM

#include "stdafx.h"
#include<iostream> using
namespace std;
struct point
{ int x,y; };
void getinput(point &a)
{ cout<<"Enter the value for x"<<endl;
cin>>a.x;
cout<<"Enter the value for y"<<endl;
cin>>a.y; }
point addpoints(point p1, point p2)
{ point p3;
p3.x=p1.x+p2.x; p3.y=p1.y+p2.y;
return p3; }
int _tmain(int argc, _TCHAR* argv[])
{ point temp;
point a1,a2; getinput(a1);
getinput(a2);
temp=addpoints(a1,a2);
cout<<temp.x<<endl;
cout<<temp.y<<endl;
system ("pause"); return 0;
}

TASK 3:

i. Declare a structure Rectangle with two Points as its members, the top left and the bottom right.
ii.Declare a variable of type Rectangle and get user input for the two points.
iii. Define a function computeArea() which accepts a Rectangle and returns its area. iv. Display the
area of the Rectangle in the main().

PROGRAM

#include "stdafx.h" #include<iostream> using namespace std; struct point


{ int x,y; }; struct rectangle
{ point topleft,bottomright; };
int compute_area(rectangle q)
{ int p1;

p1=(q.topleft.y*q.bottomright.x);
return p1; }
int _tmain(int argc, _TCHAR*
argv[])
{ int temp;
rectangle a1; a1.topleft.x=0;
a1.bottomright.y=0;
cout<<"Enter value for rectangle , for topleft y and bottom right x"<<endl;
cin>>a1.topleft.y>>a1.bottomright.x;
temp=compute_area(a1);
cout<<"THE AREA OF RECTANGLE WITH SIDES"<<a1.topleft.y<<" and
"<<a1.bottomright.x<<" = "<<temp;
system("pause");
return 0; }

You might also like