Unit II
Unit II
1
Array :
1. One-dimensional arrays
2. Two-dimensional arrays
3. multidimensional arrays
3
One-dimensional arrays :
A list of items can be given one variable name using only one subscript and
such a variable is called a single-subscripted variable or one-dimensional
array.
type variable_name[size];
type specifies the type of elements of array, such as int, float or char and the
size indicates the maximum number of elements that can be stored inside the
array.
For example :
float height[10];
4
declares the height to be an array containing 10 real elements.
Name of array
(Note that all
elements of this
array have the
same name, c)
c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
Position number
of the element
within array c 5
LECTURE 15
6
Arrays
7
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array
– Number of elements
arrayType arrayName[ numberOfElements ];
– Examples:
int c[ 10 ];
float myArray[ 3284 ];
• Declaring multiple arrays of same type
– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
8
Examples Using Arrays
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
• All elements 0
– If too many a syntax error is produced syntax error
– C arrays have no bounds checking
• If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore 5 element array
9
1 /* Fig. 6.8: fig06_08.c
2 Histogram printing program */
3 #include <stdio.h>
4 #define SIZE 10
5
6 int main()
1. Initialize
7
8
{
int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
array
9 int i, j;
10
11 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
12
13 for ( i = 0; i <= SIZE - 1; i++ ) { 2. Loop
14 printf( "%7d%13d ", i, n[ i ]) ;
15
16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */
17
18
printf( "%c", '*' );
3. Print
19 printf( "\n" );
20 }
21
22 return 0;
23 }
10
Element Value Histogram
0 19 *******************
1 3 ***
2
3
15
7
***************
*******
Program
Output
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
11
LECTURE 16
12
Two-dimensional arrays :
13
The general form of declaration of a two-dimensional
array is :
type array_name[row_size][column_size];
14
Column 0 Column 1 Column 2 Column 3
a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 0
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
15
• Initialization
– int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
– Initializers grouped by row in braces
– If not enough, unspecified elements set to
zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
16
LECTURE 17
MULTIDIMENSIONAL ARRAYS
17
Multi-dimensional array :
C allows arrays of three or more dimensions. The exact limit is determined by the
complier.
For example :
int survey[3][5][12];
float table[5][4][5][3];
18
LECTURE 18
STRINGS
19
Strings
A string is sequence of characters that is treated as a single data item. Any group
of characters defined between double quotation marks is a string constant.
Declaring strings :
C does not support strings as a data type. However, it allows us to represent
strings as character arrays. In C therefore, a string variable is any valid C vaiable
name and is always declared as an array of characters.
char string_name[size];
the size determines the number of characters in string.
For example :
char city[10];
when compiler assigns a character string to a character array, it automatically
supplies a null character (‘\0’) at the end of the string. Therefore, the size should
be equal to the maximum number of characters in the string plus one. 20
Reading strings from terminal
The familiar input function scanf can be used with %s format specification to
read in a string of characters.
For example :
char address[15];
scanf(“%s”,address);
the problem with the scanf function is that it terminates its input on the first white
space it finds.
21
Reading with getchar and gets functions :
A single character can be read from the keyboard using getchar. We can use this function
repeatedly to read successive single characters from the input and place them into a
character array. The reading is terminated when newline character (‘\n’) is entered and the
null character is then inserted at the end of the string.
The getchar function call takes the form :
char ch;
ch=getchar( );
Another and more convenient method of reading a string of text containing whitespaces is
to use the library function gets available in the <stdio.h> header file. This is simple function
with one string parameter and called as under:
gets(str);
str is a string variable declared properly. It reads characters into str from the keyboard
until a newline character is encountered and then appends a null character to the string.
Unlike scanf, it does not skip whitespaces.
22
LECTURE 19
ARITHMATIC OPERATIONS ON
STRINGS
23
C allows us to manipulate characters the same way we do
with numbers.
Whenever a character constant or character variable is
used in an expression, it is automatically converted into
integer value by the system.
25
LECTURE 20
26
String-Handling Functions
27
strcat function
29
strcpy function
30
strlen function
31
Other string handling functions
strncpy – it copies the left-most n characters of the sourse string to
the target string variable.
This is a three parameter function & has the following form :
strncpy(string1,string2,n);
strncmp – this fuction has three parameter and has the following
form :
strncmp(string1,string2,n);
This compares the left-most n characters of string1 to string2
and returns :
a) O if they are equal.
b) Negative number, if string1 is less than string2.
c) Positive number, otherwise. 32
strncat - This function concatenates the left-most n characters of
second string to the end of first string.
It takes the following form :
strncat(string1,string2,n);
33