0% found this document useful (0 votes)
5 views25 pages

05 Arrays and Strings (CS174)

This document provides an overview of arrays and strings in C programming, detailing the structure, declaration, and initialization of one-dimensional and multi-dimensional arrays. It emphasizes the importance of array indices, memory allocation, and potential pitfalls when accessing out-of-bounds elements. Additionally, it includes examples and exercises related to array operations.

Uploaded by

saimonbsk
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)
5 views25 pages

05 Arrays and Strings (CS174)

This document provides an overview of arrays and strings in C programming, detailing the structure, declaration, and initialization of one-dimensional and multi-dimensional arrays. It emphasizes the importance of array indices, memory allocation, and potential pitfalls when accessing out-of-bounds elements. Additionally, it includes examples and exercises related to array operations.

Uploaded by

saimonbsk
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/ 25

Unit IV: Arrays and Strings

LECTURE ONE
Arrays and Strings

● An array is a collection of data that holds fixed number of values of


same type.
● An array is a collection of variables of the same type that are
referenced by a common name.
● Specific elements or variables in the array are accessed by means
of an index into the array.
For example: if you want to store marks of 100 students, you
can create an array for it.
float marks[100];
The size and type of arrays cannot be changed after its
declaration.
Arrays

● Arrays are of two types:


1) One-dimensional arrays
2) Multi-dimensional arrays
Single Dimension Arrays – 1D-Array

● How to declare an array in C?


data_type array_name[array_size];
● Where data_type is the type of each element in the array,
array_name is any valid C identifier, and array_size is the number
of elements in the array which has to be a constant value.
● For example
float mark[5];
● Here, we declared an array, mark, of floating-point type and size 5.
Meaning, it can hold 5 floating-point values.

NB : In C all arrays use zero as the index to the first element in


the array.
Ways Of Array Initializing 1-D Array :

1. Size is Specified Directly


int num[5] = {2,8,7,6,0};
In the above example we have specified the size of array as 5
directly in the initialization statement. Compiler will assign the set
of values to particular element of the array.
num[0] = 2
num[1] = 8
num[2] = 7
num[3] = 6
num[4] = 0
As at the time of compilation all the elements are at Specified
Position So This Initialization Scheme is Called as “Compile
Time Initialization“.
Ways Of Array Initializing 1-D Array :

2. Size is Specified Indirectly


In this scheme of compile time Initialization, We does not provide
size to an array but instead we provide set of values to the array.
int num[] = {2,8,7,6,0};
Compiler Counts the Number Of Elements Written Inside Pair of
Braces and Determines the Size of An Array.
After counting the number of elements inside the braces, The
size of array is considered as 5 during complete execution.
This type of Initialization Scheme is also Called as “Compile
Time Initialization“
Elements of an Array and How to access them?

● You can access elements of an array by indices.


● Suppose you declared an array mark as above. The first element
is mark[0], second element is mark[1] and so on.

● In C all arrays consist of contiguous memory locations. The lowest


address corresponds to the first element in the array while the
largest address corresponds to the last element in the array.
Few key notes..

● Arrays have 0 as the first index not 1. In this example, mark[0]


● If the size of an array is n, to access the last element, (n-1) index
is used. In this example, mark[4]
● Suppose the starting address of mark[0] is 2120. Then, the next
address, mark[1], will be 2124, address of mark[2] will be 2128
and so on. It's because the size of a float is 4 bytes.
● For Example :- array[0] 12 loc 1000 n

int array[ 5 ] ; array[1] -345 loc 1004 n

array[2] 342 locn 1008


which we might illustrate as
array[3] -30000 locn 1012
follows for a 32-bit system where
array[4] 23455 locn 1016
each int requires 4 bytes.
NB : The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1
Few key notes..

● Here
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
How to insert and print array elements?

● Here
int mark[5] = {19, 10, 8, 17, 9}
// insert different value to third element
mark[3] = 9;
// take input from the user and insert in third element
scanf("%d", &mark[2]);
// take input from the user and insert in (i+1)th element
scanf("%d", &mark[i]);
// print first element of an array
printf("%d", mark[0]);
// print ith element of an array
printf("%d", mark[i-1]);
Example: C Arrays

// Program to find the average of n (n < 10) numbers using arrays


#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number %d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average marks = %d", average);
return 0;
}
Example: C Arrays

● Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Important thing to remember when working with C
arrays

● Suppose you declared an array of 10 elements. Let's say,


int testArray[10];
● You can use the array members from testArray[0] to testArray[9].
● If you try to access array elements outside of its bound, let's say
testArray[12], the compiler may not show any error. However, this
may cause unexpected output (undefined behaviour).
● This piece of memory does not belong to the array and is likely to
be in use by some other variable in the program.
● If we are just reading a value from this location the situation isn’t
so drastic our logic just goes haywire.
● However if we are writing to this memory location we will be
changing values belonging to another section of the program
which can be catastrophic.
Multidimensional Arrays
Multidimensional Arrays…

● Multidimensional arrays of any dimension are possible in C but in


practice only two or three dimensional arrays are workable.
● The most common multidimensional array is a two dimensional
array
float x[3][4];
● Here, x is a two-dimensional (2d) array. The array can hold 12
elements. You can think the array as table with 3 row and each
row has 4 column.
Multidimensional Arrays…

● Multidimensional
Multidimensional Arrays…

● Similarly, you can declare a three-dimensional (3d) array.


● float y[2][4][3];
● The array y can hold 24 elements.
● You can think this example as: Each 2 elements have 4 elements,
which makes 8 elements and each 8 elements can have 3
elements. Hence, the total number of elements is 24.
How to initialize a multidimensional array?

● There is more than one way to initialize a multidimensional array.


● Initialization of a two dimensional array
● Different ways to initialize two dimensional array

1) int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};

2) int c[][3] = {{1, 3, 0}, {-1, 5, 9}};

3) int c[2][3] = {1, 3, 0, -1, 5, 9};


How to initialize a multidimensional array?

● Initialization of a three dimensional array.


● You can initialize a three dimensional array in a similar way like a
two dimensional array.
● For example

int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};
Example #1: Two Dimensional Array to store and
display values
Output
Output
Exercises

● Write and compile C Program to Multiply two Matrix Using


Multi-dimensional Arrays
● Write C Program to find Transpose of a Matrix
END OF LECTURE ONE
DISCUSSION QUESTIONS

You might also like