Week 12 Structures
Week 12 Structures
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'
struct Point
{
int x, y;
};
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);
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>
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
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
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
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;
};
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
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;
};
If hours and minutes are equal, sort the time in increasing order of seconds.
If hours are equal (minutes are not equal), sort the time in increasing order of minutes.
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
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.
#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;
}
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.
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;
}