Module 3
Module 3
Module 3
I) ARRAY DECLARATION :
type name[size];
Where,
The type can be int, float, double, char or any other valid data type.
Name to identify the array.
The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
EXAMPLE:
int marks[5];
1st Element 2nd Element 3rd Element 4th Element 5th Element
marks[0] marks[1] marks[2] marks[3] marks[4]
The address of array elements can simply be calculated by using the base address. The
general formula to calculate the address of array element is
A]K]=BA(A)+W(K-LOWER-BOUND)
Where,
A-is an array.
K- index of the element, for which we have to calculate the address.
BA- base address of the array A.
W- is the word size of one element in memory(for ex: size of int is 2).
LOWER-BOUND- is the index of the first element in the array.
EXAMPLES
Solution :
99 67 78 56 88 90 34 85
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
A]K]=BA(A)+W(K-LOWER BOUND)
marks[4]=1000+2(4-0)
=1000+8
=1008
Solution :
A]K]=BA(A)+W(K-LOWER BOUND)
marks[4]=1000+4(4-0)
=1000+16
=1016
Note: Storing an char requires 1 byte, therefore the word size is 1 byte.
Storing an double requires 8 bytes, therefore the word size is 8 bytes.
An array must be both, declared and defined before it can be used. Array declaration only
reserves memory for the elements in the array. No values will be stored.
The values of array elements are stored when an array is defined is called as “array
initialization”.
The general form of array initialization is :
Where,
The type can be int, float, double,char or any other valid data type.
Name to identify the array.
The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
The values are specified in the curly braces and they are separated by commas.
90 82 78 95 88
marks[0] marks[1] marks[2] marks[3] marks[4]
An array with name marks is declared, that has enough space to store 5 elements.
The first element, i.e marks[0] is assigned with value 90. The second element, i.e
,marks[1] is assigned with value 82 and so on.
90 82 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
If the number of values provided is less than the number of element in the array,
then the un-assigned elements are filled with zeros.
The length of array is given by the number of elements stored in it. The general formula
to calculate the length of the array is
Where,
UPPER BOUND- is the index of the last element.
LOWER BOUND- is the index of the first element in array.
EXAMPLE:
1) Let age be an array of integers such that age[0]=2, age[1]=5, age[2]=3, age[3]=1
age[4]=7. Show the memory representation of the array and calculate its length.
2 5 3 1 7
age[0] age[1] age[2] age[3] age[4]
V) ONE-DIMENSIONAL ARRAY :
A one dimensional array or single dimensional array is used to store a linear list of values
of the same type.
The elements in a one dimensional array are stored with the index values starting from
zero to one less than size of the array.
Each element in a one dimensional array is accessed using the same name and a single
subscript[ ].
A one dimensional array is similar to a row or column matrix.
DECLARATION:
type name[size];
Where,
The type can be int, float, double,char or any other valid data type.
Name to identify the array.
The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
Examples:
1) int list[10] – declaration creates an array named list of 10 integer constants.
2) char name[20] – declaration creates name as an array of 20 characters.
3) float xyz[5] – declaration creates xyz as an array of 5 floating point numbers.
4) double p[100] – declaration creates an array of 100 double precision numbers.
TOTAL SIZE
The total amount of memory that can be allocated to a one dimensional array is
computed as
total size = size * [size of(data_type)];
Where,
size- is a number of elements of a one dimensional array.
size of( ) – is an unary operator to find the size in bytes.
data_type – basic data or user defined data type.
Where,
The type can be int, float, double,char or any other valid data type.
Name to identify the array.
The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
The values are specified in the curly braces and they are separated by commas.
10 15 20 25 30
m[0] m[1] m[2] m[3] m[4]
Figure : Memory representation of array
An array with name marks is declared, that has enough space to store 5 elements. The
first element, i.e marks[0] is assigned with value 10. The second element, i.e ,marks[1] is
assigned with value 15 and so on.
If the number of values to be initialized is less than the size of the array, then the
elements are initialized in the order from 0th location. The remaining locations will be initialized
to zero automatically.
90 82 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
98 97 90
m[0] m[1] m[2]
Here, the compiler will calculate the size of the array based on the number of initial
values.
Since number of elements is 3, totally 3*2=6 bytes are reserved. Where size of integer is
2 bytes.
char b[ ]=”COMPUTER”;
The array b is initialized as
C O M P U T E R \0
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8]
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5], i, n;
printf(“enter the size of the array\n”);
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
printf(“elements of the array are\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
}
The arrays with more than one dimension are called “Multi-Dimensional Arrays”.
Multi-dimensional arrays are defined in much the same manner as one-dimensional
arrays, except that a separate pair of square brackets is required for each subscript.
A multi-dimensional array is used, when data items are arranged in row-wise and
column-wise in a tabular fashion.
In multi-dimensional array, to identify a particular item it is necessary to specify two
subscripts. The first subscripts identifies the row number and second subscripts identifies
the column number.
DECLARATION :
Where,
The type can be int, float, double,char or any other valid data type.
Name to identify the array.
Row size- number of elements to be processed under subscript 1.
Column size- number of elements to be processed under subscript 2.
INITIALIZATION:
The values of array elements are stored when an array is defined is called as “array
initialization”.
The initialization can be done at the time of declaration using the following syntax.
Where,
size1 and size2 can be integer constants or integer expressions.
a1 to an are the values assigned to 1st row.
b1 to bn are the values assigned ti 2nd row and so on.
a,b & z – initial values to be assigned to n elements of array.
int a[4][3]={{11,22,33},
{44,55,66},
{77,88,99},
{10,11,12}};
The declaration indicates that array a has 4 rows and 3 columns. The memory
representation of this array is shown below.
11 22 33
44 55 66
77 88 99
10 11 12
The declaration indicates that array a has 4 rows and 3 columns in which only first two
columns of each row are initialized. The 3rd column of each row will be initialized with zeros
automatically. The memory representation of this array is shown below.
11 22 0
44 55 0
77 88 0
10 11 0
printf(“\n”);
}
}
Example: Create an array of characters that stores the vowels of English language.
Then, the vowels characters(a,e,i,o,& u) are either character constants or string constants.
String variable is often called as “character array”. A technique of storing more than one
character in a single variable with similar type data refers to “character array”.
Memory to character or string array gets allocated from 0th position.
When string variable is declared, size should be specified with minimum of 1 location
and maximum of 65,535 location.
By default every string is terminated by null character.
Example: “COMPUTER”
C O M P U T E R \0
Ch[0] ch[1] ch[2] ch[3] ch[4] ch[5] ch[6] ch[7] ch[8]
I) DECLARATION:
char string_name[size];
Where,
Example:
char name[30];
In above example, it is possible to store size n-1 characters in the array because the last
character would be the null character. So char name[30] can store a maximum of 29 usable
characters.
II) INITIALIZATION:
char string_name[size]={„H‟,‟E‟,‟L‟,‟L‟,‟O‟,‟\0‟};
Where,
String_name- user defined string name.
Size – determines the number of characters to be stored inside the string name.
Size of the string is not mentioned in above example. Here, the compiler will
automatically calculate the size based on the number of elements initialized.
char string_name[10]=“HELLO”;
In such cases, the compiler creates a character array of size 10; stores the value
“HELLO” in it, and finally terminates the value with a null character.
Rest of the elements in the array are automatically initialized to NULL.
H E L L O \0 \0 \0 \0 \0
Ch[0] ch[9]
I) READING STRINGS:
a) scanf(function).
b) gets( ) function.
c) getchar( ) or getch( ) function.
a) scanf( ) function:
The scanf( ) function reads characters from the keyboard and stores them in variables
according to the specified formats, which are known as format specifiers(%s).
The string can be read using scanf( ) as follows
scanf(“%s”, str);
The main pitfall with this function is that, it terminates as soon as it finds a blank space.
Example: If the user enters WELCOME TO DSATM, then str will contain only
WELCOME. This is because the moment a blank space is encountered, the string is
terminated by the scanf( ) function.
b) gets( ) function:
The string can be read using gets( ) as follows
gets( str);
char ch;
ch=getchar( );
Ex: „A‟
a) printf(function).
b) puts( ) function.
c) putchar( ) function.
a) printf( ) function:
The string can be displayed using printf( ) as follows
printf(“%s”, str);
The above syntax displays the entire contents of the arrays. The format specifier %s can
be used to display an array of characters that is terminated by the null character.
b) puts( ) function:
The string can be displayed using puts( ) as follows
puts( str);
c) putchar( ) function:
The string can be displayed using putchar( ) repeatedly to print a sequence of single
characters.
putchar( ch);
Ex: char ch=‟A‟;
putchar(name);
Ex: char name={„R‟,‟A‟,‟M‟,‟A‟};
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
printf(“what is your name ?”);
scanf(“%s”,&name);
printf(“welcome, %s\n”, name);
}
OUTPUT: What is your name ? Charu
Welcome, charu
#include<stdio.h>
#include<conio.h>
#define length 100
void main()
{
char student_name1[length]=”john”;
char student_name2[length]=”regi”;
puts(“name of 1st student”);
puts(student_name1);
puts(“name of 2nd student”);
puts(student_name2);
puts(“enter a new name for the second student”);
gets(student_name2);
puts(student_name1);
puts(student_name2);
}
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“enter a word\n”);
ch=getchar( );
putchar(ch);
}
OUTPUT: enter a word
DSATM
D
a) strcpy(dest,src)
The strcpy( ) function is used to copy the content of one string to another string.
The string to be copied can be a string which is already stored in an array of character.
The strcpy function will copy the entire string including the terminology null character, to
the new location.
SYNTAX :
strcpy(dest, source);
Where,
strcpy function has two parameters.
Destination – a string vaiable, whose value is going to be changed.
Source – the string variable, which is going to be copied to the destination.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[10], str2[10]=”hello”;
strcpy(str1,str2);
printf(“\n str1:%s”,str1);
}
OUTPUT : hello
b) strncpy(dest,src,n)
The strncpy( ) function is used to copy a specified number of characters from one string
to another string.
SYNTAX :
strncpy(dest,source,n);
Where,
strncpy( ) function has three parameters, where first two parameters are strings, which
have to be copied and the third parameter is number of characters, up to which the
second string is copied to the first string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[100], str2[100]=”hello”;
strncpy(str1,str2,2);
printf(“\n str1:%s”,str1);
}
OUTPUT : he
c) strcat(str1,str2)
SYNTAX :
strcat(string_data1, string_data2);
Where,
strcat( ) function has two arguments to add second string to first string. This means
that second string is appended to first string and the concatenated string is stored in
the first string.
C program which illustrates strcat( ) function.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50]=”programming”;
char str2[ ]=”in c”;
strcat(str1,str2);
printf(“\n str1:%s”,str1);
}
OUTPUT : programming in c
d) strncat(str1,str2,n)
The strncat( ) function is used to add two strings, where a specified number of
characters of one string are appended at the end of another string.
SYNTAX :
Where,
strncat( ) function has three parameters, where 2 parameters are the strings, which
have to be concatenated. The third parameter is number of characters upto which the
two strings are combined.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50]=”programming”;
char str2[ ]=”in c”;
strncat(str1,str2,2);
printf(“\n str1:%s”,str1);
}
OUTPUT : programming
e) strcmp(str1,str2)
SYNTAX :
strcmp(string_data1, string_data2);
Where,
strcmp( ) function takes two parameters, where 2 parameters are the strings, which
has to be compared.
f) strncmp(str1,str2,n)
SYNTAX :
strncmp(string_data1, string_data2,n);
Where,
strncmp( ) function takes three parameters, where first two parameters are the strings,
which have to be compared, and the third parameter is the number of characters up to
which the two strings are compared.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[10]=”HELLO”; char
str2[10]=”HEY”;
if(strncmp(str1,str2,2)==0)
printf(“strings are identical\n”);
else
printf(“strings are not identical\n”);
}
OUTPUT : strings are identical
g) strlen(str)
The strlen( ) function is used to find the length of the string in bytes.
The size of the operator can be used to determine the declared size of a string.
It accepts one variable or operand and returns the number of bytes allocated to it.
SYNTAX :
strlen(string_data);
Where,
strlen( ) function takes a string parameter to count the number of characters in the
string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]=”hello”;
strlen(str);
printf(“\n length of string is %d”);
}
OUTPUT : string length=5
h) strrev( )
The strrev( ) function is used to reverse a string, which takes one string argument.
SYNTAX :
strrev(string_data);
Where,
strrev( ) function takes a parameter of string data, and returns the resultant string by
reversing the order of all the characters.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[100 ];
printf(“enter string”);
gets(string1);
str1=strrev(str1);
printf(“the reverse string is=%s”,string1);
}
OUTPUT : enter string SSIT
reverse string TISS
i) strlwr( )
The strlwr( ) function is used to convert the characters stored in a string into the lower
case.
SYNTAX :
strlwr(string_data);
Where,
strlwr( ) function takes a parameter of string data, and returns the resultant string by
converting all the characters into lower case.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]={“WELCOME TO SSIT”};
str1=strlwr(str);
printf(“string in lowercase:%s”);
}
OUTPUT : welcome to ssit
j) strupr( )
The strupr( ) function is used to convert the characters stored in a string into the upper
case.
SYNTAX :
strupr(string_data);
Where,
strupr( ) function takes a parameter of string data, and returns the resultant string by
converting all the characters into upper case.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]={“welcome to ssit”};
str1=strupr(str);
printf(“string in uppercase:%s”);
}
OUTPUT: WELCOME TO SSIT
k) strchr( )
SYNTAX :
strchr(string_data, searching_character);
Where,
strchr( ) function takes two parameters, one is string data and another is the character
that has to be searched in the string data.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char *p;
p=strchr(“WELCOME TO CSE BRANCH”,‟C‟);
printf(“p”);
}
OUTPUT: C BRANCH