SlideShare a Scribd company logo
Arrays
COMPUTER
PROGRAMMING
❖ An array is simply a collection of variables of the
same data type that are referred to by a common name.
❖ A specific element in an array is accessed by an index.
❖ All array consist of contiguous memory locations
where the lowest address corresponds to the first
element whereas the highest address corresponds to
the last element.
Arrays
❖ The first element in the array is numbered 0, so the last
element is 1 less than the size of the array.
❖ An array is also known as a subscripted variable.
❖ Before using an array its type and dimension must be
declared.
❖ Index always starts from 0 in an array.
Arrays
Arrays
An array of one dimension is known as a one-
dimensional array or 1-D array
One-dimensional Arrays
Like other variables an array needs to be declared so that the
compiler will know what kind of an array and how large an
array we want.
type arr_name[size];
Here type is any valid data type,
arr_name is the name of the array you give
and size is the array size.
In other word, size specify that how many element,
array can hold.
One-dimensional Arrays
One-dimensional Arrays
e.g.
int marks[30] ;
Here, int specifies the type of the variable,
The number 30 tells how many elements of the type int will
be in our array. This number is often called the "dimension"
of the array.
The bracket i.e. [ ] tells the compiler that we are dealing
with an array.
One-dimensional Arrays
Arrays
One-dimensional Arrays
Initializing Array in C
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
float press[ ] = { 12.3, 34.2, -23.4, -11.3 } ;
int b[ 100 ], x[ 27 ];
One-dimensional Arrays
❖ This is done with subscript, the number in the brackets
following the array name.
❖ This number specifies the element’s position in the array.
❖ All the array elements are numbered, starting with 0.
❖ Thus, arr [2] is not the second element of the array, but the
third.
int arr[ ] = {1, 2, 3, 4, 5};
val = arr[2]; // val=3
One-dimensional Arrays
Accessing Elements of an Array
• Array elements are like normal variables
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
– Perform operations in subscript. If x equals 3
c[5-2]==c[3]==c[x]
One-dimensional Arrays
1-D Array Input/Output
/* Program to take 5 values from the user and store them in an array
& Print the elements stored in the array*/
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
One-dimensional Arrays
1-D Array Input/Output
// printing elements of an array
for(int i = 0; i < 5; ++i)
{
printf("%dn", values[i]);
}
return 0;
}
One-dimensional Arrays
/*Compute the sum of the elements of the array */
#include <stdio.h>
int main( void )
{ /* use initializer list to initialize array */
int a[12] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 };
int i; /* counter */
int total = 0; /* sum of array */
/* sum of contents of array a */
for ( i = 0; i < SIZE; i++ )
{
total += a[ i ];
} /* end for */
printf( "Total of array element values is %dn", total );
return 0;
} Output : Total of array element values is 383
One-dimensional Arrays
Passing a single array element to a function
#include<stdio.h>
void display(int a);
int main()
{
int myArray[] = { 2, 3, 4 };
display(myArray[2]); //Passing array element myArray[2] only.
return 0;
}
void display(int a)
{
printf("%d", a);
}
OUTPUT
4
One-dimensional Arrays
Passing 1-d array to function using call by value method
#include <stdio.h>
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j'};
for (int x=0; x<10; x++)
{
/* I’m passing each element one by one
using subscript*/
disp (arr[x]);
}
return 0;
}
void disp( char ch)
{
printf("%c ", ch);
}
OUTPUT:
a b c d e f g h i j
One-dimensional Arrays
Passing a 2D array as a parameter
#include<stdio.h>
void displayArray(int arr[3][3]);
int main()
{
int arr[3][3], i, j;
printf("Please enter 9 numbers for
the array: n");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
scanf("%d", &arr[i][j]);
}
}
// passing the array as argument
displayArray(arr);
return 0;
}
void displayArray(int arr[3][3])
{
int i, j;
printf("The complete array is: n");
for (i = 0; i < 3; ++i)
{
// getting cursor to new line
printf("n");
for (j = 0; j < 3; ++j)
{
printf("%dt", arr[i][j]);
}
}
}
One-dimensional Arrays
Practice question:
Find maximum and minimum element in
an array
Find maximum and minimum element in an array
#include <stdio.h>
int main()
{
int arr1[100];
int i, max, min, n;
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
One-dimensional Arrays
Find maximum and minimum element in an array
max = arr1[0];
min = arr1[0];
//finding max
for(i=1; i<n; i++)
{
if(arr1[i]>max)
{
max = arr1[i];
}
//finding min
if(arr1[i]<min)
{
min = arr1[i];
}
}
}
One-dimensional Arrays
Practice question:
Insert an element in an Array
Practice question:
Insert an element in an Array
// position at which element
// is to be inserted
pos = 5;
// increase the size by 1
n++;
// shift elements forward
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
return 0;
}
// C Program to Insert an element at a
//specific position in an Array
#include <stdio.h>
int main()
{
int arr[100];
int i, x, pos, n = 10;
// initial array of size 10
for (i = 0; i < 10; i++)
scanf(“%d”, &arr[i]);
// print the original array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
// element to be inserted
x = 50;
Multidimensional
Arrays
COMPUTER
PROGRAMMING
Multiple-Subscripted Arrays
➢ Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
➢Declaration of two-dimensional array in C:
int arr[10][5];
• So the above array have 10 rows and 5 columns.
Multidimensional Arrays
Double-subscripted array (2-D) with three rows and four
column.
Two- Dimensional Arrays
➢Initialization
int arr[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
– Initializers grouped by row in braces
Two- Dimensional Arrays
➢Initialization
– If not enough, unspecified elements set to
zero
int arr[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
➢Referencing elements
– Specify row, then column
printf( "%d", arr[ 0 ][ 1 ] );
Two- Dimensional Arrays
/* Example- initializing and displaying elements*/
#include<stdio.h>
void main()
{
int arr[10][5];
int i, j;
// e.g. initializing 2-D array elements by 1
for(i=0; i<10; i++)
{
for(j=0; j<5; j++)
{
arr[i][j] = 1;
}
}
Cont….
Two- Dimensional Arrays
// displaying 2-D array
for(i=0; i<10; i++)
{
for(j=0; j<5; j++)
{
printf("arr[%d][%d]=%dt", i, j, arr[i][j]);
}
printf("n");
}
}
Cont….
Two- Dimensional Arrays
Entering and displaying Matrix elements
#include<stdio.h>
void main()
{
int arr[5][3];
int i, j;
printf("Enter 5*3 Matrix: ");
for(i=0; i<5; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &arr[i][j]);
}
}
Cont….
Matrix
//Displaying the Matrix
printf("nThe Matrix is:n");
for(i=0; i<5; i++)
{
for(j=0; j<3; j++)
{
printf("%dt", arr[i][j]);
}
printf("n");
} }
Cont….
Addition of two Matrices
Matrix
Transpose of a Matrix
Matrix
Linear Search
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!"
and terminate the function
Step 4 - If both are not matched, then compare search element with
the next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with
last element in the list.
Step 6 - If last element in the list also doesn't match, then display
"Element is not found!!!" and terminate the function.
Linear Search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d integer(s)n", n);
//Entering array elements
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
//Enter number to be searched
printf("Enter a number to searchn");
scanf("%d", &search);
Linear Search
//Linear search
for (c = 0; c < n; c++)
{
if (array[c] == search)
// If required number is found
{ printf("%d is present at location %d.n", search, c+1);
break;
}
}
// for not found
if (c == n)
printf("%d isn't present in the array.n", search);
return 0;
}
Linear Search
Practice questions:
• Deletion of an array element
• Merging two arrays
Ad

More Related Content

Similar to SlideSet_4_Arraysnew.pdf (20)

6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
Arrays
ArraysArrays
Arrays
Dr. Sindhia Lingaswamy
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
guest91d2b3
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
SamQuiDaiBo
 
Arrays
ArraysArrays
Arrays
Aman Agarwal
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
 
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
 
Array
ArrayArray
Array
Kathmandu University
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PPS 4.4ARRAYS  ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...PPS 4.4ARRAYS  ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Sitamarhi Institute of Technology
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
Bhuvana Gowtham
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
sayalishivarkar1
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
Deepak Singh
 

Recently uploaded (20)

7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
abdokhattab2015
 
Hostelmanagementsystemprojectreport..pdf
Hostelmanagementsystemprojectreport..pdfHostelmanagementsystemprojectreport..pdf
Hostelmanagementsystemprojectreport..pdf
RajChouhan43
 
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Journal of Soft Computing in Civil Engineering
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
May 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its ApplicationsMay 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
ssuserd9338b
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
introduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applicationsintroduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applications
vijimech408
 
22PCOAM16 Unit 3 Session 23 Different ways to Combine Classifiers.pptx
22PCOAM16 Unit 3 Session 23  Different ways to Combine Classifiers.pptx22PCOAM16 Unit 3 Session 23  Different ways to Combine Classifiers.pptx
22PCOAM16 Unit 3 Session 23 Different ways to Combine Classifiers.pptx
Guru Nanak Technical Institutions
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Espresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptxEspresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptx
NingChacha1
 
7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
7- Bearing..pptx 7- Bearing..pptx7- Bearing..pptx
abdokhattab2015
 
Hostelmanagementsystemprojectreport..pdf
Hostelmanagementsystemprojectreport..pdfHostelmanagementsystemprojectreport..pdf
Hostelmanagementsystemprojectreport..pdf
RajChouhan43
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
May 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its ApplicationsMay 2025 - Top 10 Read Articles in Network Security and Its Applications
May 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
ssuserd9338b
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
introduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applicationsintroduction to Rapid Tooling and Additive Manufacturing Applications
introduction to Rapid Tooling and Additive Manufacturing Applications
vijimech408
 
22PCOAM16 Unit 3 Session 23 Different ways to Combine Classifiers.pptx
22PCOAM16 Unit 3 Session 23  Different ways to Combine Classifiers.pptx22PCOAM16 Unit 3 Session 23  Different ways to Combine Classifiers.pptx
22PCOAM16 Unit 3 Session 23 Different ways to Combine Classifiers.pptx
Guru Nanak Technical Institutions
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Espresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptxEspresso PD Official MP_eng Version.pptx
Espresso PD Official MP_eng Version.pptx
NingChacha1
 
Ad

SlideSet_4_Arraysnew.pdf

  • 2. ❖ An array is simply a collection of variables of the same data type that are referred to by a common name. ❖ A specific element in an array is accessed by an index. ❖ All array consist of contiguous memory locations where the lowest address corresponds to the first element whereas the highest address corresponds to the last element. Arrays
  • 3. ❖ The first element in the array is numbered 0, so the last element is 1 less than the size of the array. ❖ An array is also known as a subscripted variable. ❖ Before using an array its type and dimension must be declared. ❖ Index always starts from 0 in an array. Arrays
  • 5. An array of one dimension is known as a one- dimensional array or 1-D array One-dimensional Arrays
  • 6. Like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. type arr_name[size]; Here type is any valid data type, arr_name is the name of the array you give and size is the array size. In other word, size specify that how many element, array can hold. One-dimensional Arrays
  • 8. e.g. int marks[30] ; Here, int specifies the type of the variable, The number 30 tells how many elements of the type int will be in our array. This number is often called the "dimension" of the array. The bracket i.e. [ ] tells the compiler that we are dealing with an array. One-dimensional Arrays
  • 11. Initializing Array in C int num[6] = { 2, 4, 12, 5, 45, 5 } ; int n[ ] = { 2, 4, 12, 5, 45, 5 } ; float press[ ] = { 12.3, 34.2, -23.4, -11.3 } ; int b[ 100 ], x[ 27 ]; One-dimensional Arrays
  • 12. ❖ This is done with subscript, the number in the brackets following the array name. ❖ This number specifies the element’s position in the array. ❖ All the array elements are numbered, starting with 0. ❖ Thus, arr [2] is not the second element of the array, but the third. int arr[ ] = {1, 2, 3, 4, 5}; val = arr[2]; // val=3 One-dimensional Arrays Accessing Elements of an Array
  • 13. • Array elements are like normal variables c[ 0 ] = 3; printf( "%d", c[ 0 ] ); – Perform operations in subscript. If x equals 3 c[5-2]==c[3]==c[x] One-dimensional Arrays
  • 14. 1-D Array Input/Output /* Program to take 5 values from the user and store them in an array & Print the elements stored in the array*/ #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } One-dimensional Arrays
  • 15. 1-D Array Input/Output // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%dn", values[i]); } return 0; } One-dimensional Arrays
  • 16. /*Compute the sum of the elements of the array */ #include <stdio.h> int main( void ) { /* use initializer list to initialize array */ int a[12] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 }; int i; /* counter */ int total = 0; /* sum of array */ /* sum of contents of array a */ for ( i = 0; i < SIZE; i++ ) { total += a[ i ]; } /* end for */ printf( "Total of array element values is %dn", total ); return 0; } Output : Total of array element values is 383 One-dimensional Arrays
  • 17. Passing a single array element to a function #include<stdio.h> void display(int a); int main() { int myArray[] = { 2, 3, 4 }; display(myArray[2]); //Passing array element myArray[2] only. return 0; } void display(int a) { printf("%d", a); } OUTPUT 4 One-dimensional Arrays
  • 18. Passing 1-d array to function using call by value method #include <stdio.h> int main() { char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; for (int x=0; x<10; x++) { /* I’m passing each element one by one using subscript*/ disp (arr[x]); } return 0; } void disp( char ch) { printf("%c ", ch); } OUTPUT: a b c d e f g h i j One-dimensional Arrays
  • 19. Passing a 2D array as a parameter #include<stdio.h> void displayArray(int arr[3][3]); int main() { int arr[3][3], i, j; printf("Please enter 9 numbers for the array: n"); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { scanf("%d", &arr[i][j]); } } // passing the array as argument displayArray(arr); return 0; } void displayArray(int arr[3][3]) { int i, j; printf("The complete array is: n"); for (i = 0; i < 3; ++i) { // getting cursor to new line printf("n"); for (j = 0; j < 3; ++j) { printf("%dt", arr[i][j]); } } } One-dimensional Arrays
  • 20. Practice question: Find maximum and minimum element in an array
  • 21. Find maximum and minimum element in an array #include <stdio.h> int main() { int arr1[100]; int i, max, min, n; printf("Input the number of elements to be stored in the array :"); scanf("%d",&n); printf("Input %d elements in the array :n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } One-dimensional Arrays
  • 22. Find maximum and minimum element in an array max = arr1[0]; min = arr1[0]; //finding max for(i=1; i<n; i++) { if(arr1[i]>max) { max = arr1[i]; } //finding min if(arr1[i]<min) { min = arr1[i]; } } } One-dimensional Arrays
  • 23. Practice question: Insert an element in an Array
  • 24. Practice question: Insert an element in an Array
  • 25. // position at which element // is to be inserted pos = 5; // increase the size by 1 n++; // shift elements forward for (i = n; i >= pos; i--) arr[i] = arr[i - 1]; // insert x at pos arr[pos - 1] = x; // print the updated array for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n"); return 0; } // C Program to Insert an element at a //specific position in an Array #include <stdio.h> int main() { int arr[100]; int i, x, pos, n = 10; // initial array of size 10 for (i = 0; i < 10; i++) scanf(“%d”, &arr[i]); // print the original array for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n"); // element to be inserted x = 50;
  • 27. Multiple-Subscripted Arrays ➢ Multiple subscripted arrays – Tables with rows and columns (m by n array) – Like matrices: specify row, then column ➢Declaration of two-dimensional array in C: int arr[10][5]; • So the above array have 10 rows and 5 columns. Multidimensional Arrays
  • 28. Double-subscripted array (2-D) with three rows and four column. Two- Dimensional Arrays
  • 29. ➢Initialization int arr[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; – Initializers grouped by row in braces Two- Dimensional Arrays
  • 30. ➢Initialization – If not enough, unspecified elements set to zero int arr[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; ➢Referencing elements – Specify row, then column printf( "%d", arr[ 0 ][ 1 ] ); Two- Dimensional Arrays
  • 31. /* Example- initializing and displaying elements*/ #include<stdio.h> void main() { int arr[10][5]; int i, j; // e.g. initializing 2-D array elements by 1 for(i=0; i<10; i++) { for(j=0; j<5; j++) { arr[i][j] = 1; } } Cont…. Two- Dimensional Arrays
  • 32. // displaying 2-D array for(i=0; i<10; i++) { for(j=0; j<5; j++) { printf("arr[%d][%d]=%dt", i, j, arr[i][j]); } printf("n"); } } Cont…. Two- Dimensional Arrays
  • 33. Entering and displaying Matrix elements #include<stdio.h> void main() { int arr[5][3]; int i, j; printf("Enter 5*3 Matrix: "); for(i=0; i<5; i++) { for(j=0; j<3; j++) { scanf("%d", &arr[i][j]); } } Cont…. Matrix
  • 34. //Displaying the Matrix printf("nThe Matrix is:n"); for(i=0; i<5; i++) { for(j=0; j<3; j++) { printf("%dt", arr[i][j]); } printf("n"); } } Cont….
  • 35. Addition of two Matrices Matrix
  • 36. Transpose of a Matrix Matrix
  • 38. Step 1 - Read the search element from the user. Step 2 - Compare the search element with the first element in the list. Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function Step 4 - If both are not matched, then compare search element with the next element in the list. Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list. Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and terminate the function. Linear Search
  • 39. #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d integer(s)n", n); //Entering array elements for (c = 0; c < n; c++) scanf("%d", &array[c]); //Enter number to be searched printf("Enter a number to searchn"); scanf("%d", &search); Linear Search
  • 40. //Linear search for (c = 0; c < n; c++) { if (array[c] == search) // If required number is found { printf("%d is present at location %d.n", search, c+1); break; } } // for not found if (c == n) printf("%d isn't present in the array.n", search); return 0; } Linear Search
  • 41. Practice questions: • Deletion of an array element • Merging two arrays
  翻译: