SlideShare a Scribd company logo
Array
Concept
In C programming, one of the frequently arising problem is to handle similar types of data. For
example: If the user want to store marks of 100 students. This can be done by creating 100 variable
individually but, this process is rather tedious and impracticable. These type of problem can be handled
in C programming using arrays.
An array is a sequence of data item of homogeneous value(same type).
Arrays are of two types:
1.One-dimensional arrays
2.Multidimensional arrays
Declaration of one-dimensional array
data_type array_name[array_size];
For example:
int age[5];
Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age.
All element in an array are of the same type (int, in this case).
Array Elements
Size of array defines the number of elements in an array. Each element of array can be accessed and
used by user according to the need of program. For example:
int age[5];
Note that, the first element is numbered 0 and so on.
Here, the size of array age is 5 times the size of int because there are 5 elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes.
Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an
array.
Accessing Array Elements
In C programming, arrays can be accessed and treated like variables in C.
For example:
scanf("%d",&age[2]);
/* statement to insert value in the third element of array age[]. */
scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of array age[]. */
/* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i].
*/
printf("%d",age[0]);
/* statement to print first element of an array. */
printf("%d",age[i]);
/* statement to print (i+1)th element of an array. */
Example of array in C programming
/* C program to find the sum marks of n students using arrays */
#include <stdio.h>
int main()
{
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Output
Enter number of students: 3
Enter marks of student1: 12
Enter marks of student2: 31
Enter marks of student3: 2
sum=45
Important thing to remember in C arrays
Suppose, you declared the array of 10 students. For example: arr[10]. You can use array members
from arr[0] to arr[9]. But, what if you want to use element arr[10], arr[13] etc. Compiler may not
show error using these elements but, may cause fatal error during program execution.
Multidimensional Array
C programming language allows programmer to create arrays of arrays known as
multidimensional arrays. For example:
float a[2][6];
Here, a is an array of two dimension, which is an example of multidimensional array.
For better understanding of multidimensional arrays, array elements of above example can be
think of as below:
Initialization Of Three-dimensional Array
double cprogram[3][2][4]={
{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
};
Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m
numbers of data.
Similarly, the array of any dimension can be initialized in C programming.
Example of Multidimensional Array In C
Write a C program to find sum of two matrix of order 2*2 where, elements of matrix are
entered by user.
#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrixn");
/* Reading two dimensional Array with the help of two for loop. If there was an array of
'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j];
/* Sum of corresponding elements of two arrays. */
}
printf("Sum Of Matrix:n ");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1ft",c[i][j]);
if(j==1)
printf("n");
}
return 0;
}
Output
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
Arrays and Functions
In C programming, a single array element or an entire array can be passed to a function. Also, both
one-dimensional and multi-dimensional array can be passed to function as argument.
Passing One-dimensional Array In Function
C program to pass a single element of an array to function
#include <stdio.h>
void display(int a)
{
printf("%d",a);
}
int main(){
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
} Output 4
Passing entire one-dimensional array to a function
While passing arrays to the argument, the name of the array is passed as an argument(,i.e, starting
address of memory area is passed as argument).
Write a C program to pass an array containing age of person to a function. This function should find
average age and display the average age in main function.
#include <stdio.h>
float average(float a[]);
int main(){
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[ ])
{
int i;
float avg, sum=0.0;
for(i=0;i<6;++i){
sum+=a[i];
}
avg =(sum/6);
return avg;
}
Output Average age=27.08
Passing Multi-dimensional Arrays to Function
To pass two-dimensional array to a function as an argument, starting address of memory area reserved
is passed as in one dimensional array
Example to pass two-dimensional arrays to function
#include
void Function(int c[2][2]);
int main(){
int c[2][2],i,j;
printf("Enter 4 numbers:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
scanf("%d",&c[i][j]);
}
Function(c); /* passing multi-dimensional array to function */
return 0;
}
void Function(int c[2][2]){
/* Instead to above line, void Function(int c[][2]){ is also valid */
int i,j;
printf("Displaying:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%dn",c[i][j]);
} Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
Applications of Array
1. Stores Elements Of Same Data Types
2. Used For Maintaining Multiple Variable Names Using Single Names
3. Can Be Used For Sorting Elements
a) Bubble
b) Insertion
c) Slection
d) Bucket
4. Array Can Perform Matrix Operation
5. Array Can Be Used In CPU Scheduling
6. Array Can Be Used Recursive Function
Examples
Consider the following array definitions
9.30
In each of the following situations, write the definitions and declarations required to transfer
the indicated. variables and arrays from main to a function called .In each case, assign the
value returned from the function to the floating-point variable x.
(a) Transfer the floating-point variables a and b, and the one-dimensional, 20-element integer
array jstar.
(b) Transfer the integer variable n, the character variable c and the one-dimensional, 50-
element doubleprecision array values.
(c) Transfer the two-dimensional, 12 x 80 character array text.
(d) Transfer the one-dimensional, 40-element character array message, and the two-dimensional,
50 x 100 floating-point array accounts.
Array
Array
Array
Array
Array
Array

More Related Content

What's hot

Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
Hareem Naz
 
Arrays
ArraysArrays
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
Rajendran
 
Data structure array
Data structure  arrayData structure  array
Data structure array
MajidHamidAli
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Array
ArrayArray
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 

What's hot (20)

Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Arrays
ArraysArrays
Arrays
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Array
ArrayArray
Array
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 

Similar to Array

VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
Riki Afriansyah
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
trupti1976
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
sudhirvegad
 
Array
ArrayArray
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
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
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 

Similar to Array (20)

VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Arrays
ArraysArrays
Arrays
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
02 arrays
02 arrays02 arrays
02 arrays
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Array
ArrayArray
Array
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
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
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 

More from Anil Dutt

Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
Anil Dutt
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil Dutt
 
Ponters
PontersPonters
Ponters
Anil Dutt
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 

More from Anil Dutt (6)

Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
Ponters
PontersPonters
Ponters
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 

Recently uploaded

Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 

Recently uploaded (20)

Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 

Array

  • 1. Array Concept In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays. An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1.One-dimensional arrays 2.Multidimensional arrays
  • 2. Declaration of one-dimensional array data_type array_name[array_size]; For example: int age[5]; Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element in an array are of the same type (int, in this case).
  • 3. Array Elements Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example: int age[5]; Note that, the first element is numbered 0 and so on. Here, the size of array age is 5 times the size of int because there are 5 elements. Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.
  • 4. Initialization of one-dimensional array: Arrays can be initialized at declaration time in this source code as: int age[5]={2,4,34,3,4}; It is not necessary to define the size of arrays during initialization. int age[]={2,4,34,3,4}; In this case, the compiler determines the size of array by calculating the number of elements of an array.
  • 5. Accessing Array Elements In C programming, arrays can be accessed and treated like variables in C. For example: scanf("%d",&age[2]); /* statement to insert value in the third element of array age[]. */ scanf("%d",&age[i]); /* Statement to insert value in (i+1)th element of array age[]. */ /* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i]. */ printf("%d",age[0]); /* statement to print first element of an array. */ printf("%d",age[i]); /* statement to print (i+1)th element of an array. */
  • 6. Example of array in C programming /* C program to find the sum marks of n students using arrays */ #include <stdio.h> int main() { int marks[10],i,n,sum=0; printf("Enter number of students: "); scanf("%d",&n); for(i=0;i<n;++i) { printf("Enter marks of student%d: ",i+1); scanf("%d",&marks[i]); sum+=marks[i]; } printf("Sum= %d",sum); return 0; }
  • 7. Output Enter number of students: 3 Enter marks of student1: 12 Enter marks of student2: 31 Enter marks of student3: 2 sum=45 Important thing to remember in C arrays Suppose, you declared the array of 10 students. For example: arr[10]. You can use array members from arr[0] to arr[9]. But, what if you want to use element arr[10], arr[13] etc. Compiler may not show error using these elements but, may cause fatal error during program execution.
  • 8. Multidimensional Array C programming language allows programmer to create arrays of arrays known as multidimensional arrays. For example: float a[2][6]; Here, a is an array of two dimension, which is an example of multidimensional array. For better understanding of multidimensional arrays, array elements of above example can be think of as below:
  • 9. Initialization Of Three-dimensional Array double cprogram[3][2][4]={ {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}}, {{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}}, {{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}} }; Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m numbers of data. Similarly, the array of any dimension can be initialized in C programming.
  • 10. Example of Multidimensional Array In C Write a C program to find sum of two matrix of order 2*2 where, elements of matrix are entered by user. #include <stdio.h> int main(){ float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrixn"); /* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/ for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrixn");
  • 11. for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j) {/* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("Sum Of Matrix:n "); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("%.1ft",c[i][j]); if(j==1) printf("n"); } return 0; } Output Enter the elements of 1st matrix Enter a11: 2; Enter a12: 0.5; Enter a21: -1.1; Enter a22: 2; Enter the elements of 2nd matrix Enter b11: 0.2; Enter b12: 0; Enter b21: 0.23; Enter b22: 23; Sum Of Matrix: 2.2 0.5 -0.9 25.0
  • 12. Arrays and Functions In C programming, a single array element or an entire array can be passed to a function. Also, both one-dimensional and multi-dimensional array can be passed to function as argument. Passing One-dimensional Array In Function C program to pass a single element of an array to function #include <stdio.h> void display(int a) { printf("%d",a); } int main(){ int c[]={2,3,4}; display(c[2]); //Passing array element c[2] only. return 0; } Output 4
  • 13. Passing entire one-dimensional array to a function While passing arrays to the argument, the name of the array is passed as an argument(,i.e, starting address of memory area is passed as argument). Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function. #include <stdio.h> float average(float a[]); int main(){ float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c); /* Only name of array is passed as argument. */ printf("Average age=%.2f",avg); return 0; }
  • 14. float average(float a[ ]) { int i; float avg, sum=0.0; for(i=0;i<6;++i){ sum+=a[i]; } avg =(sum/6); return avg; } Output Average age=27.08
  • 15. Passing Multi-dimensional Arrays to Function To pass two-dimensional array to a function as an argument, starting address of memory area reserved is passed as in one dimensional array Example to pass two-dimensional arrays to function #include void Function(int c[2][2]); int main(){ int c[2][2],i,j; printf("Enter 4 numbers:n"); for(i=0;i<2;++i) for(j=0;j<2;++j){ scanf("%d",&c[i][j]); } Function(c); /* passing multi-dimensional array to function */ return 0; }
  • 16. void Function(int c[2][2]){ /* Instead to above line, void Function(int c[][2]){ is also valid */ int i,j; printf("Displaying:n"); for(i=0;i<2;++i) for(j=0;j<2;++j) printf("%dn",c[i][j]); } Output Enter 4 numbers: 2 3 4 5 Displaying: 2 3 4 5
  • 17. Applications of Array 1. Stores Elements Of Same Data Types 2. Used For Maintaining Multiple Variable Names Using Single Names 3. Can Be Used For Sorting Elements a) Bubble b) Insertion c) Slection d) Bucket 4. Array Can Perform Matrix Operation 5. Array Can Be Used In CPU Scheduling 6. Array Can Be Used Recursive Function
  • 19. Consider the following array definitions
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. 9.30 In each of the following situations, write the definitions and declarations required to transfer the indicated. variables and arrays from main to a function called .In each case, assign the value returned from the function to the floating-point variable x. (a) Transfer the floating-point variables a and b, and the one-dimensional, 20-element integer array jstar.
  • 25. (b) Transfer the integer variable n, the character variable c and the one-dimensional, 50- element doubleprecision array values.
  • 26. (c) Transfer the two-dimensional, 12 x 80 character array text.
  • 27. (d) Transfer the one-dimensional, 40-element character array message, and the two-dimensional, 50 x 100 floating-point array accounts.