CFP_Unit 4
CFP_Unit 4
UNIT 4
marks in memory:
1. Construct 100 variables to store percentage marks
obtained by 100 different students, i.e. each
variable containing one student‟s marks.
2. Construct one variable (called array) capable of
storing or holding all the hundred values.
Defination
Array is variable that can store multiple values of same
data type.
Ex.If you want to store 100 integer , you can create array
of size 100.
Array size of 10 45 63 21 33 22
10 20 15 25 8
Arr=
indices 0 1 2 3 4 5 6 7 8 9
Delcaration
mark
mark[0 mark[1 mark[2 mark[3 mark[4
] ] ] ] ]
important points about
array
Array always start with index 0
If size of array is n, then last element is at n-1
index.
Address of mark[0] is 2000 then address of
mark[1] is 2002 , mark[3] is 2004 and so on
as size of int is 2byte.
Elements are always stored in contigeous
fashion.
Initilization of array
It is possible to initilize array at the time of
declaration
int mark[5]={ 10,20,15,8,24};
or
int mark[]={ 10,20,15,8,24};
10 20 15 8 24
0 1 2 3 4
changing values of array
int mark[5]= { 10,20,15,8,24};
Make the valu of 3rd element 5
i.e mark[2]=5;
Make the valu of 5rd element 18
i.e mark[4]=18;
Advantages of array
Random access of element is possible.
Use of fewer lines of code as it creates single array
variable of multiple elements.
Easy access of elements.
Traversal can be done with single loop.
Sorting is also easy
Diasadvantages
static in nature.
Allows fixed number of elements,that decided at the
time of delcaration.
Insertion and deletion of element is costly.
Continue..
For example, assume the following group of numbers,
which represent percentage marks obtained by five students.
per = { 48, 88, 34, 23, 96 }
In C, the fourth number is referred as per[3]. This is because
in C the counting of elements begins with 0 and not with 1.
Thus, in this example per[3] refers to 23 and per[4] refers to
96.
In general, the notation would be per[i], where, i can take a
value 0, 1, 2, 3, or 4, depending on the position of the
element being referred. Here per is the subscripted variable
(array), whereas i is its subscript.
Types of Array
Single Dimension or 1D array:
-Store list of values of same data types.
-Data is store in linear form
Multidimension array
ex int array[1][1]=10;
i\j
Col 0 Col
1 Col2
Row 0 2 3 5
Row1 5 6 7
Arrays
The C language provides a capability that enables the
user to design a set of similar data types, called array.
An array is a collection of similar elements.
These similar elements could be all ints, or all floats, or
all chars, etc.
Usually, the array of characters is called a „string‟,
whereas an array of ints or floats is called simply an
array.
All elements of any given array must be of the same
type.
Entering Data into an Array
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}
The for loop causes the process of asking for and
receiving a student‟s marks from the user to be
repeated 30 times.
The first time through the loop, i has a value 0, so
the scanf( ) function will cause the value typed to be
stored in the array element marks[0], the first
element of the array.
This process will be repeated until i becomes 29.
Let Us Revise Whatever We Have Learnt
About Arrays:
void main( )
{ int avg, sum = 0 ; int i ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
{
sum = sum + marks[i] ; /* read data from an array*/
}
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg );
Operation on Array
Traversal
Copying
Reversing
Sorting
Insertion
Deletion
Traversal
Accessing Each Elements for specific purpose.(To count
total number of elements, Sum of all elements etc)
Since array is a linear data structure i.e. all elements are
A[0]=5 , A[1]=10
insert element.
Check location is valid or invalid(Loc<1
or Loc>n+1)
If location is valid insert element.
Deletion
Deletion of element could be at start of
array or end of array or in between
elements.
Take the location from where user want to
delete element.
Check entered location is valid or invalid.
void main( )
{
char name[ ] = "Klinsman" ;
printf ( "%s", name ) ;
}
The %s used in printf( ) is a format specifier for printing
out a string.
Continue…
%s can be used to receive a string from the keyboard, as shown
below.
void main( )
{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s!", name ) ;
}
And here is a sample run of the program...
Continue…
int main( )
{
char source[ ] = "Sayonara" ;
char target[20] ;
strcpy ( target, source ) ;
printf ( "\n source string = %s", source ) ;
printf ( "\n target string = %s", target ) ;
}
And here is the output...
source string = Sayonara
Standard Library String Functions
value zero.
If they are not, it returns the numeric difference
void main( )
{ J=74
char string1[ ] = "Jerry" ; F=70
Space =32
char string2[ ] = "Ferry" ;
int i, j, k;
i = strcmp ( string1, "Jerry" ) ;
j = strcmp ( string1, string2 ) ;
k = strcmp ( string1, "Jerry boy" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
}
string reverse
strrev() function # include<stdio.h>
Used to reverse the
int main()
string {
Before char s[50];
C O D E \0
printf(“Enter string”);
gets(s);
printf (“Reversed string is: %s
After
E D O C \0
”,strrev(s));
return (0);
}
Summary
A string is nothing but an array of characters
terminated by ‘\0’.
Being an array, all the characters of a string are
stored in contiguous memory locations.
Though scanf( ) can be used to receive multi-word
strings, gets( ) can do the same job in a cleaner
way.
Both printf( ) and puts( ) can handle multi-word
strings.
Strings can be operated upon using several
standard library functions like strlen( ), strcpy( ),
strcat( ) and strcmp( ) which can manipulate
Pointers
Pointer is a variable which store address of another
variable.
This variable can be of type int, float,char,array,function
or any other pointer.
Pointer syntax
int *p or float *p
working of pointers
m
p
Value
m=29 29 Value
p=&m 65222
65222 Address
65225 Address of
m
Address of m is stored in pointer p. To get value of m we
use *p
Pointer Notation
Consider the declaration,
int i = 3 ;
address.
‘*’ - ‘value at address’ operator