Unit 3
Unit 3
Unit 3
ers.com
Engaging Peers, Inspiring Careers!
score0
score1
score2
score3
.
.
score59
scores[0]
scores[1]
scores[2]
scores[3]
.
.
scores[59]
#include<stdio.h>
Sixty input
int main() {
int scores[60] , i , j, temp;
statements are
for(i = 0; i < 60 ;i++) {
called by one
printf("Enter the score : ");
loop statement
scanf("%d", &scores[i]);
1770
}
comparing
for(i=0;i<(60-1);i++)
statements
for( j=0; j <(60 -(i+1)); j++)
are included
if(scores[ j ] < scores[ j +1]) {
temp = scores[ j ];
in one loop
scores[ j ] = scores[ j +1];
statement
scores[ j + 1] = temp;
}
for( i = 0; i < 60; i ++) printf("%4d", scores[i]);
}
scores[0]
scores[1]
scores[2]
scores[3]
scores[4]
...
4022
4023
start here
4024
4025
4026
4027
4028
4029
4030
4031
scores Array
( memory
addresses)
Mean can be calculated only after reading all scores. Each deviation is difference of
individual score and mean. To calculate deviations of all scores, scores must be stored
in an ARRAY.
Accessing
Declaration of
an
Array
element
Initialization of
Array
Input to an element
Processing on Array
Scalar variable for single data item & Vector variable for
multiple data items
Scalar Variables :
A variable represents a data item and it can be used to store a single atomic
value at a time. These are also called scalar variables.
Integer takes 2 bytes memory as a single unit to store its value. i.e.,the value of
a scalar variable cannot be subdivided into a more simpler data items.
The address of first byte is the address of a variable .
Vector Variables (arrays):
In contrast, an array is multivariable (an aggregate data type), which is also
referred to a data structure. It represents a collection of related data items of same
type.
An individual data item of an array is called as element. Every element is
accessed by index or subscript enclosed in square brackets followed after the array
name.
All its elements are stored in consecutive memory locations, referred under a
common array name.
Ex : int marks[10] ; /* declaration of array */
0 is first number in computer environment. The first element of array marks is
marks[0] and last element is marks[9]. (the address of first element is the address
of the array)
An array is a derived data type. It has additional operations for retrieve and
update the individual values.
The lowest address corresponds to the first element and the highest address to
the last element.
Arrays can have from one to several dimensions. The most common array is the
string, which is simply an array of characters terminated by a null.
Elements of
Array [3] by [4]
[0]
[0]
[0]
[1]
[0]
[2]
[0]
[3]
[1]
[0]
[1]
[1]
[1]
[2]
[1]
[3]
[2]
[2]
[2] array
[2]
/*passing
an
to
[0]
[1]
[2]
[3]
function
*/
#define SIZE 10
int main() {
float list[SIZE] ,avg;
avg = average(SIZE ,
list );
}
float average( int n , float
x[]) {
float sum=0,i;
for( i = 0; i < n ; i++)
sum = sum + x[i];
\0
name
name is an array of characters has size of eleven
characters including a null character \0(ascii code is zero).
char name[25] ;
scanf(%s, name); /*reading a string until a white space is encountered ( & operator
is not required )*/
printf(%s, name); /*printing a string in input window */
gets(name) ; /* reading a string including white spaces until \n is encountered. */
puts(name); /* printing a string and moves cursor to new line */
String Manipulation Functions in <string.h>
- returns the length of string excluding the last null
strlen(s1)
character.
strcpy(s1,s2)
- copies characters in s2 into s1.
strcat(s1,s2)
- concatenates s2 to s1.
strcmp(s1,s2)
-compares s1 with s2 lexicographically and returns 0 if two
strings are
same , returns -1 if s1 is before s2
and returns +1 if s1 is after s2.
strcmpi(s1,s2)
-compares s1 with s2 like strcmp() but case of characters is
ignored.
strchr(s1,ch)
-returns pointer to first occurrence of the character ch in s1.
strstr(s1,s2)
-returns pointer to first occurrence s2 in s1.
strrev(s1)
-returns pointer to the reversed string.
Passing Parameters By
Value
/* program to swap two numbers
*/
#include<stdio.h>
void swap(int x, int y)
{
int temp;
temp = x; x = y; y = temp;
printf(\nIn swap() : %d %d
,x,y);
}
int main()
{
int a = 25,b = 37;
printf(Before swap() : %d
%d,a,b);
swap (a,b);
printf(\nAfter swap() : %d
%d,a,b);
}
Output :
Before swap() 25 37
In swap ()
37 25
After swap()
25 37
Passing Parameters By
Reference
Output :
Before swap() 25 37
In swap ()
37 25
After swap()
37 25
char *ptr =
NULL;
ptr = &option;
*ptr = N;
Value in option
404
2
Y
option
Creates a pointer
variable with a name
ptr
Which can hold a
Memory address
Memory address
of
Variable option
Is copied to the
Pointer ptr
The value N is
stored in the
variable
which has the
memory address
4042
ptr
4042
ptr
4042
ptr
Y
option
404
2
N
option
404
2
n2
n1
p
NUL
L
NUL
L
Prints address of n1
n1
Prints 6 3
n1
n2
n2
Prints 3 3
*p = 7 ;
printf ( %d %d , *p , *q ) ;
}
n1
Prints 7 7
n2
When two pointers are referencing with one variable, both pointers
contains address of the same variable, and the value changed
through with one pointer will reflect to both of them.
Prints 12 12
p = a;
printf("%d %d\n", *p, *a);
Prints 12 12
q = p;
printf("%d %d",*p,*q);
b = a; /* error */
}
Prints 12 31 56
arr[0] or *( arr + 0 )
12
p - 1
arr[1] or *( arr + 1 )
31
arr[2] or *( arr + 2 )
56
p +1
arr[3] or *( arr + 3 )
19
p +2
arr[4] or *( arr + 4 )
42
p +3
Array of Pointers
The advantage of pointer array is that the length of each row in the array may
be different. The important application of pointer array is to store character strings of
different length. Example :
char *day[ ] = { Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday };
pa
25
4024
4056
402
4
405
6
407
8
ppa
a[0][1]
base_addre
ss
a[0][2]
a[1][0]
a[1][1]
a[1][2]
a[2][0]
a[2][1]
a[2][2]
a[3][0]
a[3][1]
Address of a[ i ] [ j ] = *( * ( base_address + i ) + j ) = * ( * ( a + i ) + j )
a[3][2]
void Pointer
void type pointer is a generic
pointer, which can be assigned to any
data type without cast during
compilation or runtime. void pointer
cannot be dereferenced unless it is
cast.
int main( ) {
void* p;
int x = 7;
float y = 23.5;
p = &x;
printf(x contains : %d\n, *( ( int *)p) );
p = &y;
printf(y contains : %f\n, *( ( float *)p) );
}
Output :
x contains 7
y contains 23.500000
Function Pointers
Function pointers are pointers, which
point to the address of a function.
Declaration :
<return type> (* function_pointer)
(type1 arg1, type2 arg2, . );
int add ( int a, int b ) { return (a + b) ; }
int sub ( int a, int b ) { return (a b) ; }
int (*fp ) (int, int ) ; /* function pointer */
int main( ) {
fp = add;
printf(Sum = %d\n, fp( 4, 5 ) ) ;
fp = sub;
printf(Difference = %d\n, fp( 6 , 2 ) ) ;
}
Output :
Sum = 9
Difference = 4
free ( p );
p = NULL;
}
};
struct book b1 ;
b1 *ptr ;
ptr = (book *) calloc ( 10, sizeof ( book ) );
ptr = (book * ) realloc ( ptr , 35 * sizeof ( book ) );
Modifies the size of previously allocated memory to
new size.
Classification of Characters
control
iscntrl
()
space
isspace
()
graphical
isgraph ()
alpha-numeric
isalnum ( )
alphabeti
c
isalpha( )
upper
isupper ( )
printabl
isprint
e
()
punctuation
ispunct ( )
digit
isdigit ()
lower
islower
()
long atol(s)
float atof(s)
void* calloc(u1,u2)
void exit(u)
int rand(void)
void* realloc(p,u)
void srand(u)
void systerm(s)
<time.h>
clock_t clock()
Sets time.
double difftime(t1,t2)
FaaDoOEngine
ers.com
Engaging Peers, Inspiring Careers!