0% found this document useful (0 votes)
3 views

Lecture-SP12-Array-2

The document is a lecture on structured programming focusing on arrays in C, covering topics such as properties, advantages, disadvantages, declaration, initialization, and examples of one-dimensional and two-dimensional arrays. It also discusses character arrays and strings, including their initialization, assignment, reading, and printing. Various C programs demonstrate operations like sorting, inserting, deleting, and manipulating arrays and strings.

Uploaded by

Kishor Dongare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture-SP12-Array-2

The document is a lecture on structured programming focusing on arrays in C, covering topics such as properties, advantages, disadvantages, declaration, initialization, and examples of one-dimensional and two-dimensional arrays. It also discusses character arrays and strings, including their initialization, assignment, reading, and printing. Various C programs demonstrate operations like sorting, inserting, deleting, and manipulating arrays and strings.

Uploaded by

Kishor Dongare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CSE 06131223  CSE 06131224

Structured Programming
Lecture 12
Array in C (2)

Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
A R R AY I N C

• C Array
• Properties of Array
• Advantage and disadvantage of C Array
• Declaration and Initialization of Array
• C Array Example
• Two Dimensional Array in C
• Character Arrays and Strings

Array in C 2
C Program using array:
Sorting an array of N numbers.

Array in C 3
C Program using array:

Inserting a new element into an array.

Array in C 4
C Program using array:

Deleting an element from an array.

Array in C 5
Two Dimensional (2D) Array in C
• The two-dimensional array can be defined as an array of arrays.
• The 2D array is organized as matrices which can be represented as the collection of
rows and columns.
• However, 2D arrays are created to implement a relational database look alike data
structure. It provides ease of holding the bulk of data at once which can be passed to
any number of functions wherever required.

Array in C 6
Two Dimensional (2D) Array in C
• Two-dimensional arrays are stored in memory, as
shown in the following figure.
• As with the single-dimensional arrays, each dimension
of the 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.

Array in C 7
Declaration of 2D Array in C
• The syntax to declare the 2D array is given below:
data-type arrayName[rows][columns];

• Consider the following example.


int arr[4][3];

Here, 4 is the number of rows, and 3 is the number of columns, and arr is the name of the
array variable of integer type.

Array in C 8
Initialization of 2D Array in C
• In the 1D array, we don't need to specify the size of the array if the declaration and initialization
are being done simultaneously.
• However, this will not work with 2D arrays.
• We will have to define at least the second dimension of the array. The two-dimensional array
can be declared and defined in the following way:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
int table[2][3] = {
{0,0,0},
{1,1,1}
};

Array in C 9
C Program using 2D array:

Traversing/accessing elements of 2D
array.

Array in C 10
C Program using 2D array:

Printing elements of a matrix, receiving from input.

Array in C 11
C Program using 2D array:

Adding two given matrices.

Array in C 12
C Program using 2D array:

Multiplication of two given matrices.

Array in C 13
Character Arrays and Strings
• For example, the following array (or string of characters):
char name [20];
• Initialization of strings:
• Because strings of characters are ordinary arrays they fulfill all their same rules. For example, if we
want to initialize a string of characters with predetermined values we can do it just like any other
array:
char mystring[] = { 'H', 'e', 'l', 'l', 'o', '\0’ };
• Therefore we could initialize the string mystring with values by either of these two ways:
char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char mystring [] = "Hello";
Array in C 14
Character Arrays and Strings
• Assigning values to strings:
• Since the lvalue of an assignation can only be an element of an array and not the entire
array, it would be valid to assign a string of characters to an array of char using a method
like this:
char mystring[10];
mystring[0] = 'H';
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '\0';

Array in C 15
Character Arrays and Strings
• Reading and printing strings:
• The input function scanf() can be used with %s format to read in a string of characters:
For example:
• char address[10];
• scanf(“%s”, address);
• The output function printf() can be used with %s format to print the string to the
terminal. For example:
• printf(“The given address is %s”, address);

Array in C 16
C Program using Character Arrays and Strings:

Reading and printing of multiple strings.

Array in C 17
C Program using Character Arrays and Strings:

Copying one string into another string.

Array in C 18
C Program using Character Arrays and Strings:

Reverse a string.

Array in C 19
?
Array in C
THE END
20

You might also like