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

10. Array

The document provides an overview of various concepts in C programming, focusing on arrays, their properties, initialization, and operations such as sorting and searching. It includes examples of one-dimensional and two-dimensional arrays, as well as string handling and common string functions. Additionally, it presents exercises for practical application of the discussed topics.

Uploaded by

tilkeshtailor46
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

10. Array

The document provides an overview of various concepts in C programming, focusing on arrays, their properties, initialization, and operations such as sorting and searching. It includes examples of one-dimensional and two-dimensional arrays, as well as string handling and common string functions. Additionally, it presents exercises for practical application of the discussed topics.

Uploaded by

tilkeshtailor46
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C Language

Switch
Pointers
Structure
Recursion

Functions
Linked list

Array
Stack

Lecture
On

Linked list
“ Arrays ”

Structure

Recursion
By: Bhagirath Singh Chauhan

Stack
Mob.: 9829275869
Pointers
C Language
Switch
Pointers Array
Structure
Recursion

Functions # An array is defined as the collection of homogeneous (similar) type


Linked list

of data items stored at contiguous memory locations.


Array
# It is simply a grouping of like-type data. We can declare an array in
Stack

the following way:


datatype arrayname [ size] ;
Example:
int arr[5];
Array elements are accessed by using an integer index. Array index
starts with 0 and goes till size of array minus 1.
arr[0] arr[1] arr[2] arr[3] arr[4]
arr
1000 1004 1008 1012 1016
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Array Introduction
Structure
Recursion

Functions # Compared to the basic data type, it is a derived data type.


Linked list

Array
# The array is the simplest data structure where each data
element can be randomly accessed by using its index
Stack

number.
# Data structure is a format for arranging and storing the data
in an organized way.

A 10 8 4 15 20 All integers

B ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ All characters

C ‘a’ ‘b’ 10 1.2 ‘c’ Not allowed

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Properties of Array
Structure
Recursion

Functions
Linked list

Array Points to be noted:


Stack

# The elements in the array should be of same type.


# Subscript (array size) cannot be negative.
# Subscript must always be an integer.

Types of Array
1. One-dimensional arrays
2. Two-dimensional arrays
3. Multidimensional arrays

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Initialization of Array
Structure
Recursion

Functions The simplest way to initialize an array is by using the index of each
Linked list

element. We can initialize each element of the array by using the


Array index.
Stack

int arr[5];

arr[0] arr[1] arr[2] arr[3] arr[4]

arr
1000 1004 1008 1012 1016
10
arr[0] = 10;
arr[1] = 22;
22
76
arr[2] = 76;
arr[3] = 11;
11
30
arr[4] = 30;
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Declaration with Initialization
Structure
Recursion

Functions We can initialize the array at the time of declaration.


Linked list

Array
Stack

int arr[5] = { 10, 22, 76, 11, 30 };


int arr[ ]={10, 22, 76, 11, 30};
char name[15] = {‘B’, ‘h’, ‘a’, ‘g’, ‘i‘, ‘r’, ‘a’, ‘t’, ‘h’, ‘\0’ };
char name[ ] = “Bhagirath” ;

arr[0] arr[1] arr[2] arr[3] arr[4]

arr 10 22 76 11 30
1000 1004 1008 1012 1016

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Array: Run Time Initialization
Structure
Recursion

Functions Example:
Linked list

Array #include <stdio.h>


Stack

int main()
{
int arr[5], i ;
printf(“Enter 5 integer values:”) ;
for ( i = 0; i < 5; i ++)
scanf(“%d ”, &arr[i] ) ; arr[0] arr[1] arr[2] arr[3] arr[4]
printf(“Elements are: ”);
arr 8 6 10 22 9
for ( i = 0; i < 5; i ++)
printf(“%d ”, arr[i] ) ; 1000 1004 1008 1012 1016
return 0;
}
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Variable Length Array
Structure
Recursion

Functions int main()


Linked list

{
Array
int size, i;
Stack

printf("Enter the size of array: ");


Variable length arrays scanf("%d",&size);
are also known as int arr[size];
runtime sized or printf("Enter %d numbers: ", size);
variable sized arrays. for( i = 0; i < size; i++)
The size of such array scanf("%d", &arr[i]);
is defined at run-time. printf("Elements of array: ");
for( i = 0; i < size; i++)
printf("%4d",arr[i]);
return 0;
}
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers
Example: Sum and Average
Structure
Recursion

int main() {
Functions int arr[5], i, sum = 0;
Linked list

float avg;
Array printf(“Enter 5 integer values: ”);
Stack

for( i = 0; i < 5; i ++ )
{
arr[0] arr[1] arr[2] arr[3] arr[4]
scanf(“%d”, &arr[i] );
sum += arr[i];
}
arr 8 12 10 20 4 avg = (float) sum / 5;
1000 1004 1008 1012 1016 printf(“Elements of an array are: ”);
for( i = 0; i < 5; i ++ )
printf(“%d ”, arr[i] ) ;
printf(“\nSum = %d\n”, sum);
printf(“Average = %.2f\n”, avg);
return 0;
}
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers
Linear Search Program
Structure
Recursion

#include <stdio.h> for( i = 0 ; i < n; i++)


Functions #define MAX 100 {
Linked list

int main() if( arr[i] == item)


Array { {
Stack

int arr[MAX], n, flag = 0, item, i; flag = 1;


printf(“Enter the size of array: “); break;
scanf(“%d”,&n); 8 }
}
printf(“Enter %d elements: ”, n);
if( flag == 1)
for( i = 0; i < n; i++) printf(“item is present” );
scanf(“%d”,&arr[i]); else
printf(“Enter search item: ”); printf(“item is not present”);
scanf(“%d”,&item); return 0;
}
item
arr 12 8 9 10 11 42 3 18 …..
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Arrays: Bubble Sort
Structure
Recursion

Functions arr 66 22 77 44 11 88 33 55
Linked list

Array
Pass-1 arr
Stack

Pass Required:
Pass-2 arr n–1

Pass-3 arr for(i = 0; i < n-1; i++)


for(j = 0; j < n-i-1; j++)
Pass-4 arr

Pass-5 arr

Pass-6 arr

Pass-7 arr
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Arrays: Bubble Sort Program
Structure
Recursion

Functions int main()


Linked list

{
void bubbleSort(int a[], int n)
Array int arr[8] = { 66, 22, 77, 44, 11, 88, 33, 55};
Stack

{ int i;
int i, j, temp; printf("Unsorted list is:\n");
for(i = 0; i < n-1; i++) for(i = 0; i < 8; i++)
for(j = 0; j < n-i-1; j++) printf("%d ",arr[i]);
{ bubbleSort(arr, 8);
if(a[j] > a[j+1] )
printf("\nSorted list is:\n");
{
for(i = 0; i < 8; i++)
temp = a[j];
printf("%d ",arr[i]);
a[j] = a[j+1];
return 0;
a[j+1] = temp;
}
} item
}
} arr 66 22 77 44 11 88 33 55
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Character Array or String
Structure
Recursion

Functions
Linked list

String in C programing is a sequence of characters terminated


Array
with a null character ‘\0’. Strings are defined as an array of
Stack

characters.

Declaration of Strings

Declaring a string is as simple as declaring a one-dimensional


array. Below is the basic syntax for declaring a string.
char str[size];
Note: There is an extra terminating character which is the Null
character (‘\0’) used to indicate the termination of a string.

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Most used String Functions
Structure
Recursion

Functions Function Description


Linked list

Array strlen(str) Returns the length of string str.


Stack

Compares the first string with the second string. If


strcmp(str1,str2)
strings are the same it returns 0.
strcpy(str1,str2) Copies the contents of string str2 to string str1.
Concat str1 string with str2 string and the result is
strcat(str1, str2)
stored in the first string.
strlwr(str) Converts string str to lowercase.
strupr(str) Converts string str to uppercase.
strstr(str1,str2) Find the first occurrence of str2 in str1.
Strrev(str) Reverse the string str.
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Two Dimensional Arrays
Structure
Recursion

Functions # A two dimensional arrays has two subscript/indexes.


Linked list

# The first subscript refers to the row, and the second to the column.
Array
# A two-dimensional array looks like a table consisting of rows and
Stack

columns.
# 2-D array is declared as:
int arr[3][3];

arr arr[0][0] arr[0][1] arr[0][2]

arr[1][0] arr[1][1] arr[1][2]

arr[2][0] arr[2][1] arr[2][2]

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Initialization of Two Dimensional Array
Structure
Recursion

Functions int arr[3][3] = { { 11, 22, 33}, {44, 55, 66}, {77, 88, 99} };
Linked list

OR
Array
Stack

int arr[3][3] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
OR
int arr[ ][3] = { { 11, 22, 33}, {44, 55, 66}, {77, 88, 99} };
OR
int arr[3][3] = {
{ 11, 22, 33}, arr 11 22 33
{ 44, 55, 66},
{ 77, 88, 99} 44 55 66
};
77 88 99

By: Bhagirath Singh Chauhan (Mob.: 9829275869)


C Language
Switch
Pointers Example: Sum of two Matrices
Structure
Recursion

int main() {
Functions
int A[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} } , B[3][3] = { {4,2,1}, {3,5,9}, {1,6,2} };
Linked list

int C[3][3], i, j;
Array
for ( i = 0; i < 3; i ++)
Stack

{
for ( j = 0; j < 3; j++ )
C[i][j] = A[i][j] + B[i][j]; A 1 2 3 B 4 2 1
} 4 5 6 3 5 9
printf(“Sum of A and B matrices: ”); 1 6 2
7 8 9
for ( i = 0; i < 3; i ++)
{
for ( j = 0; j < 3; j++ ) C
printf(“%d ”, C[i][j] );
printf(“\n”);
}
return 0;
}
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Exercises
Structure
Recursion

Functions 1. Write a program to search an element and to find how many times it is
Linked list

present in the array.


Array
2. Write a program to find the sum of diagonal elements in a matrix.
Stack

3. Write a program to find the second largest number in an array.


4. Write a program to remove the duplicate elements of the array.
5. Write a program to merge two arrays and print the merged array in
ascending order.
6. Write a program to insert an element into an sorted array of integers.
7. Write a program to display only the negative elements of the array.
8. Print Transpose of a matrix.
9. Write a program in ‘C’ to read characters one by one and display it back.
10. Write a program to find a word in a given line of text.
11. Write a program to sort the given list of names in ascending order.
By: Bhagirath Singh Chauhan (Mob.: 9829275869)
C Language
Switch
Pointers Any Query...
Structure
Recursion

Functions
Linked list

Array
Stack

You might also like