0% found this document useful (0 votes)
111 views10 pages

PPS Unit-4

Pps unit 4 notes

Uploaded by

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

PPS Unit-4

Pps unit 4 notes

Uploaded by

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

PPS NOTES BY PRADEEP KUMAR SHARMA

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

1 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Accessing Array Elements


An element is accessed by index or subscript. index or subscript must be char or int type. This is
done by placing the index of the element within square brackets after the name of the array. For
example −
int marks[5];
The above statement will take the 5 element from the array and assign the value to marks variable.
First element will be marks[0]
Second element marks[1] and so on the last element will be marks[4]
Initializing of C Array
The simplest way to initialize an array is by using the index of each element. Or We can initialize each
element of the array by using the brace. Consider the following example.
int marks[5]={80,60,70,85,75};

or

we can initialize without size it consider size from no. of element

int marks[]={80,60,70,85,75}; that stores values at different index


automatically as-
marks[0]=80;
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;

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;

2 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

//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

Now we can read array values as


for(i=0;i<5;i++)
{
scanf("%d ",&marks[i]);
}

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

3 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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])
{

4 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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]);
}
}

Character Array & string


The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The
character array or the string is used to manipulate text such as word or sentences. Each character in
the array occupies one byte of memory, and the last character must always be ('\0'). The termination
character ('\0') is important in a string since it is the only way to identify where the string ends. When
we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
There are two ways to declare a string in c language.
1. By char array(static or fixed size)
2. By pointer(dynamic or variable size)
Let's see the example of declaring string by char array in C language.
1. char ch[10]={'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};
2. char ch[]=”program”;
As we know, array index starts from 0, so it will be represented as in the figure given below.
p r o g r a m \0
0 1 2 3 4 5 6 7
While declaring string, size is not mandatory. So we can write the above code as given below:
3. char ch[]={'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};

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-

5 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

o 0 if s1 and s2 are equal


o Negative difference of ASCII value of first mismatch characters if ASCII of
s1<ASCII of s2
o Positive difference of ASCII value of first mismatch characters if ASCII of s1>ASCII
of s2

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];

6 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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;
};

7 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Differences between Union & structure


Structure Union

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.

It supports flexible array. It does not support a flexible array.

It can waste memory It can save memory

8 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Enumerated data type


Enumerated Types allow us to create our own symbolic names for a list of related ideas. These
names can be accessible using a integer. The keyword for an enumerated type is “enum”
enum color{black, white, green, blue, red};
0 1 2 3 4
enum color c;
c=green;
printf(“%d”,c); //will print 2

Notion of order of complexity


Complexity is one of important characteristics of algorithm. It defines that how much memory and
time is required to implement an algorithm.
To express or estimate complexity we use some notation called asymptotic notations, using these
notations we can compares two algorithms. There are three notations –
1) O-notation or Big Oh – it represents upper bound or worst case analysis. Let functions f(n)
and g(n) from the positive integers to the positive integers, we say that f(n) = O(g(n)) when
there exist constants c > 0 and n0 > 0 such that

f(n) ≤ c * g(n), for all n ≥ n0

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:

9 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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

f(n) ≥ c*g(n), for all n ≥ n0

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

10 PPS NOTES BY PRADEEP KUMAR SHARMA

You might also like