SlideShare a Scribd company logo
Array
An array is a finite set of same type of data items. In other word it is a collection of homogeneous
data items.
The elements of an array are stored in successive memory locations. An element of an array is
referred by array name and index number (subscript).
Types of array
1. One dimensional (1-D) or linear array
2. Two dimensional (2-D) array
1-D Array
An array that can be represented by only one dimension such as row or column and that holds
finite number of same type of data items is called one dimensional array.
Array B
1 2 3 4 5 6 7 8 9
0 10 12 13 19 20 23 18 29
Fig: Graphical representation of 1-D array
1, 2, …………., 9 index number
0, 10, …….., 29 data items or elements of the array
B the array name
Symbolically the element of the array is expressed as Bi or B[i], which denotes ith
element of the
array.
Store/retrieve an element into/from an array
int a[10], i, n;
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
scanf(“%d”, &a[i]);
}
for(i=1;i<=n;i++)
{
printf(“%d”, a[i]);
}
C Program to Store/retrieve an element into/from an array
#include <stdio.h>
int main()
{
int a[100], i, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
printf("Array elementsn");
for (i = 1; i <= n; i++)
{
printf("%dt", a[i]);
}
return 0;
}
Sample Output
Enter number of elements in array
5
Enter 5 elements
1
2
3
4
5
Array elements
1 2 3 4 5
Algorithm to search the largest element of a list
1. Input x[1….n]
2. for(i=1;i<=n;i++)
Store data to x[i];
3. large=x[1];
4. for(i=2;i<=n;i++)
if(large<x[i])
large=x[i];
5. Output: The largest number (print the value of large)
C program to search the largest element of a list
#include<stdio.h>
#include<conio.h>
int main()
{
int x[100],i,n,large;
printf("Enter the number of elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
large=x[1];
for(i=2;i<=n;i++)
{
if(large<x[i])
{
large=x[i];
}
}
printf("The largest among the %d elements is %d",n,large);
return 0;
}
Sample Output
Enter the number of elements: 5
12
6
7
9
0
The largest among the 5 elements is 12
Algorithm to search a particular element from a list
1. Input: A set of data in array a, and variable x. i.e., the target element
a[1….n], x;
2. found=0
3. for (i=1; i<=n; i++)
{
if (a[i]==x)
location=i;
found= 1;
break;
}
4. Output: if (found==1)
print”FOUND” message and location.
else print ”NOT FOUND” message
C program to search a particular element from a list
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);
return 0;
}
Sample Output
Enter the number of elements in array
6
Enter 6 integer(s)
3
4
0
2
9
7
Enter the number to search
0
0 is present at location 3.
Algorithm to find out the summations of even and odd numbers
1. Input: An array and variable (to store the results of summations)
A[1….n], sum_odd=0, sum_even=0;
2. for (i=1; i<=n; i++)
{
if(A[i]%2==0), sum_even=sum_even+A[i];
else sum_odd=sum_odd+A[i];
}
3. Output: Summationof odd numbers (print sum_odd) and
Summation of even numbers (print sum_even)
C program to find out the summations of even and odd numbers
#include <stdio.h>
int main()
{
int array[100], i, num, sum_of_odd = 0, sum_of_even = 0;
printf("Enter the number of elementsn");
scanf("%d", &num);
printf("Enter the elements n");
for(i=1;i<=num;i++)
{
scanf("%d",&array[i]);
}
for (i = 1; i <= num; i++)
{
if (array[i] % 2 == 0)
sum_of_even = sum_of_even + array[i];
else
sum_of_odd = sum_of_odd + array[i];
}
printf("Sum of all odd numbers = %dn", sum_of_odd);
printf("Sum of all even numbers = %dn", sum_of_even);
return 0;
}
Sample Output
Enter the number of elements
5
Enter the elements
1
2
5
3
9
Sum of all odd numbers = 18
Sum of all even numbers = 2
Algorithm to find out the summations of even and odd indexed numbers
1. Input: An array and variable (to store the results of summations)
A[1….n], sum_odd=0, sum_even=0;
2. for (i=1; i<=n; i++)
{
if(i%2==0), sum_even=sum_even+A[i];
else sum_odd=sum_odd+A[i];
}
3. Output: Summation of numbers in odd indices (print sum_odd) and
Summation of numbers in even indices (print sum_even)
C program to find out the summations of even and odd indexed numbers
#include <stdio.h>
int main()
{
int array[100], i, num, sum_of_odd = 0, sum_of_even = 0;
printf("Enter the number of elementsn");
scanf("%d", &num);
printf("Enter the elements n");
for(i=1;i<=num;i++)
{
scanf("%d",&array[i]);
}
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
sum_of_even = sum_of_even + array[i];
else
sum_of_odd = sum_of_odd + array[i];
}
printf("Sum of all odd number indices = %dn", sum_of_odd);
printf("Sum of all even number indices = %dn", sum_of_even);
return 0;
}
Sample Output
Enter the number of elements
5
Enter the elements
1
2
5
8
7
Sum of all odd number indices = 13
Sum of all even number indices = 10
Algorithm to insert an element into an array
1. Input: An array A[1….n], the position of insertion m and the data x.
2. Increase the size of the array, A[1….n+1]
3. for (i=n; i>=m; i--)
A[i+1]=A[i];
4. A[m]=x;
5. Output: The array, A with size n+1
C program to insert an element into an array
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (c = 1; c <= n; c++)
{
scanf("%d", &array[c]);
}
printf("Enter the location where you wish to insert an
elementn");
scanf("%d", &position);
printf("Enter the value to insertn");
scanf("%d", &value);
for (c = n; c >= position; c--)
{
array[c+1] = array[c];
}
array[position] = value;
printf("Resultant array isn");
for (c = 1; c <= n+1; c++)
{
printf("%dn", array[c]);
}
return 0;
}
Sample Output
Enter number of elements in array
5
Enter 5 elements
1
2
3
7
5
Enter the location where you wish to insert an element
4
Enter the value to insert
0
Resultant array is
1
2
3
0
7
5
Algorithm to delete an element from an array
1. Input: An array A[1….n], the position of deletion m.
2. for (i=m; i<n; i++)
A[i]=A[i+1];
3. Output: The updated array, A
C program to delete an element from an array
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( c = 1 ; c <= n ; c++ )
{
scanf("%d", &array[c]);
}
printf("Enter the location where you wish to delete
elementn");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.n");
else
{
for ( c = position ; c < n ; c++ )
{
array[c] = array[c+1];
}
printf("Resultant array isn");
for( c = 1 ; c < n ; c++ )
printf("%dn", array[c]);
}
return 0;
}
Sample Output
Enter number of elements in array
4
Enter 4 elements
8
9
0
3
Enter the location where you wish to delete element
2
Resultant array is
8
0
3

More Related Content

PPTX
C programming
PPT
PPSX
C programming array & shorting
PDF
C Prog - Array
PDF
Data Structure using C
PPTX
PPT
Arrays
C programming
C programming array & shorting
C Prog - Array
Data Structure using C
Arrays

What's hot (19)

PDF
design and analysis of algorithm Lab files
DOC
Basic c programs updated on 31.8.2020
DOCX
SaraPIC
PDF
Common problems solving using c
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
PDF
C Programming Example
DOCX
Python lab manual all the experiments are available
DOCX
Chapter 8 c solution
PDF
Data Structure in C Programming Language
PPTX
C Programming Example
PPTX
LAB PROGRAMS SARASWATHI RAMALINGAM
PDF
Numerical analysis
PDF
c-programming-using-pointers
PDF
SPL 11.1 | Problems on Loop , Nested Loop
PPT
Thesis PPT
PPTX
4. chapter iii
PPT
All important c programby makhan kumbhkar
design and analysis of algorithm Lab files
Basic c programs updated on 31.8.2020
SaraPIC
Common problems solving using c
Let us C (by yashvant Kanetkar) chapter 3 Solution
C Programming Example
Python lab manual all the experiments are available
Chapter 8 c solution
Data Structure in C Programming Language
C Programming Example
LAB PROGRAMS SARASWATHI RAMALINGAM
Numerical analysis
c-programming-using-pointers
SPL 11.1 | Problems on Loop , Nested Loop
Thesis PPT
4. chapter iii
All important c programby makhan kumbhkar
Ad

Similar to 1D Array (20)

PDF
programs on arrays.pdf
PPT
presentation_arrays_1443553113_140676.ppt
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
PDF
SlideSet_4_Arraysnew.pdf
PPT
array.ppt
PDF
02 arrays
DOC
Ada file
PPTX
Examples sandhiya class'
PDF
Array Programs.pdf
PPSX
C Programming : Arrays
DOCX
ADA FILE
PDF
Array (data structure using c++).PPT presentation
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
DOCX
DAA Lab File C Programs
PPT
Arrays
PPTX
arrays in c programming - example programs
PPTX
C Programming Language Part 8
PPTX
one dimentional array on programming with C
PDF
PPTX
Module_3_Arrays - Updated.pptx............
programs on arrays.pdf
presentation_arrays_1443553113_140676.ppt
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
SlideSet_4_Arraysnew.pdf
array.ppt
02 arrays
Ada file
Examples sandhiya class'
Array Programs.pdf
C Programming : Arrays
ADA FILE
Array (data structure using c++).PPT presentation
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
DAA Lab File C Programs
Arrays
arrays in c programming - example programs
C Programming Language Part 8
one dimentional array on programming with C
Module_3_Arrays - Updated.pptx............
Ad

More from A. S. M. Shafi (20)

DOCX
Data Warehouse Schema (Star, Snowflake).docx
PDF
Correlation Analysis in Machine Learning.pdf
PDF
Naive Bayes and Decision Tree Algorithm.pdf
PDF
Frequent Pattern Growth Mining Algorithm.pdf
PDF
Direct Hashing and Pruning Algorithm in Data MIning.pdf
PDF
Association Rule Mining with Apriori Algorithm.pdf
PDF
HITS Algorithm in Data and Web MIning.pdf
PDF
Page Rank Algorithm in Data Mining and Web Application.pdf
PDF
K Nearest Neighbor Classifier in Machine Learning.pdf
PDF
K Means Clustering Algorithm in Machine Learning.pdf
PDF
2D Transformation in Computer Graphics
PDF
3D Transformation in Computer Graphics
PDF
Projection
PDF
2D Transformation
PDF
Line drawing algorithm
PDF
Fragmentation
PDF
File organization
PDF
Bankers algorithm
PDF
RR and priority scheduling
PDF
Fcfs and sjf
Data Warehouse Schema (Star, Snowflake).docx
Correlation Analysis in Machine Learning.pdf
Naive Bayes and Decision Tree Algorithm.pdf
Frequent Pattern Growth Mining Algorithm.pdf
Direct Hashing and Pruning Algorithm in Data MIning.pdf
Association Rule Mining with Apriori Algorithm.pdf
HITS Algorithm in Data and Web MIning.pdf
Page Rank Algorithm in Data Mining and Web Application.pdf
K Nearest Neighbor Classifier in Machine Learning.pdf
K Means Clustering Algorithm in Machine Learning.pdf
2D Transformation in Computer Graphics
3D Transformation in Computer Graphics
Projection
2D Transformation
Line drawing algorithm
Fragmentation
File organization
Bankers algorithm
RR and priority scheduling
Fcfs and sjf

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
master seminar digital applications in india
PPTX
Institutional Correction lecture only . . .
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharma ospi slides which help in ospi learning
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPH.pptx obstetrics and gynecology in nursing
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
master seminar digital applications in india
Institutional Correction lecture only . . .
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
TR - Agricultural Crops Production NC III.pdf

1D Array

  • 1. Array An array is a finite set of same type of data items. In other word it is a collection of homogeneous data items. The elements of an array are stored in successive memory locations. An element of an array is referred by array name and index number (subscript). Types of array 1. One dimensional (1-D) or linear array 2. Two dimensional (2-D) array 1-D Array An array that can be represented by only one dimension such as row or column and that holds finite number of same type of data items is called one dimensional array. Array B 1 2 3 4 5 6 7 8 9 0 10 12 13 19 20 23 18 29 Fig: Graphical representation of 1-D array 1, 2, …………., 9 index number 0, 10, …….., 29 data items or elements of the array B the array name Symbolically the element of the array is expressed as Bi or B[i], which denotes ith element of the array. Store/retrieve an element into/from an array int a[10], i, n; scanf(“%d”, &n); for(i=1;i<=n;i++) { scanf(“%d”, &a[i]); } for(i=1;i<=n;i++) { printf(“%d”, a[i]);
  • 2. } C Program to Store/retrieve an element into/from an array #include <stdio.h> int main() { int a[100], i, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } printf("Array elementsn"); for (i = 1; i <= n; i++) { printf("%dt", a[i]); } return 0; } Sample Output Enter number of elements in array 5 Enter 5 elements 1 2 3 4
  • 3. 5 Array elements 1 2 3 4 5 Algorithm to search the largest element of a list 1. Input x[1….n] 2. for(i=1;i<=n;i++) Store data to x[i]; 3. large=x[1]; 4. for(i=2;i<=n;i++) if(large<x[i]) large=x[i]; 5. Output: The largest number (print the value of large) C program to search the largest element of a list #include<stdio.h> #include<conio.h> int main() { int x[100],i,n,large; printf("Enter the number of elements: "); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x[i]); } large=x[1]; for(i=2;i<=n;i++) { if(large<x[i])
  • 4. { large=x[i]; } } printf("The largest among the %d elements is %d",n,large); return 0; } Sample Output Enter the number of elements: 5 12 6 7 9 0 The largest among the 5 elements is 12 Algorithm to search a particular element from a list 1. Input: A set of data in array a, and variable x. i.e., the target element a[1….n], x; 2. found=0 3. for (i=1; i<=n; i++) { if (a[i]==x) location=i; found= 1; break; } 4. Output: if (found==1) print”FOUND” message and location.
  • 5. else print ”NOT FOUND” message C program to search a particular element from a list #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; } Sample Output Enter the number of elements in array
  • 6. 6 Enter 6 integer(s) 3 4 0 2 9 7 Enter the number to search 0 0 is present at location 3. Algorithm to find out the summations of even and odd numbers 1. Input: An array and variable (to store the results of summations) A[1….n], sum_odd=0, sum_even=0; 2. for (i=1; i<=n; i++) { if(A[i]%2==0), sum_even=sum_even+A[i]; else sum_odd=sum_odd+A[i]; } 3. Output: Summationof odd numbers (print sum_odd) and Summation of even numbers (print sum_even) C program to find out the summations of even and odd numbers #include <stdio.h> int main() { int array[100], i, num, sum_of_odd = 0, sum_of_even = 0; printf("Enter the number of elementsn");
  • 7. scanf("%d", &num); printf("Enter the elements n"); for(i=1;i<=num;i++) { scanf("%d",&array[i]); } for (i = 1; i <= num; i++) { if (array[i] % 2 == 0) sum_of_even = sum_of_even + array[i]; else sum_of_odd = sum_of_odd + array[i]; } printf("Sum of all odd numbers = %dn", sum_of_odd); printf("Sum of all even numbers = %dn", sum_of_even); return 0; } Sample Output Enter the number of elements 5 Enter the elements 1 2 5 3 9 Sum of all odd numbers = 18 Sum of all even numbers = 2
  • 8. Algorithm to find out the summations of even and odd indexed numbers 1. Input: An array and variable (to store the results of summations) A[1….n], sum_odd=0, sum_even=0; 2. for (i=1; i<=n; i++) { if(i%2==0), sum_even=sum_even+A[i]; else sum_odd=sum_odd+A[i]; } 3. Output: Summation of numbers in odd indices (print sum_odd) and Summation of numbers in even indices (print sum_even) C program to find out the summations of even and odd indexed numbers #include <stdio.h> int main() { int array[100], i, num, sum_of_odd = 0, sum_of_even = 0; printf("Enter the number of elementsn"); scanf("%d", &num); printf("Enter the elements n"); for(i=1;i<=num;i++) { scanf("%d",&array[i]); } for (i = 1; i <= num; i++) { if (i % 2 == 0) sum_of_even = sum_of_even + array[i]; else sum_of_odd = sum_of_odd + array[i];
  • 9. } printf("Sum of all odd number indices = %dn", sum_of_odd); printf("Sum of all even number indices = %dn", sum_of_even); return 0; } Sample Output Enter the number of elements 5 Enter the elements 1 2 5 8 7 Sum of all odd number indices = 13 Sum of all even number indices = 10 Algorithm to insert an element into an array 1. Input: An array A[1….n], the position of insertion m and the data x. 2. Increase the size of the array, A[1….n+1] 3. for (i=n; i>=m; i--) A[i+1]=A[i]; 4. A[m]=x; 5. Output: The array, A with size n+1 C program to insert an element into an array #include <stdio.h> int main() { int array[100], position, c, n, value;
  • 10. printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (c = 1; c <= n; c++) { scanf("%d", &array[c]); } printf("Enter the location where you wish to insert an elementn"); scanf("%d", &position); printf("Enter the value to insertn"); scanf("%d", &value); for (c = n; c >= position; c--) { array[c+1] = array[c]; } array[position] = value; printf("Resultant array isn"); for (c = 1; c <= n+1; c++) { printf("%dn", array[c]); } return 0; } Sample Output Enter number of elements in array 5 Enter 5 elements
  • 11. 1 2 3 7 5 Enter the location where you wish to insert an element 4 Enter the value to insert 0 Resultant array is 1 2 3 0 7 5 Algorithm to delete an element from an array 1. Input: An array A[1….n], the position of deletion m. 2. for (i=m; i<n; i++) A[i]=A[i+1]; 3. Output: The updated array, A C program to delete an element from an array #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n);
  • 12. printf("Enter %d elementsn", n); for ( c = 1 ; c <= n ; c++ ) { scanf("%d", &array[c]); } printf("Enter the location where you wish to delete elementn"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.n"); else { for ( c = position ; c < n ; c++ ) { array[c] = array[c+1]; } printf("Resultant array isn"); for( c = 1 ; c < n ; c++ ) printf("%dn", array[c]); } return 0; } Sample Output Enter number of elements in array 4 Enter 4 elements 8 9
  • 13. 0 3 Enter the location where you wish to delete element 2 Resultant array is 8 0 3