Section D
Union
• Union in C is a special data type available in C
that allows storing different data types in the
same memory location.
• a union with many members, but only one
member can contain a value at any given time.
• Unions provide an efficient way of using the
same memory location for multiple purposes.
Syntax
• The union tag(union name) is optional and each
member definition is a normal variable definition,
such as int i; or float f; or any other valid variable
definition. At the end of the union's definition,
before the final semicolon, we can specify one or
more union variables but it is optional
• Example
union Data {
int i;
float f;
char str[20]; } data;
Here we define a union type named Data having three
members i, f, and str .
• a variable of Data type can store an integer, a floating-
point number, or a string of characters. It means a single
variable, i.e., same memory location, can be used to store
multiple types of data.
• we can use any built-in or user defined data types inside a
union based on your requirement.
• The memory occupied by a union will be large enough to
hold the largest member of the union. For example, in the
above example, Data type will occupy 20 bytes of memory
space because this is the maximum space which can be
occupied by a character string.
Accessing Union Members
• To access any member of a union, we use
the member access operator (.). The member
access operator is coded as a period between the
union variable name and the union member that we
wish to access.
• We use the keyword union to define variables of
union type
• Example
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20]; };
int main( ) {
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0; }
Output
data.i : 1917853763
data.f: 4122360580327794860
data.str : C Programming
Explanatiom
Here, the values of i and f members of union got
corrupted because the final value assigned to
the variable has occupied the memory location
and this is the reason that the value
of str member is getting printed very well.
union Data {
int i;
float f;
char str[20]; };
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0; }
Output
data.i : 10
data.f : 220.500000
data.str : C Programming
Explanation
Here, all the members are getting printed very
well because one member is being used at a
time.
// C program to illustrate differences
// between structure and Union
#include <stdio.h>
#include <string.h>
// declaring structure
struct struct_example
{
int integer;
float decimal;
char name[20];
};
// declaring union
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
struct struct_example s;
union union_example u;
// difference two and three
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
// difference five
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "C programming");
printf("structure data:\n integer: %d\n “ "decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "C programming");
printf("\nunion data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
return 0;
}
Output
sizeof structure : 28
sizeof union : 20
Accessing all members at a time:
structure data:
integer: 183
decimal: 90.00
name: C programming
union data:
integer: 1919950915
decimal: 4756185880441909561201111597056.00
name: C programming
Call by Reference
• The call by reference method of passing arguments
to a function copies the address of an argument
into the formal parameter.
• Inside the function, the address is used to access
the actual argument used in the call.
• It means the changes made to the parameter affect
the passed argument.
• To pass a value by reference, argument pointers are
passed to the functions just like any other value.
#include <stdio.h>
int main (){
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of
b : %d\n", b );
/* calling a function to swap the values*/
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
void swap(int *x, int *y){
int temp;
temp = *x;/* save the value of x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
return;
}
Output
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100
It shows that the change has reflected outside
the function as well, unlike call by value where
the changes do not reflect outside the
function.
Bubble Sort
• It is the simplest sorting technique which is
also called as an exchange sort.
• Procedure
• Compare the first element with the remaining
elements in the list and exchange(swap) them
if they are not in order.
• Repeat the same for other elements in the list
until all the elements gets sorted.
Algorithm
• Run a nested for loop to traverse the input
array using two variables i and j, such that 0 ≤ i
< n-1 and 0 ≤ j < n-i-1
• If arr[j] is greater than arr[j+1] then swap
these adjacent elements, else move on
• Print the sorted array
#include<stdio.h>
int main(){
int a[50], i,j,n,t;
printf("enter the No: of elements in the list:\n");
scanf("%d", &n);
printf("enter the elements:\n");
for(i=0; i<n; i++) {
scanf ("%d", &a[i]);
}
printf("Before bubble sorting the elements are:\n");
for(i=0; i<n; i++) {
printf("%d \t\n", a[i]);}
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
} } }
printf ("after bubble sorting the elements are:\n");
for (i=0; i<n; i++)
{printf("%d\t", a[i];)
}
return 0;}
Output
enter the No: of elements in the list:
6
enter the elements:
9
7
5
6
9
0
Before bubble sorting the elements are:
9
7
5
6
9
0
after bubble sorting the elements are:
05 6 7 9 9
Time Complexity: O(N2)
Auxiliary Space: O(1)
Linear Search
1. Time Complexity
Best Case : O(1)
Average Case : O(n)
Worst Case: O(n)
Best Case Complexity - In Linear search, best case occurs when the element
we are finding is at the first position of the array. The best-case time
complexity of linear search is O(1).
Average Case Complexity - The average case time complexity of linear search
is O(n).
Worst Case Complexity - In Linear search, the worst case occurs when the
element we are looking is present at the end of the array. The worst-case
in linear search could be when the target element is not present in the
given array, and we have to traverse the entire array. The worst-case time
complexity of linear search is O(n).
2. Space Complexity
Space Complexity : O(1)
The space complexity of linear search is O(1)
#include <stdio.h>
int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}
Output
• Thanks