0% found this document useful (0 votes)
10 views20 pages

Understanding Arrays in C Programming

The document explains arrays in C programming, detailing one-dimensional and multi-dimensional arrays, their declaration, and how to access elements. It highlights the importance of arrays for managing large sets of similar data values and provides examples of syntax for declaring and initializing arrays. Additionally, it includes practice tasks related to array operations such as summation, searching, and matrix manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Understanding Arrays in C Programming

The document explains arrays in C programming, detailing one-dimensional and multi-dimensional arrays, their declaration, and how to access elements. It highlights the importance of arrays for managing large sets of similar data values and provides examples of syntax for declaring and initializing arrays. Additionally, it includes practice tasks related to array operations such as summation, searching, and matrix manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ARRAYS

Arrays: One Dimensional array


- Two-Dimensional Array
ARRAYS
 Large number of data values – different variables – number of
different variables.
 If variables increase – complexity also increases; confuse with
different variable names.
 There may be situations where we need to work with a large
number of similar data values.
 To make this work easier, C programming language provides a
concept called "Array".
Lets Learn in real time…
ARRAYS
 An array is a special type of variable used to store multiple values
of same data type at a time. (Or)
 An array is a collection of similar data items stored in continuous
memory locations with single name.

 Types : single & multi-dimensional


Why Arrays??
ARRAY DECLARATION
Syntax:
datatype arrayName [ size ] ; datatype arrayName [ size ] = {value1, value2, ...} ;

datatype arrayName [ ] = {value1, value2, ...} ;


Eg: int a[3]; All the three memory locations have a
common name 'a’. Accessing individual
Compiler allocates 6 bytes of memory location is not possible directly.
contiguous memory locations with a Hence compiler not only allocates the memory
single name 'a' and tells the compiler but also assigns a numerical reference value
to store three different integer to every individual memory location of an
values (each in 2 bytes of memory) array. This reference number is called "Index"
into that 6 bytes of memory or "subscript" or "indices".
ARRAY DECLARATION

int arr[] = {1,2,3,4,5};


ACCESSING AN ARRAY
The individual elements of an array are identified using the combination of 'arrayName' and
'indexValue’.
Syntax :
arrayName [ indexValue ] ;

Eg: int a[1]=100;


INPUT AN ARRAY
Getting input from the user for an array. – Using For loop
Eg: Get an array of size n and display the array
PRACTICE TASK
1. Sum and average of n elements
2. Find minimum and maximum element in an array
3. Reverse array elements and print
4. Sum of even and odd numbers separately in an array
5. Search an element in an array
6. Count the frequency of a number in an array
7. Find prime number in an array
8. Sort the elements –ascending and descending order
9. Add first element of an existing array as 10 and last element as -10
10. Separate +ve, -ve and 0 elements from array
11. Find Armstrong number in an array
12. Reverse each number in an array
MULTI-DIMENSIONAL ARRAY
C programming language allows multidimensional arrays. Here is the general form of a
multi-dimensional array declaration: type name[size1][size2]...[size
N];
For example, the following declaration creates a three-dimensional integer array:
int threedim[5][10]
[4];
The simplest form of multidimensional array is the two-dimensional array.
type arrayName [ x ][ y ];

Thus, every element in the array a is


identified by an element name of the form
a[ i ][ j ], where ‘a’ is the name of the array,
and ‘i' and ‘j’ are the subscripts that
uniquely identify each element in ‘a’.
Lets Learn in real time…
Lets Learn in real time…
Lets Learn in real time…
Lets Learn in real time…
Lets Learn in real time…
Initializing Two-Dimensional Arrays – 3 rows and 2 columns

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by
0 */
{4, 5, 6, 7} , /* initializers for row indexed by
1 */
{8, 9, 10, 11} /* initializers for row indexed by
2 */Two-Dimensional Arrays – 3 rows and 2 columns
Accessing
};

int val = a[3][2];


PRACTICE TASK
1. Addition of two matrices
2. Transpose of a matrix
3. Subtraction of two matrices
4. Sum of diagonal elements
5. Multiplication of matrix
6. Sum of outer boundary elements
7. Check if two matrix are identical
8. Determinant of a matrix
9. Print row and column sum of a matrix
10. Sum of a matrix

You might also like