Unit1 Algorithms
Unit1 Algorithms
ASSI.PRO.UPEKSHA CHAUDHRI 1
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 2
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
As one would not follow any written instructions to cook the recipe, but
only the standard one. Similarly, not all written instructions for
programming are an algorithm. For some instructions to be an algorithm,
it must have the following characteristics:
• Clear and Unambiguous: The algorithm should be unambiguous.
Each of its steps should be clear in all aspects and must lead to
only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should
be well-defined inputs. It may or may not take input.
• Well-Defined Outputs: The algorithm must clearly define what
output will be yielded and it should be well-defined as well. It
should produce at least 1 output.
• Finite-ness: The algorithm must be finite, i.e. it should terminate
after a finite time.
• Feasible: The algorithm must be simple, generic, and practical,
such that it can be executed with the available resources. It must
not contain some future technology or anything.
• Language Independent: The Algorithm designed must be
language-independent, i.e. it must be just plain instructions that
can be implemented in any language, and yet the output will be the
same, as expected.
• Input: An algorithm has zero or more inputs. Each that contains a
fundamental operator must accept zero or more inputs.
• Output: An algorithm produces at least one output. Every
instruction that contains a fundamental operator must accept zero
or more inputs.
• Definiteness: All instructions in an algorithm must be
unambiguous, precise, and easy to interpret. By referring to any of
the instructions in an algorithm one can clearly understand what is
to be done. Every fundamental operator in instruction must be
defined without any ambiguity.
• Finiteness: An algorithm must terminate after a finite number of
steps in all test cases. Every instruction which contains a
ASSI.PRO.UPEKSHA CHAUDHRI 3
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 4
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements
or groups of elements from a particular data structure. They can be of
different types based on their approach or the data structure in which the
element should be found.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to
the requirement. The algorithms which help in performing this function
are called sorting algorithms. Generally sorting algorithms are used to
sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they
contain an index with a key ID. In hashing, a key is assigned to specific
data.
7. Divide and Conquer Algorithm:
This algorithm breaks a problem into sub-problems, solves a single sub-
problem, and merges the solutions to get the final solution. It consists of
the following three steps:
• Divide
• Solve
• Combine
Advantages of Algorithms:
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a given
problem.
• In an Algorithm the problem is broken down into smaller pieces or
steps hence, it is easier for the programmer to convert it into an
actual program.
Disadvantages of Algorithms:
ASSI.PRO.UPEKSHA CHAUDHRI 5
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Algorithm Fundamental:
Theta notation
Mathematical Representation of Theta notation:
Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤
c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0}
Note: Θ(g) is a set
The above expression can be described as if f(n) is theta of g(n), then the
value f(n) is always between c1 * g(n) and c2 * g(n) for large values of n
(n ≥ n0). The definition of theta also requires that f(n) must be non-
negative for values of n greater than n0.
The execution time serves as both a lower and upper bound on the
algorithm’s time complexity.
ASSI.PRO.UPEKSHA CHAUDHRI 7
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
It exist as both, most, and least boundaries for a given input value.
A simple way to get the Theta notation of an expression is to drop low-
order terms and ignore leading constants. For example, Consider the
expression 3n3 + 6n2 + 6000 = Θ(n3), the dropping lower order terms is
always fine because there will always be a number(n) after which Θ(n3)
has higher values than Θ(n2) irrespective of the constants involved. For a
given function g(n), we denote Θ(g(n)) is following set of functions.
Examples :
{ 100 , log (2000) , 10^4 } belongs to Θ(1)
{ (n/4) , (2n+3) , (n/100 + log(n)) } belongs to Θ(n)
{ (n^2+n) , (2n^2) , (n^2+log(n))} belongs to Θ( n2)
Note: Θ provides exact bounds.
2. Big-O Notation (O-notation):
Big-O notation represents the upper bound of the running time of an
algorithm. Therefore, it gives the worst-case complexity of an algorithm.
.It is the most widely used notation for Asymptotic analysis.
.It specifies the upper bound of a function.
.The maximum time required by an algorithm or the worst-case time
complexity.
.It returns the highest possible output value(big-O) for a given input.
.Big-O(Worst Case) It is defined as the condition that allows an algorithm
to complete statement execution in the longest amount of time possible.
ASSI.PRO.UPEKSHA CHAUDHRI 8
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 9
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
1. Problem Understanding
2. Algorithm Planning
3. Design of algorithms
4. Algorithm Verification and Validation
5. Algorithm analysis
6. Algorithm Implementation
7. Perform post-mortem analysis
1. Problem Understanding:
Is it possible to solve the given problem? This falls under the domain
called computability theory. Computability theory deals with the
solvability of the given problem.
2. Design of Algorithms
Algorithm design is the next stage of problem solving. Algorithm design
is a way of developing the algorithmic solutions using the appropriate
design strategy.
.
ASSI.PRO.UPEKSHA CHAUDHRI 11
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
1. Subjective measures.
2. Objective measures.
Subjective measures include factors like ease of implementation or style
of the algorithm or understandability of algorithms. But the problem with
the subjective measures are that they vary from person to person. Hence
Objective measures are preferable.
ASSI.PRO.UPEKSHA CHAUDHRI 12
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
7. Postmortem analysis
A theoretical best solution for the given problem is called lower bound of
the algorithm. The worst case estimate of behavior of the algorithm is
called upper bound. The difference between upper and lower bound is
called algorithmic gap. Technically, no algorithmic gaps should exist.
But practically, there may be vast gap present between two
bounds.Problem solving process ends with postmortem analysis. Any
analysis should end with the valuable insight. Insights like Is the problem
solvable? Are there any limits of this algorithm? and Is the algorithm
efficient? are asked and collected.
Refining the algorithms is to bring these gaps short by reefing planning,
design, implementation and analysis. Thus problem solving is not linear
but rather this is a cycle.
Searching
ASSI.PRO.UPEKSHA CHAUDHRI 13
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
• Graph problems
Graph theory is a key topic in computer science that's important for
algorithms and data structures.
Combinatorial problems
The analysis of discrete structures is important for understanding the
properties of those structures and for analyzing algorithms.
Convex hulls
A building block for more sophisticated geometric algorithms.
ASSI.PRO.UPEKSHA CHAUDHRI 14
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Randomized algorithms
Important in both theoretical computing science and real-world
applications.
ASSI.PRO.UPEKSHA CHAUDHRI 15
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 16
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Theta notation
Mathematical Representation of Theta notation:
Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤
c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0}
Note: Θ(g) is a set
The above expression can be described as if f(n) is theta of g(n), then the
value f(n) is always between c1 * g(n) and c2 * g(n) for large values of n
(n ≥ n0). The definition of theta also requires that f(n) must be non-
negative for values of n greater than n0.
The execution time serves as both a lower and upper bound on the
algorithm’s time complexity.
It exist as both, most, and least boundaries for a given input value.
A simple way to get the Theta notation of an expression is to drop low-
order terms and ignore leading constants. For example, Consider the
expression 3n3 + 6n2 + 6000 = Θ(n3), the dropping lower order terms is
always fine because there will always be a number(n) after which Θ(n3)
has higher values than Θ(n2) irrespective of the constants involved. For a
given function g(n), we denote Θ(g(n)) is following set of functions.
Examples :
{ 100 , log (2000) , 10^4 } belongs to Θ(1)
{ (n/4) , (2n+3) , (n/100 + log(n)) } belongs to Θ(n)
{ (n^2+n) , (2n^2) , (n^2+log(n))} belongs to Θ( n2)
Note: Θ provides exact bounds.
ASSI.PRO.UPEKSHA CHAUDHRI 17
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 18
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 19
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Terminal/Terminator
ASSI.PRO.UPEKSHA CHAUDHRI 21
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Process
Decision
ASSI.PRO.UPEKSHA CHAUDHRI 22
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Document
Data or Input/Output
Stored Data
ASSI.PRO.UPEKSHA CHAUDHRI 23
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Flow Arrow
Comment or
Annotation
Predefined process
ASSI.PRO.UPEKSHA CHAUDHRI 24
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
On-page
connector/reference
Off-page
connector/reference
ASSI.PRO.UPEKSHA CHAUDHRI 25
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 26
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
This box is of an oval shape which is used to indicate the start or end of
the program. Every flowchart diagram has an oval shape that depicts the
start of an algorithm and another oval shape that depicts the end of an
algorithm. For example:
ASSI.PRO.UPEKSHA CHAUDHRI 27
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
2. Data
ASSI.PRO.UPEKSHA CHAUDHRI 28
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
3. Process
ASSI.PRO.UPEKSHA CHAUDHRI 29
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
4. Decision
ASSI.PRO.UPEKSHA CHAUDHRI 30
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
5. Flow
ASSI.PRO.UPEKSHA CHAUDHRI 31
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Advantages of Flowchart
• It is the most efficient way of communicating the logic of the
system.
• It acts as a guide for a blueprint during the program design.
• It also helps in the debugging process.
• Using flowcharts we can easily analyze the programs.
• flowcharts are good for documentation.
Disadvantages of Flowchart
ASSI.PRO.UPEKSHA CHAUDHRI 32
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 33
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Array:-
An array is a type of linear data structure that is defined as a collection of
elements with same or different data types. They exist in both single
dimension and multiple dimensions. These data structures come into
picture when there is a necessity to store multiple elements of similar
nature together at one place.
The difference between an array index and a memory address is that the
array index acts like a key value to label the elements in the array.
ASSI.PRO.UPEKSHA CHAUDHRI 34
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 35
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
2D Array
2D array can be defined as an array of arrays. The 2D array is organized
as matrices which can be represented as the collection of rows and
columns.
How to declare 2D Array
The syntax of declaring two dimensional array is very much similar to
that of a one dimensional array, given as follows.
1. int arr[max_rows][max_columns];
however, It produces the data structure which looks like following.
ASSI.PRO.UPEKSHA CHAUDHRI 36
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Above image shows the two dimensional array, the elements are
organized in the form of rows and columns. First element of the first row
is represented by a[0][0] where the number shown in the first index is the
number of that row while the number shown in the second index is the
number of the column.
How do we access data in a 2D array
Due to the fact that the elements of 2D arrays can be random accessed.
Similar to one dimensional arrays, we can access the individual cells in a
2D array by using the indices of the cells. There are two indices attached
to a particular cell, one is its row number while the other is its column
number.
However, we can store the value stored in any particular cell of a 2D
array to some variable x by using the following syntax.
1. int x = a[i][j];
ASSI.PRO.UPEKSHA CHAUDHRI 37
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
where i and j is the row and column number of the cell respectively.
We can assign each cell of a 2D array to 0 by using the following code:
1. for ( int i=0; i<n ;i++)
2. {
3. for (int j=0; j<n; j++)
4. {
5. a[i][j] = 0;
6. }
7. }
Initializing 2D Arrays
The syntax to declare and initialize the 2D array is given as follows.
1. int arr[2][2] = {0,1,2,3};
The number of elements that can be present in a 2D array will always be
equal to (number of rows * number of columns).
Example : Storing User's data into a 2D array and printing it.
C Example :
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
9. printf("Enter a[%d][%d]: ",i,j);
10. scanf("%d",&arr[i][j]);
ASSI.PRO.UPEKSHA CHAUDHRI 38
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
11. }
12. }
13. printf("\n printing the elements ....\n");
14. for(i=0;i<3;i++)
15. {
16. printf("\n");
17. for (j=0;j<3;j++)
18. {
19. printf("%d\t",arr[i][j]);
20. }
21. }
ASSI.PRO.UPEKSHA CHAUDHRI 39
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
ASSI.PRO.UPEKSHA CHAUDHRI 40
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Declaration of 2D Array
A 2D array with m rows and n columns can be created as:
type arr_name[m][n];
where,
• type: Type of data to be stored in each element.
• arr_name: Name assigned to the array.
• m: Number of rows.
• n: Number of columns.
For example, we can declare a two-dimensional integer array with
name ‘arr’ with 10 rows and 20 columns as:
int arr[10][20];
Initialization of 2D Arrays
We can initialize a 2D array by using a list of values enclosed inside
‘{ }’ and separated by a comma as shown in the example below:
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
ASSI.PRO.UPEKSHA CHAUDHRI 41
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
The elements will be stored in the array from left to right and top to
bottom. So, the first 4 elements from the left will be filled in the first
row, the next 4 elements in the second row, and so on. This is
clearly shown in the second syntax where each set of inner braces
represents one row.
In list initialization, we can skip specifying the size of the row. The
compiler will automatically deduce it in this case. So, the below
declaration is valid.
type arr_name[][n] = {…values…};
It is still compulsory to define the number of columns.
Note: The number of elements in initializer list should always be
either less than or equal to the total number of elements in the
array.
2D Array Traversal
Traversal means accessing all the elements of the array one by
one. An element in two-dimensional array is accessed using row
indexes and column indexes inside array subscript operator [].
arr_name[i][j]
where, i and j are row index and column index.
To traverse the whole array, we will use two loops. One loop to go
over each row from top to bottom and the other nested inside it to
access each element in the current row from left to right.
Example of 2D Array
The below example demonstrates the row-by-row traversal of a 2D
array.
C
// C Program to illustrate the 2D array
#include <stdio.h>
ASSI.PRO.UPEKSHA CHAUDHRI 42
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
int main() {
Output
arr[0][0]: 0 arr[0][1]: 1
arr[1][0]: 2 arr[1][1]: 3
arr[2][0]: 4 arr[2][1]: 5
Time Complexity: O(n * m), where n and m are numbers of rows
and columns respectively.
Auxiliary Space: O(1)
ASSI.PRO.UPEKSHA CHAUDHRI 43
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Pointer:-
Pointers are one of the core components of the programming language.
A pointer can be used to store the memory address of other variables,
functions, or even other pointers. The use of pointers allows low-level
memory access, dynamic memory allocation, and many other
functionality in C.
In this article, we will discuss C pointers in detail, their types, uses,
advantages, and disadvantages with examples.
What is a Pointer
A pointer is defined as a derived data type that can store the address of
other C variables or a memory location. We can access and manipulate
the data stored in that memory location using pointers.
As the pointers in tore the memory addresses, their size is independent
of the type of data they are pointing to. This size of pointers in C only
depends on the system architecture.
Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we
use the ( * ) dereferencing operator in the pointer declaration.
datatype * ptr;
where
• ptr is the name of the pointer.
• datatype is the type of data it is pointing to.
The above syntax is used to define a pointer to a variable. We can also
define pointers to functions, structures, etc.
How to Use Pointers?
The use of pointers in can be divided into three steps:
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
ASSI.PRO.UPEKSHA CHAUDHRI 44
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it.
To declare a pointer, we use the ( * ) dereference operator before its
name.
Example
int *ptr;
The pointer declared here will point to some random memory address as
it is not initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to
the pointer variable. We generally use the ( &: ampersand ) addressof
operator to get the memory address of a variable and then store it in the
pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This
method is called pointer definition as the pointer is declared and
initialized at the same time.
Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to
some value before starting using it. Otherwise, it may lead to number of
errors.
3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in
the memory address specified in the pointer. We use the same ( * )
dereferencing operator that we used in the pointer declaration.
ASSI.PRO.UPEKSHA CHAUDHRI 45
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Dereferencing a Pointer in C
C Pointer Example
C
// C program to illustrate Pointers
#include <stdio.h>
void geeks()
{
int var = 10;
// Driver program
int main()
{
geeks();
return 0;
}
Output
Value at ptr = 0x7ffca84068dc
Value at var = 10
Value at *ptr = 10
Types of Pointers
Pointers in C can be classified into many different types based on the
parameter on which we are defining their types. If we consider the type
of variable stored in the memory location pointed by the pointer, then the
pointers can be classified into the following types:
1. Integer Pointers
As the name suggests, these are the pointers that point to the integer
values.
Syntax
int *ptr;
2. Array Pointer
Pointers and Array are closely related to each other. Even the array name
is the pointer to its first element. They are also known as Pointer to
Arrays. We can create a pointer to an array using the given syntax.
Syntax
char *ptr = &array_name;
Union:-
The Union is a user-defined data type in C language that can contain
elements of the different data types just like structure. But unlike
structures, all the members in the C union are stored in the same
ASSI.PRO.UPEKSHA CHAUDHRI 48
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
memory location. Due to this, only one member can store data at the
given instance.
Syntax of Union in C
The syntax of the union in C can be divided into three steps which are as
follows:
C Union Declaration
In this part, we only declare the template of the union, i.e., we only
declare the members’ names and data types along with the name of the
union. No memory is allocated to the union in the declaration.
union union_name {
datatype member1;
datatype member2;
...
};
ASSI.PRO.UPEKSHA CHAUDHRI 49
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Keep in mind that we have to always end the union declaration with a
semi-colon.
Different Ways to Define a Union Variable
We need to define a variable of the union type to start using union
members. There are two methods using which we can define a union
variable.
1. With Union Declaration
2. After Union Declaration
1. Defining Union Variable with Declaration
union union_name {
datatype member1;
datatype member2;
...
} var1, var2, ...;
2. Defining Union Variable after Declaration
union union_name var1, var2, var3...;
where union_name is the name of an already declared union.
Access Union Members
We can access the members of a union by using the ( . ) dot
operator just like structures.
var1.member1;
where var1 is the union variable and member1 is the member of the
union.
The above method of accessing the members of the union also works for
the nested unions.
var1.member1.memberA;
Here,
• var1 is a union variable.
• member1 is a member of the union.
ASSI.PRO.UPEKSHA CHAUDHRI 50
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
// driver code
int main()
{
var1.member1 = 15;
return 0;
}
Output
The value stored in member1 = 15
• C
struct point {
int value;
};
ASSI.PRO.UPEKSHA CHAUDHRI 52
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
int main()
{
struct point s;
return 0;
}
In the above code s is an instance of struct point and ptr is the struct
pointer because it is storing the address of struct point.
Accessing the Structure Member with the Help of Pointers
There are two ways to access the members of the structure with the help
of a structure pointer:
1. With the help of (*) asterisk or indirection operator and (.) dot
operator.
2. With the help of ( -> ) Arrow operator.
Below is the program to access the structure members using the
structure pointer with the help of the dot operator.
• C
ASSI.PRO.UPEKSHA CHAUDHRI 53
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main()
{
s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;
return 0;
ASSI.PRO.UPEKSHA CHAUDHRI 54
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
Output:
1
What is Structure?
The structure is one of the user-defined data types in C that can contain
elements of different types as its members.
Declaration of a Structure in C
struct structure_name{
memberType memberName;
...
...
};
Array of Structures
An array whose elements are of type structure is called array of
structure. It is generally useful when we need multiple structure variables
in our program.
ASSI.PRO.UPEKSHA CHAUDHRI 55
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
};
// structure template
struct Employee {
char Name[20];
int employeeID;
int WeekAttendence[7];
};
// driver code
int main()
{
// defining array of structure of type Employee
struct Employee emp[5];
// adding data
for (int i = 0; i < 5; i++) {
emp[i].employeeID = i;
strcpy(emp[i].Name, "Amit");
int week;
ASSI.PRO.UPEKSHA CHAUDHRI 57
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
// printing data
for (int i = 0; i < 5; i++) {
printf("Emplyee ID: %d - Employee Name: %s\n",
emp[i].employeeID, emp[i].Name);
printf("Attendence\n");
int week;
for (week = 0; week < 7; week++) {
printf("%d ", emp[i].WeekAttendence[week]);
}
printf("\n");
}
return 0;
}
Output
Emplyee ID: 0 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 1 - Employee Name: Amit
Attendence
ASSI.PRO.UPEKSHA CHAUDHRI 58
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
0123456
Emplyee ID: 2 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 3 - Employee Name: Amit
Attendence
0123456
Emplyee ID: 4 - Employee Name: Amit
Attendence
0123456
ASSI.PRO.UPEKSHA CHAUDHRI 59
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
example:-
#include <stdio.h>
#include <string.h>
// Declaration of the
// dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the
// Outer structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
ASSI.PRO.UPEKSHA CHAUDHRI 60
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
};
// Driver code
int main()
{
// Structure variable
struct Organisation org;
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name,
"GeeksforGeeks");
strcpy(org.org_number, "GFG123768");
ASSI.PRO.UPEKSHA CHAUDHRI 61
BZ GROW MORE INSTITUTE OF MSC(CA&IT)
org.org_number);
printf("Employee id : %d\n",
org.emp.employee_id);
printf("Employee name : %s\n",
org.emp.name);
printf("Employee Salary : %d\n",
org.emp.salary);
}
Output:
The size of structure organisation : 68
Organisation Name : GeeksforGeeks
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000
ASSI.PRO.UPEKSHA CHAUDHRI 62