0% found this document useful (0 votes)
27 views33 pages

Unit II

This document provides an overview of one-dimensional arrays in C. It discusses that an array is a fixed sized collection of related data items of the same type. It then explains that C supports one-dimensional, two-dimensional, and multidimensional arrays. One-dimensional arrays allow storing a list of items using a single variable name and one subscript. The document demonstrates declaring and initializing a one-dimensional integer array of size 10. It also provides an example C program that initializes and prints a one-dimensional array.

Uploaded by

Dark soul
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
27 views33 pages

Unit II

This document provides an overview of one-dimensional arrays in C. It discusses that an array is a fixed sized collection of related data items of the same type. It then explains that C supports one-dimensional, two-dimensional, and multidimensional arrays. One-dimensional arrays allow storing a list of items using a single variable name and one subscript. The document demonstrates declaring and initializing a one-dimensional integer array of size 10. It also provides an example C program that initializes and prints a one-dimensional array.

Uploaded by

Dark soul
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 33

LECTURE 14

ONE DIMENSIONAL ARRAYS

1
Array :

An array is a fixed sized sequenced collection of related data items of


same data type.
In its simplest form an array can be used to represent a list of numbers or
a list of names using a single name.
We can use an array name salary to represent a set of salaries of a group
employees in an organization. We can refer to the individual salaries by
writing a number called index or subscript in brackets after the array name.
for example salary[10] represents the salary of 10th employee.
While the complete set of values referred to as an array, individual values
are called elements.
2
C supports the following types of arrays :

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.

Declaration of one-dimensional arrays:


Like any other variable, arrays must be declared before they are used.
The general form of array declaration is :

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

• Array elements are like normal variables


c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
– Perform operations in subscript. If x equals
3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]

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

TWO DIMENSIONAL ARRAYS

12
Two-dimensional arrays :

One-dimensional array can store a list of values. There could be


situations where a table of values will have to be stored.
We can think of a table as a matrix consist of rows and columns.
C allows to define such table of items by using two-dimensional
array such as v[3][4].

13
The general form of declaration of a two-dimensional
array is :

type array_name[row_size][column_size];

Each dimension of array is indexed from zero to its maximum size


minus one, the first index selects the row and the second index
selects the column within that row.

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.

The general form of multi-dimensional array is :


type array_name[s1][s2][s3[]……….[sm];
Where si is the size of ith dimension .

For example :
int survey[3][5][12];
float table[5][4][5][3];

survey is a three-dimensional array, whereas table is a four-dimensional array.

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

Using scanf function :

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.

To write a character in its integer representation, we may


write it as an integer.
For example :
char x=‘a’;
printf(“%d”,x);
Will display 97 ASCII value of a on the screen.
24
It is also possible to perform arithmetic operations on the characters
and variables
For example :
int x;
x=‘z’-1;
is a valid statement. The ASCII value of z is 122 and therefore the
statement will assign 121 to x.

25
LECTURE 20

26
String-Handling Functions

C library supports a large number of string-handling functions that can be


used to carry out many of the string manipulations.

Most commonly used string functions are :


strcat( ) – concatenates two strings
strcmp( ) – compares two strings
strcpy – copies one string into another
strlen – finds the length of a string
These functions are defined in string.h header file.

27
strcat function

The strcat function joins two strings together.


It takes the following form :
strcat(string1,string2);
Where string1 & string two are character arrays.
When the strcat function is excuted, string two is appended to
string1.
It does so by removing the null character at the end of string1 and
placing string2 from there.
The string at string2 remains unchanged.
Size of string1 should be enough to accommodate the final string.
strcat function may also append a string constant to a string variable.
28
strcmp() function

The strcmp compares two functions identified by the arguments and


has a value of 0 if they are equal.
If they are not, it has numeric difference between the first
nonmatching characters in the strings.
It takes the form :
strcmp(string1,string2);
string1 and string2 may be string variables or string constants.

29
strcpy function

strcpy copies one string into another.


It takes the form :
strcpy(string1,string2);
It assigns the contents of string2 to string1.
string2 may be a character array variable or a string constant.

30
strlen function

This function counts and returns the number of characters in a


string.
It takes the form :
n=strlen(string);
Where n is an integer variable, which receives the value of the
length of the string.
The argument may be a string constant.
The counting ends at the first null character.

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

strstr – It is a two-parameter function that can be used to locate a


sub-string in a string.
It takes the form :
strstr(string1,string2);
The function strstr searches the string1 to see whether the string2 is
contained in string1.
if yes function returns the position of the first occurrence of the sub-
string. Otherwise, it returns a NULL pointer.

33

You might also like