PPS Unit-4
PPS Unit-4
Array
An array is defined as the collection of similar type of data items stored at contiguous memory
locations. Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability to store the
collection of derived data types, such as pointers, structure, etc. The array is the simplest data
structure where each data element can be randomly accessed by using its index number or
subscript
Properties of Array
The array contains the following properties.
o Each element of an array is of same data type and carries the same size
o Elements of the array are stored at contiguous memory locations where the first element
is stored at the smallest memory location and at index 0
o Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed
the l imit. So, it doesn't grow the size dynamically so it can waste the memory.
Types of C Array
We can declare an array in the c language in the following types
One or single dimensional
Two or multi dimensional.
Declaration of C Array
We can declare an array in the c language in the following way.
Declaring One or single dimensional
data_type array_name[array_size];
Now, let us see the example to declare the array.
int m[5];
Here, int is the data_type, m are the array_name, and 5 is the array_size. It is just like 5
variable of int type.
Declaring Two or multi dimensional
data_type array_name[array_size1][array_size2];
Now, let us see the example to declare the array.
int m[3][5];
Here, int is the data_type, m are the array_name, and 3 and 5 is the dimensions . It is just
like 3 rows and 5 columns means 3×5=15 element / variable of int type.
int m[3][5][2]; means it can store 3×5×2=30 element / variable
or
We can see that index of element start from 0 and goes upto size-1 by increasing 1 value. So we can
access all element using loop e.g. we can print array as-
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output
80
60
70
85
75
For printing two dimensional just we require two index and two loop
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",marks[i][j]);
}
}
Similarly For reading two dimensional just we require two index and two loop
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
scanf("%d ",&marks[i][j]);
}
}
Application of array
1. Collecting element
2. Searching element
3. Sorting array
4. Matrix calculation
5. Storing string
Searching
Searching is the process of investigating or finding a value in array
Searching can be two type
1. Linear searching or sequential searching (can be used for unsorted array )
2. Binary searching(used for sorted array only )
Linear searching or sequential searching : is a method for finding an element within a list. It
sequentially checks each element of the list until a match is found or the whole list has been
searched.
Program -
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[]={12,23,78,98,67,56,45,19,65,9},key,i,s=0;
clrscr();
printf("\nENTER A NUMBER to search: ");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==arr[i])
printf("\nTHE NUMBER %d EXISTS IN THE ARRAY",key);
s=1;
}
if(s==0)
printf("\nTHE NUMBER %d DOES NOT EXIST IN THE ARRAY",key);
getch();
}
Sorting
Sorting is process of arranging value in a particular order. Order can be increasing or decreasing
e.g.
Bubble sort: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in wrong order.
#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
We can also define the string by the pointer in C language. For example:
1. char *ch="c program";
In such case, '\0' will be appended at the end of the string by the compiler.
String handling functions
gets(char *s)- it reads string from keyboard and store in a variable s.
puts(char *s)- it prints string s on output screen.
strlen(char *s)- it counts number of characters in string s.
strlwr(char *s)- it converts all character of string s to lower case
strupr(char *s)- it converts all characters of string s to upper case
strrev(char *s)- it reverses all character of string s
strcat(char *s1, char *s2)- it concatenates string s2 to end of string s1
strcpy(char *s1, char *s2)- it copies strings2 to string s1
strcmp(char *s1, char *s2)- it compare two string s1 and s2 and returns-
Structure
It is secondary data type and it is collection of elements of different data types. Each element
contains values and allocates memory for each elements.
Declaration of structure
“struct” keyword is used to declare structure
struct student
{
int rollno;
char name[20];
float fee;
};
Initialization of structure
struct student
{
int rollno;
char name[20];
float fee;
};
struct strudent s1={1,”ram”,65.5};
Accessing element/member of structure
we can access elements using dot(.) membership operator
struct student
{
int rollno;
char name[20]; //structure of array
float fee;
};
struct strudent s1;
struct student s[60]; // array of structure
s1.rollno=2;
s1.name=”syam”;
s1.fee=560.5;
operation on structure
only assignment operation can be performed on structure.
struct student
{
int rollno;
char name[20];
float fee;
};
struct strudent s1,s2;
s1.rollno=2;
s1.name=”syam”;
s1.fee=560.5;
s2=s1; //assignment
Nested structure
A structure can contains another structure as element.
struct student
{
int rollno;
char name[20];
float fee;
};
struct faculty
{
char name[20];
float sal;
};
struct college
{
student s;
faculty f;
};
Now to access element of nested structure
struct college c;
c.s.name=”ram”;
c.f.name=”syam”;
Union
It is secondary data type and it is collection of elements of different data types with shared memory.
Only one element can contain values at a time and allocates memory for largest elements.
Declaration of structure
“union” keyword is used to declare structure
union student
{
int rollno;
char name[20];
float fee;
};
we can use a struct keyword to define a structure. we can use a union keyword to define a union.
Every member within structure is assigned a unique In union, a memory location is shared by all the data
memory location. members.
Changing the value of one data member will not Changing the value of one data member will change the
affect other data members in structure. value of other data members in union.
It enables you to initialize several members at once. It enables you to initialize only the first member of
union.
The total size of the structure is the sum of the size The total size of the union is the size of the largest data
of every data member. member.
It is mainly used for storing various data types. It is mainly used for storing one of the many data types
that are available.
It occupies space for each and every member It occupies space for a member having the highest size
written in inner parameters. written in inner parameters.
You can retrieve any member at a time. You can access one member at a time in the union.
this means that function f(n) does not grow faster than g(n), or that function g(n) is an upper
bound for f(n), for all sufficiently large n→∞. Here is a graphic representation of f(n) = O(g(n))
relation:
2) Ω-notation or big Omega- it represents lower bound or best case analysis. Let functions f(n)
and g(n) from the positive integers to the positivee integers, we say that f(n) = Ω (g(n)) when
there exist constants c > 0 and n0 > 0 such that
this means that function g(n) does oes not grow faster than ff(n),
(n), or that function g(n) is an lower bound
for f(n), for all sufficiently large n→∞.
→∞. Here is a gr
graphic
aphic representation of f(n) = Ω (g(n)) relation:
Time
3) Θ-notation or big Theta- it represents middle bound or average case analysis. Let functions
f(n) and g(n) from the positive integers to the positive integers, we say that f(n) = Θ (g(n))
when there exist constants c1,c2
1,c2 > 0 and n0 > 0 such that
0≤
≤ c1*g(n) ≤f(n) ≤ c2*g(n), for all n ≥ n0
this means that function f(n) grow between c1*g(n) and c2*g(n) so f(n) is an middle bound,
for all sufficiently large n→∞.
→∞. Here is a graphic representation of f(n) = Θ (g(n)) relation:
Time