0% found this document useful (0 votes)
15 views24 pages

Week 12 Structures

structures

Uploaded by

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

Week 12 Structures

structures

Uploaded by

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

Structure

A structure is a user defined data type in C/C++.


A structure creates a data type that can be used to group items of possibly different types into a single type.

Keyword “struct” is used to create a structure.

struct address
{
// members or fields of structure
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Structure
A structure variable can either be declared with structure declaration

struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'

or as a separate declaration like basic types.

struct Point
{
int x, y;
};

struct Point p1; // The variable p1 is declared like a normal variable

Note: In C++, the struct keyword is optional before in declaration of a variable. In C, it is mandatory.
Structure
#include <stdio.h>

struct Point
{
int x, y; Structure members can be initialized using curly braces ‘{}’.
}; Structure members are accessed using dot (.) operator.

int main()
{
struct Point p1 = { 4, 7 };
printf("x = %d, y = %d\n", p1.x, p1.y);

// Accessing members of point p1


p1.x = 20;
printf("x = %d, y = %d\n", p1.x, p1.y);

return 0;
}
Structure
#include <stdio.h> Like other primitive data types, we can create an
struct Point array of structures.
{
int x, y;
};

int main()
{
struct Point arr[5];
for (int i = 0; i < 5; i++) // Create an array of structures
{
arr[i].x = i * i; // Access array members
arr[i].y = i + i;
}
for (int i = 0; i < 5; i++)
printf("%d %d\n", arr[i].x, arr[i].y);
return 0;
}
Structure
#include <stdio.h>

struct Point Like primitive types, we can have pointer to a


{ structure. If we have a pointer to structure, members
int x, y; are accessed using arrow ( –> ) operator.
};
(*p1).x is equivalent to p1–>x
int main()
{
Point *p1 = new Point;
p1->x = 10; p1->y = 23;
printf("%d %d\n", p1->x, p1->y);

printf("%d %d\n", (*p1).x, (*p1).y);


delete p1;
return 0;
}
Structure
#include <stdio.h>
struct Point
{
int x, y;
};
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin; Size of the structure equals to the sum of sizes of its types
};
int main()
{
printf("%d\n", sizeof(Point));
printf("%d\n", sizeof(address));
return 0;
}
4817. Rectangle

Find the perimeter and the area of a rectangle.

Input. Each line is a separate test case that contains the length n and the width m (1 ≤ n, m ≤ 1000) of a
rectangle.
Output. For each test case print in one line the perimeter and the area of a rectangle.
Sample input Sample output
3 1 8 3
5 3 16 15 To compute the perimeter and the area of the rectangle,
10 7 34 70 use the formulas:
P = (n + m) * 2;
S=n*m

Implement structure rectangle to solve the problem


#include <stdio.h> 4817. Rectangle

struct rectangle
{
int x, y;
} a;
int GetPerimeter(struct rectangle p)
{
return 2 * (p.x + p.y);
}
int GetArea(struct rectangle p)
{
return p.x * p.y;
}
int main(void)
{
while (scanf("%d %d", &a.x, &a.y) == 2)
printf("%d %d\n", GetPerimeter(a), GetArea(a));
return 0;
}
#include <stdio.h> 4817. Rectangle

struct rectangle
{
int x, y;
} a;
int GetPerimeter(struct rectangle &p) Pass by reference
{
return 2 * (p.x + p.y);
}
int GetArea(struct rectangle &p) Pass by reference
{
return p.x * p.y;
}
int main(void)
{
while (scanf("%d %d", &a.x, &a.y) == 2)
printf("%d %d\n", GetPerimeter(a), GetArea(a));
return 0;
}
#include <stdio.h> 4817. Rectangle

struct rectangle
{
int x, y;
} a;
int GetPerimeter(struct rectangle *p) Pass by pointer
{
return 2 * (p->x + p->y);
}
int GetArea(struct rectangle *p) Pass by pointer
{
return p->x * p->y;
}
int main(void)
{
while (scanf("%d %d", &a.x, &a.y) == 2)
printf("%d %d\n", GetPerimeter(&a), GetArea(&a));
return 0;
}
3171. Point within a circle

Check, if the point is inside the circle.

Input. The first line contains the coordinates of the circle center and its radius. The second line
contains the coordinates of point A. All numbers are integers not exceeding 10000 by absolute value.
Output. Print “YES”, if the point A belongs to a circle (with boundaries), and “NO” otherwise.
Sample input Sample output
2 1 2 NO
1 3

Implement structures point, circle


3171. Point within a circle

Let (x, y) be the coordinates of the center of the circle and r be its radius. Let (a, b) be the coordinates of
point A. Point A lies inside the circle (including boundaries) if
(x – a)2 + (y – b)2 ≤ r2

r
(x,y) struct circle
{
int x, y, r;
A(a,b)
};

struct point
{
int x, y;
};

// return True if point p is inside a circle c


int inside(struct point p, struct circle c);
2132. Point on a line

Determine whether a point belongs to the line given by equation Ax + By + C = 0.

Input. Five integers – the coordinates of the point x, y and the coefficients A, B, C of the line equation (it
is guaranteed that A and B are not simultaneously 0).
Output. Print “YES”, if the point belongs to the line and “NO” otherwise.
Sample input Sample output
3 7 -2 1 -1 YES

Implement structures point, line


972. Sorting time

Sort the time according to specified criteria.

Input. First given the number n (1 ≤ n ≤ 100), and then n times. Each time is given as three integers –
hours (0 to 23), minutes (0 to 60) and seconds (from 0 to 60).
Output. Print the times, sorted in nondecreasing order (time is also displayed in the form of three
numbers, do not print the leading zeros).
Sample input Sample output
4 7 30 0 Declare the time structure that contains hours, minutes and seconds.
10 20 30 10 20 30
7 30 00 13 30 30 struct MyTime
23 59 59 23 59 59 {
13 30 30 int hour, min, sec;
};

Declare the array of structures.

struct MyTime lst[100];


972. Sorting time

Function f returns 1 if a < b.

int f(MyTime a, MyTime b)


{

If hours and minutes are equal, sort the time in increasing order of seconds.

if ((a.hour == b.hour) && (a.min == b.min)) return a.sec < b.sec;

If hours are equal (minutes are not equal), sort the time in increasing order of minutes.

if (a.hour == b.hour) return a.min < b.min;

Otherwise sort the time in increasing order of hours.

return a.hour < b.hour;


}
972. Sorting time

Function swap swaps the times a and b.

void swap(MyTime &a, MyTime &b)


{
MyTime temp = a; a = b; b = temp;
}

The main part of the problem. Read the input data.

scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d %d %d", &lst[i].hour, &lst[i].min, &lst[i].sec);
972. Sorting time

Sort the data using a swap sort.

for (i = 0; i < n; i++)


for (j = i + 1; j < n; j++)
if (!f(lst[i], lst[j])) swap(lst[i], lst[j]);

Print the sorted data.

for (i = 0; i < n; i++)


printf("%d %d %d\n", lst[i].hour, lst[i].min, lst[i].sec);
Preprocessor and macros
The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program
before it is compiled. These transformations can be the inclusion of header file, macro expansions etc.

C Source Code Preprocessor Compiler

The #include preprocessor is used to include header files to C programs. For example,

#include <stdio.h>

Here, stdio.h is a header file. The #include preprocessor directive replaces the above line with the
contents of stdio.h header file. That’s the reason why you need to use #include <stdio.h> before you can use
functions like scanf() and printf().

You can also create your own header file containing function declaration and include it in your program
using this preprocessor directive.

#include ”my_header.h”
Preprocessor and macros
A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced
by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like
when they are used:
 object-like macros resemble data objects when used;
 function-like macros resemble function calls.

Here, when we use PI in our program, it is replaced with acos(-1.0).


Preprocessor and macros
You can also define macros that work in a similar way like a function call.
This is known as function-like macros. For example,

#include <stdio.h>
#include <math.h>
#define CircleArea(r) acos(-1.0)*(r)*(r)

int main(void)
{
double radius = 1;
double area = CircleArea(radius);
printf("%lf\n", area);
return 0;
}

Every time the program encounters CircleArea(argument), it is replaced by


3.1415*(argument)*( argument)
Suppose, we passed 1 as an argument then CircleArea(1) expands to
Preprocessor and macros
#include <stdio.h>
#define min(x,y) (x < y) ? x : y
#define max(x,y) (x > y) ? x : y

int main(void)
{
int a = 10, b = 5;
printf("%d %d\n", min(a, b), max(a, b));
return 0;
}
Predefined macros
Here are some predefined macros in C programming.

__DATE__ A string containing the current date


__TIME__ A string containing the current time
__FILE__ A string containing the current fie

#include <stdio.h> #include <stdio.h>

int main(void) int main(void)


{ {
puts(__DATE__); printf("Date: %s\n",__DATE__);
puts(__TIME__); printf("Time: %s\n", __TIME__);
puts(__FILE__); printf("File: %s\n", __FILE__);
return 0; return 0;
} }
typedef
The C programming language provides a keyword called typedef, which you can use to give a type a
new name. Following is an example to define a term ll for 64-byte numbers:

typedef long long ll;

After this type definition, the identifier ll can be used as an abbreviation for the type long long, for
example
#include <stdio.h>
ll a, b, c; typedef long long ll;

int main(void)
{
ll a, b;
scanf("%lld %lld", &a, &b);
printf("%lld\n", a * b);
return 0;
}
typedef
You can use typedef to give a name to your user defined data types as well. For example, you can use
typedef with structure to define a new data type and then use that data type to define structure variables
directly as follows

#include <stdio.h>
typedef long long ll;
Remember that typedef interpretation
typedef struct fraction is performed by the compiler whereas
{ #define statements are processed
ll numerator, denominator; by the pre-processor
} fraction;

int main(void)
{
fraction a;
a.numerator = 3; a.denominator = 5;
printf("%lld/%lld\n", a.numerator, a.denominator);
return 0;
}

You might also like