SlideShare a Scribd company logo
ARRAYS
• An arrays is defined as collection of
homogenous (similar) data which is stored in
contiguous memory locations or(continuous
memory addresses) because arrays is a linear
data structure .
• Arrays can be single(1-D) or multi
dimensional(2-D).
1st
element last element
Address of an array
10 2 5 11 72
for int 1000 1002 1004 1006 1008
1st
element last element
10.5 2.68 58.7 11.6 72.9
for float 1000 1004 1008 10012 10016
1st
element last element
Calculating the length of an array
Length = upper_bound –lower_bound+1
Eg. 4-0+1= 5
Declaring Arrays
data-type variable-name[size];
Examples:-
char a[5]; /* char type value array */
float a[9]; /* float type value array */
int a[10]; /* int type value array */
a[0],a[1]…. Are known as index
The index of an array starts from 0 to size-1 means first
element of any array will be stored at a[0] address
and the last element will be at a[size - 1].
Initialization of Array
• After declaration it is necessary to initialize an
array. Otherwise, it will contain garbage value(
random value). An array can be initialized at
either compile time or at runtime. No validity
check for array index not available in c.
Compile time Initialization :- It means that
providing the value to an array in the code,
during creation of it.
Syntax:-
data-type arrayname[size] = { list of values };
• int a[4] = { 67, 87, 56, 77 };
• float a[5] = { 23.4, 6.8, 5.5 };
• int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error .
• If the values are more than the declared array size than
the compiler will give an error.
Example:-
1. #include<stdio.h>
2. void main()
3. {
4. int i;
5. int a[] = {1, 3, 4};
6. for(i = 0 ; i < 3 ; i++)
7. {
8. printf("%dn",a[i]);
9. }
10. }
• #include<stdio.h>
• int main(){
• int arr[5] = {1,-1,3};
• printf("Array elements are:");
• for(int i = 0; i < 5; i++)
• printf("%d ", arr[i]);
• return 0;
• }
Rest two place will be initialized with 0. if
int arr[5] ={} if this will leave blank , compiler will
give the error.
• #include<stdio.h>
• int main(){
• float arr[3] = {1.5,-
1.10,3.8};
• printf("Array elements
are:");
• for(int i = 0; i < 3; i++)
• printf("%f ", arr[i]);
• return 0;
• }
• #include<stdio.h>
• int main(){
• char arr[3] = {'a','b','c'};
• printf("Array elements
are:n");
• for(int i = 0; i < 3; i++)
• printf("%cn", arr[i]);
• return 0;
• }
Runtime Array initialization
• An array can also be initialized at runtime
using scanf() function. Means user will provide
the values. This approach is usually used for
initializing large arrays.
Example:-
1. scanf("%d", &a[1]); // means index 0 but
position first.
2. for(int i = 0; i < 10; i++)
scanf("%d", &a[i]);
3. for(int i = 0; i < 10; i+=2)
scanf("%d", &a[i]);
Accessing Array elements
Accessing an array elements or printing
Example:-
for(int i = 0; i < 10; i++)
printf("%d", a[i]);
To access and print elements at specified index
printf("%d", a[0]);
printf("%d", a[5]);
NOTE:- if we try to access elements, more than the size
of an array or less than 0,then it won’t show an error
but will produce the wrong output (garbage value).
WAP to calculate sum of elements of array.
• #include<stdio.h>
• int main(){
• int arr[5];
• printf("Enter array elements:"");
• for(int i = 0; i < 5; i++)
• scanf("%d", &arr[i]);
• printf("Array elements are:"");
• for(int i = 0; i < 5; i++)
• printf("%d ", arr[i]);
• int sum = 0;
• for(int i = 0; i < 5; i++)
• sum += arr[i];
• printf("Sum =%d", sum);
• }
WAP to copy date from one array to other
• #include <stdio.h>
• int main()
• {
• int a[5] = {1,2,2,4,5}, a2[5];
• for(int i = 0; i < 5; i++)
• a2[i] = a[i];
• for(int i = 0; i < 5; i++)
• printf("%d ", a2[i]);
• return 0;
• }
• #include<stdio.h>
• int main(){
• int a[10];
• printf("Array elements are:n");
• for(int i = 0; i < 10; i++)
• {
• if (i<4)
• a[i]= 1;
• else if(i>=4&&i<7)
• a[i]= 2;
• else
• a[i]= 0;
• printf("%dn", a[i]);
• }
• return 0;
• }
• Program to insert an elements at specific place in array.
• #include <stdio.h>
• int main()
• {
• int a[10],i,in,e;
• printf("enter elements in array n");
• for(i=0;i<5;i++)
• {
• scanf("%d",&a[i]);
• printf("%dt",a[i]);
• }
• printf("nenter elements and indexn");
• scanf("%dn%d",&e,&in);
• for(i=5;i>=in; i--)
• {
• a[i+1]=a[i];
• }
• a[in]=e;
• for(i=0;i<6;i++)
• {
• printf("%dt",a[i]);
• }
• return 0;
• }
Program to delete an elements from an array.
• #include <stdio.h>
• int main()
• {
• int a[5],i,in;
• printf("enter elements in array n");
• for(i=0;i<5;i++)
• scanf("%d",&a[i]);
• printf("enter index from where you want to delete elementsn");
• scanf("%d",&in);
• for(i=in;i<5;i++)
• a[i]=a[i+1];
• printf("after deletionn");
• for(i=0;i<4;i++)
• printf("%d",a[i]);
• return 0;
• }
Examples
• Program to reverse elements of an array.
#include <stdio.h>
int main()
{
int a[5],i;
printf("enter elements in array n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("elements of arrayn");
for(i=4;i>=0;i--)
printf("%d",a[i]);
return 0;
}
Merging of two arrays
• #include <stdio.h>
• int main()
• {
• int i,in=0;
• int a[3]={2,3,4}, b[3]={5,6,7}, c[6];
• for(i=0;i<3;i++)
• {
• c[in]= a[i];
• in++;
• }
• for(i=0;i<3;i++)
• {
• c[in]= b[i];
• in++;
•
• }
• for(i=0;i<6;i++)
• {
• printf("%d",c[i]);
• }
• return 0;
• }
Find the largest & smallest element in an array
• #include <stdio.h>
• int main() {
• int n;
• int a[5];
• for (int i = 0; i < 5; i++)
• {
• scanf("%d", & a[i]);
• }
• for (int i = 0; i < 5; i++) {
• if (a[0] < a[i]) {
• a[0] = a[i];
• }
• }
• printf("Largest element = %d", a[0]);
• return 0;
• }
• #include <stdio.h>
• int main() {
• int n;
• int a[5];
• for (int i = 0; i < 5; i++)
• {
• scanf("%d", & a[i]);
• }
• for (int i = 0; i < 5; i++) {
• if (a[0] > a[i]) {
• a[0] = a[i];
• }
• }
• printf("smallest element = %d", a[0]);
• return 0;
• }
Two dimensional Arrays
• It is also known as multidimensional array. It
contains a row index and a column index. Both
the row's and column's index begins with 0.
• Just like a one-dimensional array, Compile
time and runtime initialization of two
dimensional array is possible.
Declaration:-
data-type array-name[row-size][column-size]
Example:-
int a[3][4];
array ppt of c programing language for exam
Compile-time initialization
Examples:-
int a[2][2] = {1, 2, 3, 4}; // {{1, 2},{3, 4}}
Int a[2][3] = {1, 2, 3, 4}; // {{1, 2, 3},{4}}
int a[2][4] = {1, 2, 3, 4}; // {{1,2,3,4}}
First of all the values are stored in first row, and then if there is
any extra value, it goes to next row.
Example:-
int a[][3] = {
{0,0,0},
{1,1,1}
}; // no any values is assigned to row in array. It means that
initialization of any number of rows. But must specify number
of columns, else it will give a compile time error.
Runtime initialization
WAP to show the Runtime initialization
• #include<stdio.h>
• void main()
• {
• int arr[3][4];
• int i, j, ;
• printf("Enter array elements:n");
• for(i = 0; i < 3;i++)
• {
• for(j = 0; j < 4; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 3; i++)
• {
• for(j = 0; j < 4; j++)
• {
• printf("%d", arr[i][j]);
• }
• }
• }
We can also initialize 2D arrays like this
int a[2][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} };
or
int a[2][4] = {0,1,2,3,4,5,6,7};
Both are equivalent
Int [2][4] ={0} ;// all initialized with zero
int [3][4]= {1,1,1,1,2,2,2,2}
firstly 1 1 1 1
2 2 2 2
This row will be initialized by 0
Calculate the sum of all the elements
• #include<stdio.h>
• int main()
• {
• int arr[2][3];
• int i, j,sum =0;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", arr[i][j]);
• sum= sum+arr[i][j];
•
• }
• printf("n");
• }
• printf("n%d", sum);
• }
• #include<stdio.h>
• int main()
• {
• int arr[2][3];
• int i, j,sum =0;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &arr[i][j]);
• }
• }
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", arr[i][j]);
• sum= sum+arr[i][j];
•
• }
• printf("n");
• printf("n%dn", sum);
• }
•
//Printing two matrix
• #include<stdio.h>
• int main()
• {
• int a[2][3],b[2][3];
• int i, j;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &a[i][j]);
• }
• }
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &b[i][j]);
• }
• }
• printf("1st matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", a[i][j]);
• }
• printf("n");
• }
• printf("2nd matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", b[i][j]);
• }
• printf("n");
• }
• }
•
Addition of two matrix
• #include<stdio.h>
• int main()
• {
• int a[2][3],b[2][3],c[2][3];
• int i, j;
• printf("Enter array elements:n");
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &a[i][j]);
• }
• }
• for(i = 0; i < 2;i++)
• {
• for(j = 0; j < 3; j++)
• {
• scanf("%d", &b[i][j]);
• }
• }
• printf("1st matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", a[i][j]);
• }
• printf("n");
• }
• printf("2nd matrixn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• printf("%dt", b[i][j]);
• }
• printf("n");
• }
• printf("the addition of two matrix isn");
• for(i = 0; i < 2; i++)
• {
• for(j = 0; j < 3; j++)
• {
• c[i][j]= a[i][j]+b[i][j];
• printf("%dt", c[i][j]);
• }
• printf("n");
• }
•
• }
Linear search
A linear search, also known as a sequential search, is a
method of finding an element within a list. It checks
each element of the list sequentially until a match is
found or the whole list has been searched. It works on
both sorted and unsorted list of elements.
Algorithm:-
1. Begin with the leftmost element of array[] and one by
one compare searching element with each element.
2. If searching element matches with an element then
return the index.
3. If searching element does not match with any of the
elements then return -1.
#include <stdio.h>
int main()
{
int a[50], b, i, n;
printf("Enter number of elements in array:n");
scanf("%d", &n);
printf("Enter %d elements:n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search:n");
scanf("%d", &b);
for (i = 0; i < n; i++)
{
if (a[i] == b)
{
printf("Element %d found at index %d.n", b, i);
break;
}
}
if (i == n)
printf("Element %d not found.n", b);
return 0;
}
Binary search
• A Binary Search is a sorting algorithm, that is
used to search an element in a sorted array.
• A binary search technique works only on a
sorted array, so an array must be sorted to
apply binary search on the array.
• It is a searching technique that is better then
the liner search technique as the number of
iterations decreases in the binary search.
• #include <stdio.h>
• int main()
• {
• int i, first, last, middle, n, search,
array[50];
• printf("Enter number of
elementsn");
• scanf("%d", &n);
• printf("Enter %d integersn", n);
• for (i = 0; ci< n; i++)
• scanf("%d", &array[i]);
• printf("Enter value to findn");
• scanf("%d", &search);
• first = 0;
• last = n - 1;
• middle = (first+last)/2;
• while (first <= last) {
• if (array[middle] < search)
• first = middle + 1;
• else if (array[middle] ==
search) {
• printf("%d found at location
%d.n", search, middle+1);
• break;
• }
• else
• last = middle - 1;
• middle = (first + last)/2;
• }
• if (first > last)
• printf("Not found! %d isn't
present in the list.n", search);
• return 0;
• }
• #include <iostream>
• using namespace std;
• int main()
• {
• int i,j,s,a[10];
•
• for(i=0;i<4;i++)
• {
• cin>>a[i];
• }
• for(i=0;i<4-1;i++)
• {
• for(j=0;j<4-1;j++)
• {
• if(a[j]>a[j+1])
•
• {
• s=a[j];
• a[j]=a[j+1];
• a[j+1]=s;
• }
• }
• }
• cout<<"the sorted array
is"<<endl;
• for(i=0;i<8;i++)
• {
• cout<<a[i]<<endl;
• }
•
• return 0;
• } //bubble sort
Insertion sort
• #include <iostream>
• using namespace std;
• int main()
• {
• int i,j,s,a[10];
•
• for(i=0;i<4;i++)
• {
• cin>>a[i];
• }
• for(i=1;i<4;i++)
• {
• s=a[i];
• j=i-1;
• while(j>=0&&a[j]>s)
• {
• a[j+1]=a[j];
• j--;
• }
• a[j+1]=s;
• }
• cout<<"the sorted array
is"<<endl;
• for(i=0;i<4;i++)
• {
• cout<<a[i]<<endl;
• }
•
• return 0;
• }

More Related Content

PPTX
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
unit-2-dsa.pptx
sayalishivarkar1
 
PPTX
Mcs011 solved assignment by divya singh
DIVYA SINGH
 
PDF
codes.txt.pdf code presentation engineering
ts5092207
 
PDF
11 1. multi-dimensional array eng
웅식 전
 
PPTX
arrays in c programming - example programs
stalin721831
 
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
unit-2-dsa.pptx
sayalishivarkar1
 
Mcs011 solved assignment by divya singh
DIVYA SINGH
 
codes.txt.pdf code presentation engineering
ts5092207
 
11 1. multi-dimensional array eng
웅식 전
 
arrays in c programming - example programs
stalin721831
 

Similar to array ppt of c programing language for exam (20)

PDF
C++ L04-Array+String
Mohammad Shaker
 
PPT
Array i imp
Vivek Kumar
 
PPT
Arrays 06.ppt
ahtishamtariq511
 
PPTX
Looping statements.pptx for basic in c language
aryann1217z
 
PPTX
Looping statements c language basics ppt with example
aryann1217z
 
PPT
SP-First-Lecture.ppt
FareedIhsas
 
PPTX
Java arrays
Mohammed Sikander
 
PDF
COgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdf
mbpgbca
 
PPTX
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
PPTX
Arrays
Mitali Chugh
 
PPT
Array
Samsil Arefin
 
PPT
arrays
teach4uin
 
PPT
array2d.ppt
DeveshDewangan5
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
PDF
COA_remaining_lab_works_077BCT033.pdf
JavedAnsari236392
 
PDF
2 BytesC++ course_2014_c4_ arrays
kinan keshkeh
 
PDF
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
C++ L04-Array+String
Mohammad Shaker
 
Array i imp
Vivek Kumar
 
Arrays 06.ppt
ahtishamtariq511
 
Looping statements.pptx for basic in c language
aryann1217z
 
Looping statements c language basics ppt with example
aryann1217z
 
SP-First-Lecture.ppt
FareedIhsas
 
Java arrays
Mohammed Sikander
 
COgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdf
mbpgbca
 
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
Arrays
Mitali Chugh
 
arrays
teach4uin
 
array2d.ppt
DeveshDewangan5
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
COA_remaining_lab_works_077BCT033.pdf
JavedAnsari236392
 
2 BytesC++ course_2014_c4_ arrays
kinan keshkeh
 
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
mitivete
 
Ad

Recently uploaded (20)

PPT
Lecture in network security and mobile computing
AbdullahOmar704132
 
PDF
Queuing formulas to evaluate throughputs and servers
gptshubham
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
ternal cell structure: leadership, steering
hodeeesite4
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Lecture in network security and mobile computing
AbdullahOmar704132
 
Queuing formulas to evaluate throughputs and servers
gptshubham
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Ppt for engineering students application on field effect
lakshmi.ec
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
ternal cell structure: leadership, steering
hodeeesite4
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Ad

array ppt of c programing language for exam

  • 1. ARRAYS • An arrays is defined as collection of homogenous (similar) data which is stored in contiguous memory locations or(continuous memory addresses) because arrays is a linear data structure . • Arrays can be single(1-D) or multi dimensional(2-D). 1st element last element
  • 2. Address of an array 10 2 5 11 72 for int 1000 1002 1004 1006 1008 1st element last element 10.5 2.68 58.7 11.6 72.9 for float 1000 1004 1008 10012 10016 1st element last element Calculating the length of an array Length = upper_bound –lower_bound+1 Eg. 4-0+1= 5
  • 3. Declaring Arrays data-type variable-name[size]; Examples:- char a[5]; /* char type value array */ float a[9]; /* float type value array */ int a[10]; /* int type value array */ a[0],a[1]…. Are known as index The index of an array starts from 0 to size-1 means first element of any array will be stored at a[0] address and the last element will be at a[size - 1].
  • 4. Initialization of Array • After declaration it is necessary to initialize an array. Otherwise, it will contain garbage value( random value). An array can be initialized at either compile time or at runtime. No validity check for array index not available in c. Compile time Initialization :- It means that providing the value to an array in the code, during creation of it. Syntax:- data-type arrayname[size] = { list of values };
  • 5. • int a[4] = { 67, 87, 56, 77 }; • float a[5] = { 23.4, 6.8, 5.5 }; • int marks[4] = { 67, 87, 56, 77, 59 }; // Compile time error . • If the values are more than the declared array size than the compiler will give an error. Example:- 1. #include<stdio.h> 2. void main() 3. { 4. int i; 5. int a[] = {1, 3, 4}; 6. for(i = 0 ; i < 3 ; i++) 7. { 8. printf("%dn",a[i]); 9. } 10. }
  • 6. • #include<stdio.h> • int main(){ • int arr[5] = {1,-1,3}; • printf("Array elements are:"); • for(int i = 0; i < 5; i++) • printf("%d ", arr[i]); • return 0; • } Rest two place will be initialized with 0. if int arr[5] ={} if this will leave blank , compiler will give the error.
  • 7. • #include<stdio.h> • int main(){ • float arr[3] = {1.5,- 1.10,3.8}; • printf("Array elements are:"); • for(int i = 0; i < 3; i++) • printf("%f ", arr[i]); • return 0; • } • #include<stdio.h> • int main(){ • char arr[3] = {'a','b','c'}; • printf("Array elements are:n"); • for(int i = 0; i < 3; i++) • printf("%cn", arr[i]); • return 0; • }
  • 8. Runtime Array initialization • An array can also be initialized at runtime using scanf() function. Means user will provide the values. This approach is usually used for initializing large arrays. Example:- 1. scanf("%d", &a[1]); // means index 0 but position first. 2. for(int i = 0; i < 10; i++) scanf("%d", &a[i]); 3. for(int i = 0; i < 10; i+=2) scanf("%d", &a[i]);
  • 9. Accessing Array elements Accessing an array elements or printing Example:- for(int i = 0; i < 10; i++) printf("%d", a[i]); To access and print elements at specified index printf("%d", a[0]); printf("%d", a[5]); NOTE:- if we try to access elements, more than the size of an array or less than 0,then it won’t show an error but will produce the wrong output (garbage value).
  • 10. WAP to calculate sum of elements of array. • #include<stdio.h> • int main(){ • int arr[5]; • printf("Enter array elements:""); • for(int i = 0; i < 5; i++) • scanf("%d", &arr[i]); • printf("Array elements are:""); • for(int i = 0; i < 5; i++) • printf("%d ", arr[i]); • int sum = 0; • for(int i = 0; i < 5; i++) • sum += arr[i]; • printf("Sum =%d", sum); • }
  • 11. WAP to copy date from one array to other • #include <stdio.h> • int main() • { • int a[5] = {1,2,2,4,5}, a2[5]; • for(int i = 0; i < 5; i++) • a2[i] = a[i]; • for(int i = 0; i < 5; i++) • printf("%d ", a2[i]); • return 0; • }
  • 12. • #include<stdio.h> • int main(){ • int a[10]; • printf("Array elements are:n"); • for(int i = 0; i < 10; i++) • { • if (i<4) • a[i]= 1; • else if(i>=4&&i<7) • a[i]= 2; • else • a[i]= 0; • printf("%dn", a[i]); • } • return 0; • }
  • 13. • Program to insert an elements at specific place in array. • #include <stdio.h> • int main() • { • int a[10],i,in,e; • printf("enter elements in array n"); • for(i=0;i<5;i++) • { • scanf("%d",&a[i]); • printf("%dt",a[i]); • } • printf("nenter elements and indexn"); • scanf("%dn%d",&e,&in); • for(i=5;i>=in; i--) • { • a[i+1]=a[i]; • } • a[in]=e; • for(i=0;i<6;i++) • { • printf("%dt",a[i]); • } • return 0; • }
  • 14. Program to delete an elements from an array. • #include <stdio.h> • int main() • { • int a[5],i,in; • printf("enter elements in array n"); • for(i=0;i<5;i++) • scanf("%d",&a[i]); • printf("enter index from where you want to delete elementsn"); • scanf("%d",&in); • for(i=in;i<5;i++) • a[i]=a[i+1]; • printf("after deletionn"); • for(i=0;i<4;i++) • printf("%d",a[i]); • return 0; • }
  • 15. Examples • Program to reverse elements of an array. #include <stdio.h> int main() { int a[5],i; printf("enter elements in array n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("elements of arrayn"); for(i=4;i>=0;i--) printf("%d",a[i]); return 0; }
  • 16. Merging of two arrays • #include <stdio.h> • int main() • { • int i,in=0; • int a[3]={2,3,4}, b[3]={5,6,7}, c[6]; • for(i=0;i<3;i++) • { • c[in]= a[i]; • in++; • } • for(i=0;i<3;i++) • { • c[in]= b[i]; • in++; • • } • for(i=0;i<6;i++) • { • printf("%d",c[i]); • } • return 0; • }
  • 17. Find the largest & smallest element in an array • #include <stdio.h> • int main() { • int n; • int a[5]; • for (int i = 0; i < 5; i++) • { • scanf("%d", & a[i]); • } • for (int i = 0; i < 5; i++) { • if (a[0] < a[i]) { • a[0] = a[i]; • } • } • printf("Largest element = %d", a[0]); • return 0; • } • #include <stdio.h> • int main() { • int n; • int a[5]; • for (int i = 0; i < 5; i++) • { • scanf("%d", & a[i]); • } • for (int i = 0; i < 5; i++) { • if (a[0] > a[i]) { • a[0] = a[i]; • } • } • printf("smallest element = %d", a[0]); • return 0; • }
  • 18. Two dimensional Arrays • It is also known as multidimensional array. It contains a row index and a column index. Both the row's and column's index begins with 0. • Just like a one-dimensional array, Compile time and runtime initialization of two dimensional array is possible. Declaration:- data-type array-name[row-size][column-size] Example:- int a[3][4];
  • 20. Compile-time initialization Examples:- int a[2][2] = {1, 2, 3, 4}; // {{1, 2},{3, 4}} Int a[2][3] = {1, 2, 3, 4}; // {{1, 2, 3},{4}} int a[2][4] = {1, 2, 3, 4}; // {{1,2,3,4}} First of all the values are stored in first row, and then if there is any extra value, it goes to next row. Example:- int a[][3] = { {0,0,0}, {1,1,1} }; // no any values is assigned to row in array. It means that initialization of any number of rows. But must specify number of columns, else it will give a compile time error.
  • 21. Runtime initialization WAP to show the Runtime initialization • #include<stdio.h> • void main() • { • int arr[3][4]; • int i, j, ; • printf("Enter array elements:n"); • for(i = 0; i < 3;i++) • { • for(j = 0; j < 4; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 3; i++) • { • for(j = 0; j < 4; j++) • { • printf("%d", arr[i][j]); • } • } • }
  • 22. We can also initialize 2D arrays like this int a[2][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} }; or int a[2][4] = {0,1,2,3,4,5,6,7}; Both are equivalent Int [2][4] ={0} ;// all initialized with zero int [3][4]= {1,1,1,1,2,2,2,2} firstly 1 1 1 1 2 2 2 2 This row will be initialized by 0
  • 23. Calculate the sum of all the elements • #include<stdio.h> • int main() • { • int arr[2][3]; • int i, j,sum =0; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", arr[i][j]); • sum= sum+arr[i][j]; • • } • printf("n"); • } • printf("n%d", sum); • } • #include<stdio.h> • int main() • { • int arr[2][3]; • int i, j,sum =0; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &arr[i][j]); • } • } • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", arr[i][j]); • sum= sum+arr[i][j]; • • } • printf("n"); • printf("n%dn", sum); • } •
  • 24. //Printing two matrix • #include<stdio.h> • int main() • { • int a[2][3],b[2][3]; • int i, j; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &a[i][j]); • } • } • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &b[i][j]); • } • } • printf("1st matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", a[i][j]); • } • printf("n"); • } • printf("2nd matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", b[i][j]); • } • printf("n"); • } • } •
  • 25. Addition of two matrix • #include<stdio.h> • int main() • { • int a[2][3],b[2][3],c[2][3]; • int i, j; • printf("Enter array elements:n"); • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &a[i][j]); • } • } • for(i = 0; i < 2;i++) • { • for(j = 0; j < 3; j++) • { • scanf("%d", &b[i][j]); • } • } • printf("1st matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", a[i][j]); • } • printf("n"); • } • printf("2nd matrixn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • printf("%dt", b[i][j]); • } • printf("n"); • } • printf("the addition of two matrix isn"); • for(i = 0; i < 2; i++) • { • for(j = 0; j < 3; j++) • { • c[i][j]= a[i][j]+b[i][j]; • printf("%dt", c[i][j]); • } • printf("n"); • } • • }
  • 26. Linear search A linear search, also known as a sequential search, is a method of finding an element within a list. It checks each element of the list sequentially until a match is found or the whole list has been searched. It works on both sorted and unsorted list of elements. Algorithm:- 1. Begin with the leftmost element of array[] and one by one compare searching element with each element. 2. If searching element matches with an element then return the index. 3. If searching element does not match with any of the elements then return -1.
  • 27. #include <stdio.h> int main() { int a[50], b, i, n; printf("Enter number of elements in array:n"); scanf("%d", &n); printf("Enter %d elements:n", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("Enter a number to search:n"); scanf("%d", &b); for (i = 0; i < n; i++) { if (a[i] == b) { printf("Element %d found at index %d.n", b, i); break; } } if (i == n) printf("Element %d not found.n", b); return 0; }
  • 28. Binary search • A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. • A binary search technique works only on a sorted array, so an array must be sorted to apply binary search on the array. • It is a searching technique that is better then the liner search technique as the number of iterations decreases in the binary search.
  • 29. • #include <stdio.h> • int main() • { • int i, first, last, middle, n, search, array[50]; • printf("Enter number of elementsn"); • scanf("%d", &n); • printf("Enter %d integersn", n); • for (i = 0; ci< n; i++) • scanf("%d", &array[i]); • printf("Enter value to findn"); • scanf("%d", &search); • first = 0; • last = n - 1; • middle = (first+last)/2; • while (first <= last) { • if (array[middle] < search) • first = middle + 1; • else if (array[middle] == search) { • printf("%d found at location %d.n", search, middle+1); • break; • } • else • last = middle - 1; • middle = (first + last)/2; • } • if (first > last) • printf("Not found! %d isn't present in the list.n", search); • return 0; • }
  • 30. • #include <iostream> • using namespace std; • int main() • { • int i,j,s,a[10]; • • for(i=0;i<4;i++) • { • cin>>a[i]; • } • for(i=0;i<4-1;i++) • { • for(j=0;j<4-1;j++) • { • if(a[j]>a[j+1]) • • { • s=a[j]; • a[j]=a[j+1]; • a[j+1]=s; • } • } • } • cout<<"the sorted array is"<<endl; • for(i=0;i<8;i++) • { • cout<<a[i]<<endl; • } • • return 0; • } //bubble sort
  • 31. Insertion sort • #include <iostream> • using namespace std; • int main() • { • int i,j,s,a[10]; • • for(i=0;i<4;i++) • { • cin>>a[i]; • } • for(i=1;i<4;i++) • { • s=a[i]; • j=i-1; • while(j>=0&&a[j]>s) • { • a[j+1]=a[j]; • j--; • } • a[j+1]=s; • } • cout<<"the sorted array is"<<endl; • for(i=0;i<4;i++) • { • cout<<a[i]<<endl; • } • • return 0; • }