SlideShare a Scribd company logo
Computer
Programming in “C”
Unit iV: arraYS in ‘C’
ARRAY in C
• An array is a set of elements of the same data type that are referred
to by the same name.
• An array in C programming language can be defined as “number of
memory locations, each of which can store the same data type and
which can be referred by the same variable name”.
• All arrays consist of continuous memory locations.
• The lowest memory address corresponds to the first element and
the highest address to the last element.
Types of Arrays
Single dimensional Array/ Matrix
Linear Array
Single subscript Two subscripts Multiple subscripts
ARRAYS in C
• Array is a collection of homogenous data stored under unique name.
• The values in an array are called as “Elements of an Array”.
• These elements are accessed by numbers called as “subscripts or
index numbers”.
• Arrays may be of any variable type.
• An Array is declared as
data_type array_name [size]
e.g. int sample[10]
Program for ARRAY
• Output:
The elements of array a
2 3 5 6 7
•
Initializing a Character Array
• Strings are one-
dimensional array of
characters terminated by a
null character ‘0’
• To hold the null character
at the end of an array, the
size of the character array
containing the string is one
more than the number of
characters in the word.
•
Functions in Character Array
Functions
used in
Array
Functions
used in
Array
Output:
Single Dimensional Array
• A list of items can be given one variable name using only one subscript.
Such a variable is called as single dimensional array or single
subscripted variable.
• An array which is having either a single row or a single column is
termed as one dimensional array. One dimensional array have the
subscripts in a linear manner.
Program to input 10 numbers from user
and find total of them
• • Output:
Enter any ten numbers: 1
2
3
4
5
6
7
8
9
10
Total is: 55
#include<stdio.h>
int main( )
{
int a[25], n, i ;
float , ;
printf("Enter the number of terms:") ;
scanf("%d", &n) ;
printf("n Enter the Numbers:n") ;
for (i = 0 ; i < n ; i++)
{
scanf("%d", &a[i]) ;
}
12
for (i = 0 ; i < n ; i++)
{
sum = sum + a[i] ;
avg = sum / n ;
}
printf("n Mean of entered
Numbers are : %f ", mean) ;
return ( 0 ) ;
}
Write a Program in c to find the mean of n
numbers using array
• Write a Program in C to find the mean of n numbers using array
13
#include < stdio.h >
int main( )
{
int a[25], n, i ;
float mean = 0, sum = 0 ;
printf(" Enter the Numbers of
terms: ") ;
scanf("%d ",& n) ;
printf("n Enter the Numbers :
n") ;
for ( i = 0 ; i < =n ; i++)
{
scanf("%d ",& a[i]) ;
}
for ( i = 0 ; i < n ; i++)
{
sum = sum + a[i] ;
avg = sum / n ;
}
printf("n Mean of
entered Numbers are :
%f ",mean) ;
return ( 0 ) ;
}
C Program to Copy All the
Elements of One Array to
Another Array
14
#include <stdio.h>
int main()
{
int a[5] = { 3, 6, 9, 2, 5 }, n = 5;
int b[n], i;
printf("The first array is :");
for (i = 0; i < n; i++)
{
printf("%d", a[i]);
}
// copying elements from one array to another
for (i = 0; i < n; i++)
{
b[i] = a[i];
}
// displaying array after copying the
//elements from one array to other
printf("nThe second array is :");
for (i = 0; i < n; i++) {
printf("%d ", b[i]);
}
return 0;
}
Program to find the smallest and largest number from an array.
•
Program to search an element in the array
Program to search an element in the array
• Output:
Enter elements of an array:
10 9 8 7 6 5 4 3 2 1
Enter item to search: 6
Item found at location: 5
18
Reversing Arrays in C
#include<stdio.h>
int main()
{
int i, n, arr[n], i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int rev[n], j = 0;
for(j = n-1; j >= 0; j--)
{
rev[j] = arr[i];
}
printf("The Reversed array: ");
for(j = 0; j < n; j++)
{
printf("%d ", rev[j]);
} }
2D Array (Two Dimensional Array)
• 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.
19
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
data_type array_name [rows] [columns];
Consider the following example.
int twodimen[4][3];
2D Array (Two Dimensional Array)
• Output:
• Array
contents are:
10 20
30 40
20
Multi Dimensional Array
• C language allows array of 3 or more dimensions is called as multi-
dimensional array.
• The syntax to declare the multi-dimentional array is given below.
• data_type array_name [S1] [S2] [S3] …. [Sn]
• Consider the following example.
• int sample1 [4] [5] [5];
• float sample2 [10] [20] [30];
21
Multi Dimensional Array
•
• Output:
Enter matrix of 3*3:
1 2 3 4 5 6 7 8 9
Matrix is:
1 2 3
4 5 6
7 8 9
C Program to add 2 Matrices
• •
C Program to add 2 Matrices
• Output:
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
2 1
Sum of entered matrices
6 8
5 5
Program to Transpose a matrix
CP PPT_Unit IV computer programming in c.pdf
Sorting:
27
What is Sorting?
A Sorting Algorithm is used to rearrange a given array or list of elements according to a
comparison operator on the elements. The comparison operator is used to decide the
new order of elements in the respective data structure.
Sorting Algorithms:
• Bubble Sort
• Insertion Sort
• Selection Sort
• Merge Sort
• Quick Sort
• Heap Sort
28
Linear Searching :
• Linear search is the simplest method for searching. In Linear search technique of searching;
the element to be found in searching the elements to be found is searched sequentially in
the list.
• This method can be performed on a sorted or an unsorted list
In case of a sorted list searching starts from 0th element and continues until the element is
found from the list or the element whose value is greater than (assuming the list is sorted
in
ascending order), the value being searched is reached.
29
Linear Search Algorithm
• Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first
element in the array.
Step 3: If both are matched, display “Target element is found” and
terminate the Linear Search function.
Step 4: If both are not matched, compare the search element with the
next element in the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target)
element is compared with the last element of the array.
Step 6 :– If the last element in the list does not match, the Linear Search
Function will be terminated, and the message “Element is not found”
will be displayed.
30
Linear Searching C program:
31
Bubble Sort Algorithm
• Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order.
32
Input: arr[] = {5, 1, 4, 2, 8}
First Pass:
Bubble sort starts with very first two elements, comparing them to check which one is
greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps
since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Bubble Sort Algorithm
33
Second Pass:
Now, during second iteration it should look like this:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Third Pass:
Now, the array is already sorted, but our algorithm does not know if it is completed.
The algorithm needs one whole pass without any swap to know it is sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Program for sorting an array in ascending order
#include<stdio.h>
int main()
{
int array[50], n, i, j, temp;
printf("Enter the number of elements:n");
scanf("%d",&n);
printf("Enter the %d integers:n",n);
for(i=0;i<n;++i)
{
scanf("%d",&array[i]);
}
Program for sorting an array in ascending order (Continued )
//Ascending Order
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
printf("Sorted list in
ascending order:n");
for(i=0;i<n;i++)
printf("%dn",array[i]);
return 0;
}
36
Arrays in C: Bubble Sort in Ascending order
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%dt",a[i]);
}
}
#include<stdio.h>
int main(void)
{
int a[100], i, j, temp, n;
printf("n Enter the max no.of Elements
to Sort: n");
scanf("%d",&n);
printf("n Enter the Elements : n");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
• Given an array arr[] of N elements, the task is to write a function to
search a given element x in arr[].
• Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}, x = 110;
Output: 6
Explanation: Element x is present at index 6
• Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}, x = 175;
Output: -1
Explanation: Element x is not present in arr[].
37
Insertion Sort Algorithm
• Insertion sort algorithm sorts the dataset by transferring
one element at a time to the partially sorted array. Thus,
this sorting algorithm has a low overhead.
38
Consider the following example:
40 30 10 70 50 20 60
We consider 40 as the partially sorted array. When we consider 30, it is less than 40. So we swap
them. Then, we consider 30 and 40 are in the partially sorted array.
30 40 10 70 50 20 60
Now, we consider 10. 10 is less than 30. So, we place the elements as below. 10, 30 and 40 are in
the partially sorted array.
10 30 40 70 50 20 60
Insertion Sort Algorithm
39
Now, we consider 70. It is greater than 40, so there is no need for any
movement. 10, 30, 40, 70 are in the partially sorted array.
Now, consider 50. It is less than 70 but greater than 40. We can place them in
the correct position. 10,30, 40, 50,70 are now in the partially sorted array.
10 30 40 50 70 20 60
Now, consider 20. It is greater than 10 but less than 30. We can place it in the
correct position. 10,20,30,40,50, 70 are in the partially sorted array.
10 20 30 40 50 70 60
Consider 60. It is less than 70 but greater than 50. We can place it in the
correct position.
10 20 30 40 50 60 70
Now, we can see that all the elements are sorted. Here, the number of swaps
in insertion sort is minimized but the number of comparisons is still high.
Insertion Sort Algorithm
• Before going through the program, lets see the steps
of insertion sort with the help of an example.
Input elements: 89 17 8 12 0
•
Step 1: 89 17 8 12 0 (the bold elements are sorted
list and non-bold unsorted list)
•
Step 2: 17 89 8 12 0 (each element will be
removed from unsorted list and placed at the right
position in the sorted list)
Step 3: 8 17 89 12 0
Step 4: 8 12 17 89 0
Step 5: 0 8 12 17 89 40
Insertion Sort Algorithm
41
#include<stdio.h>
int main()
{
int i, j, count, temp, Array[25];
printf("How many numbers u are going
to enter?:");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// This loop would store the input
numbers in array
for(i=0; i<count; i++)
scanf("%d", &Array[i]);
// Implementation of insertion sort algorithm
for(i=1; i<count; i++)
{
temp=Array[i];
j=i-1;
while((temp<Array[j]) && (j>=0))
{
Array[j+1]=Array[j];
j=j-1;
}
Array[j+1]=temp;
}
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf("%d", Array[i]);
return 0;
}
OUTPUT:
2D Array (Two Dimensional Array)
• 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.
42
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Consider the following example.
int twodimen[4][3];
43
C program to Insert an element in an Array
First get the element to be inserted, say x
Then get the position at which this element is to be inserted, say pos
Then shift the array elements from this position to one position forward(towards right),
and do this for all the other elements next to pos.
Insert the element x now at the position pos, as this is now empty.
Ad

More Related Content

Similar to CP PPT_Unit IV computer programming in c.pdf (20)

CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
Praveen M Jigajinni
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
Abdul Samee
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Epsiba1
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
Vasim Pathan
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
object oriented programming lab manual .docx
object oriented programming  lab manual .docxobject oriented programming  lab manual .docx
object oriented programming lab manual .docx
Kirubaburi R
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsnaModule 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
Intro C# Book
 
object oriented programing in python and pip
object oriented programing in python and pipobject oriented programing in python and pip
object oriented programing in python and pip
LakshmiMarineni
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
Unit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdfUnit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
Abdul Samee
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Epsiba1
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
Vasim Pathan
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
object oriented programming lab manual .docx
object oriented programming  lab manual .docxobject oriented programming  lab manual .docx
object oriented programming lab manual .docx
Kirubaburi R
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsnaModule 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
object oriented programing in python and pip
object oriented programing in python and pipobject oriented programing in python and pip
object oriented programing in python and pip
LakshmiMarineni
 
Unit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdfUnit-5-Part1 Array in Python programming.pdf
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 

Recently uploaded (20)

Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Ad

CP PPT_Unit IV computer programming in c.pdf

  • 1. Computer Programming in “C” Unit iV: arraYS in ‘C’
  • 2. ARRAY in C • An array is a set of elements of the same data type that are referred to by the same name. • An array in C programming language can be defined as “number of memory locations, each of which can store the same data type and which can be referred by the same variable name”. • All arrays consist of continuous memory locations. • The lowest memory address corresponds to the first element and the highest address to the last element.
  • 3. Types of Arrays Single dimensional Array/ Matrix Linear Array Single subscript Two subscripts Multiple subscripts
  • 4. ARRAYS in C • Array is a collection of homogenous data stored under unique name. • The values in an array are called as “Elements of an Array”. • These elements are accessed by numbers called as “subscripts or index numbers”. • Arrays may be of any variable type. • An Array is declared as data_type array_name [size] e.g. int sample[10]
  • 5. Program for ARRAY • Output: The elements of array a 2 3 5 6 7 •
  • 6. Initializing a Character Array • Strings are one- dimensional array of characters terminated by a null character ‘0’ • To hold the null character at the end of an array, the size of the character array containing the string is one more than the number of characters in the word. •
  • 10. Single Dimensional Array • A list of items can be given one variable name using only one subscript. Such a variable is called as single dimensional array or single subscripted variable. • An array which is having either a single row or a single column is termed as one dimensional array. One dimensional array have the subscripts in a linear manner.
  • 11. Program to input 10 numbers from user and find total of them • • Output: Enter any ten numbers: 1 2 3 4 5 6 7 8 9 10 Total is: 55
  • 12. #include<stdio.h> int main( ) { int a[25], n, i ; float , ; printf("Enter the number of terms:") ; scanf("%d", &n) ; printf("n Enter the Numbers:n") ; for (i = 0 ; i < n ; i++) { scanf("%d", &a[i]) ; } 12 for (i = 0 ; i < n ; i++) { sum = sum + a[i] ; avg = sum / n ; } printf("n Mean of entered Numbers are : %f ", mean) ; return ( 0 ) ; } Write a Program in c to find the mean of n numbers using array
  • 13. • Write a Program in C to find the mean of n numbers using array 13 #include < stdio.h > int main( ) { int a[25], n, i ; float mean = 0, sum = 0 ; printf(" Enter the Numbers of terms: ") ; scanf("%d ",& n) ; printf("n Enter the Numbers : n") ; for ( i = 0 ; i < =n ; i++) { scanf("%d ",& a[i]) ; } for ( i = 0 ; i < n ; i++) { sum = sum + a[i] ; avg = sum / n ; } printf("n Mean of entered Numbers are : %f ",mean) ; return ( 0 ) ; }
  • 14. C Program to Copy All the Elements of One Array to Another Array 14 #include <stdio.h> int main() { int a[5] = { 3, 6, 9, 2, 5 }, n = 5; int b[n], i; printf("The first array is :"); for (i = 0; i < n; i++) { printf("%d", a[i]); } // copying elements from one array to another for (i = 0; i < n; i++) { b[i] = a[i]; } // displaying array after copying the //elements from one array to other printf("nThe second array is :"); for (i = 0; i < n; i++) { printf("%d ", b[i]); } return 0; }
  • 15. Program to find the smallest and largest number from an array. •
  • 16. Program to search an element in the array
  • 17. Program to search an element in the array • Output: Enter elements of an array: 10 9 8 7 6 5 4 3 2 1 Enter item to search: 6 Item found at location: 5
  • 18. 18 Reversing Arrays in C #include<stdio.h> int main() { int i, n, arr[n], i; printf("Enter the size of the array: "); scanf("%d", &n); printf("Enter the elements: "); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } int rev[n], j = 0; for(j = n-1; j >= 0; j--) { rev[j] = arr[i]; } printf("The Reversed array: "); for(j = 0; j < n; j++) { printf("%d ", rev[j]); } }
  • 19. 2D Array (Two Dimensional Array) • 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. 19 Declaration of two dimensional Array in C The syntax to declare the 2D array is given below. data_type array_name [rows] [columns]; Consider the following example. int twodimen[4][3];
  • 20. 2D Array (Two Dimensional Array) • Output: • Array contents are: 10 20 30 40 20
  • 21. Multi Dimensional Array • C language allows array of 3 or more dimensions is called as multi- dimensional array. • The syntax to declare the multi-dimentional array is given below. • data_type array_name [S1] [S2] [S3] …. [Sn] • Consider the following example. • int sample1 [4] [5] [5]; • float sample2 [10] [20] [30]; 21
  • 22. Multi Dimensional Array • • Output: Enter matrix of 3*3: 1 2 3 4 5 6 7 8 9 Matrix is: 1 2 3 4 5 6 7 8 9
  • 23. C Program to add 2 Matrices • •
  • 24. C Program to add 2 Matrices • Output: Enter the number of rows and columns of matrix 2 2 Enter the elements of first matrix 1 2 3 4 Enter the elements of second matrix 5 6 2 1 Sum of entered matrices 6 8 5 5
  • 27. Sorting: 27 What is Sorting? A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure.
  • 28. Sorting Algorithms: • Bubble Sort • Insertion Sort • Selection Sort • Merge Sort • Quick Sort • Heap Sort 28
  • 29. Linear Searching : • Linear search is the simplest method for searching. In Linear search technique of searching; the element to be found in searching the elements to be found is searched sequentially in the list. • This method can be performed on a sorted or an unsorted list In case of a sorted list searching starts from 0th element and continues until the element is found from the list or the element whose value is greater than (assuming the list is sorted in ascending order), the value being searched is reached. 29
  • 30. Linear Search Algorithm • Step 1: First, read the search element (Target element) in the array. Step 2: In the second step compare the search element with the first element in the array. Step 3: If both are matched, display “Target element is found” and terminate the Linear Search function. Step 4: If both are not matched, compare the search element with the next element in the array. Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with the last element of the array. Step 6 :– If the last element in the list does not match, the Linear Search Function will be terminated, and the message “Element is not found” will be displayed. 30
  • 31. Linear Searching C program: 31
  • 32. Bubble Sort Algorithm • Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. 32 Input: arr[] = {5, 1, 4, 2, 8} First Pass: Bubble sort starts with very first two elements, comparing them to check which one is greater. ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
  • 33. Bubble Sort Algorithm 33 Second Pass: Now, during second iteration it should look like this: ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) Third Pass: Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
  • 34. Program for sorting an array in ascending order #include<stdio.h> int main() { int array[50], n, i, j, temp; printf("Enter the number of elements:n"); scanf("%d",&n); printf("Enter the %d integers:n",n); for(i=0;i<n;++i) { scanf("%d",&array[i]); }
  • 35. Program for sorting an array in ascending order (Continued ) //Ascending Order for(i=0; i<n; i++) for(j=i+1; j<n; j++) { if(array[i]>array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } printf("Sorted list in ascending order:n"); for(i=0;i<n;i++) printf("%dn",array[i]); return 0; }
  • 36. 36 Arrays in C: Bubble Sort in Ascending order for(i=0; i<n; i++) for(j=i+1; j<n; j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } for(i=0; i<n; i++) { printf("%dt",a[i]); } } #include<stdio.h> int main(void) { int a[100], i, j, temp, n; printf("n Enter the max no.of Elements to Sort: n"); scanf("%d",&n); printf("n Enter the Elements : n"); for(i=0; i<n; i++) { scanf("%d", &a[i]); }
  • 37. • Given an array arr[] of N elements, the task is to write a function to search a given element x in arr[]. • Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}, x = 110; Output: 6 Explanation: Element x is present at index 6 • Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}, x = 175; Output: -1 Explanation: Element x is not present in arr[]. 37
  • 38. Insertion Sort Algorithm • Insertion sort algorithm sorts the dataset by transferring one element at a time to the partially sorted array. Thus, this sorting algorithm has a low overhead. 38 Consider the following example: 40 30 10 70 50 20 60 We consider 40 as the partially sorted array. When we consider 30, it is less than 40. So we swap them. Then, we consider 30 and 40 are in the partially sorted array. 30 40 10 70 50 20 60 Now, we consider 10. 10 is less than 30. So, we place the elements as below. 10, 30 and 40 are in the partially sorted array. 10 30 40 70 50 20 60
  • 39. Insertion Sort Algorithm 39 Now, we consider 70. It is greater than 40, so there is no need for any movement. 10, 30, 40, 70 are in the partially sorted array. Now, consider 50. It is less than 70 but greater than 40. We can place them in the correct position. 10,30, 40, 50,70 are now in the partially sorted array. 10 30 40 50 70 20 60 Now, consider 20. It is greater than 10 but less than 30. We can place it in the correct position. 10,20,30,40,50, 70 are in the partially sorted array. 10 20 30 40 50 70 60 Consider 60. It is less than 70 but greater than 50. We can place it in the correct position. 10 20 30 40 50 60 70 Now, we can see that all the elements are sorted. Here, the number of swaps in insertion sort is minimized but the number of comparisons is still high.
  • 40. Insertion Sort Algorithm • Before going through the program, lets see the steps of insertion sort with the help of an example. Input elements: 89 17 8 12 0 • Step 1: 89 17 8 12 0 (the bold elements are sorted list and non-bold unsorted list) • Step 2: 17 89 8 12 0 (each element will be removed from unsorted list and placed at the right position in the sorted list) Step 3: 8 17 89 12 0 Step 4: 8 12 17 89 0 Step 5: 0 8 12 17 89 40
  • 41. Insertion Sort Algorithm 41 #include<stdio.h> int main() { int i, j, count, temp, Array[25]; printf("How many numbers u are going to enter?:"); scanf("%d",&count); printf("Enter %d elements: ", count); // This loop would store the input numbers in array for(i=0; i<count; i++) scanf("%d", &Array[i]); // Implementation of insertion sort algorithm for(i=1; i<count; i++) { temp=Array[i]; j=i-1; while((temp<Array[j]) && (j>=0)) { Array[j+1]=Array[j]; j=j-1; } Array[j+1]=temp; } printf("Order of Sorted elements: "); for(i=0;i<count;i++) printf("%d", Array[i]); return 0; } OUTPUT:
  • 42. 2D Array (Two Dimensional Array) • 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. 42 Declaration of two dimensional Array in C The syntax to declare the 2D array is given below. data_type array_name[rows][columns]; Consider the following example. int twodimen[4][3];
  • 43. 43 C program to Insert an element in an Array First get the element to be inserted, say x Then get the position at which this element is to be inserted, say pos Then shift the array elements from this position to one position forward(towards right), and do this for all the other elements next to pos. Insert the element x now at the position pos, as this is now empty.