SlideShare a Scribd company logo
INTRODUCTION
ONE-DIMENSIONAL ARRAY
MULTIDIMENSIONAL ARRAY
Array
Introduction
 An array is a sequence of homogenous elements
 It holds multiple values of same type.
 Each block of array is stored consecutively in
memory.
SYNTAX:
data-type name[size];
Example:
int a[6];
Arrays always start with 0 and end with [size-1]
One dimensional Array
An array is a data structure consisting of a collection of elements (values
or variables), each identified by at least one array index
SYNTAX:
data-type name[index];
EXAMPLE:
int num[10];
Initialization
 int num[6]={2,4,6,7,8,12};
 Individual elements can also be initialize as:
 num[0]=2;
 num[1]=4;
 num[2]=6;
 num[3]=7;
 num[4]=8;
 num[5]=12;
A specific element in an array is accessed by an index.
Arrays: Example
#include<stdio.h>
#include<conio.h>
int main()
{
int age[3];
age[0] = 25;
age[1] = 30;
age[2] = 35;
for (int j=0; j<3; j++)
printf("%dn",age[j]);
getch(); }
25
30
35
age[1]
age[0]
age[2]
Reading Data from User
 for loop is used to read data from the user.
Arrays: Example
#include<stdio.h>
#include<conio.h>
int main()
{
int age[3];
age[0] = 25;
age[1] = 30;
age[2] = 35;
printf("Ages are ");
for (int j=0; j<3; j++)
printf("%dn",age[j]);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int age[3];
for (int i = 0; i<3; i++) {
printf("Enter ages n");
scanf("%d",&age[i]);}
printf("Ages are ");
for (int j=0; j<3; j++)
printf("%d n",age[j]);
getch();
}
Initializing Arrays in Declarations
• Possible to declare the size & initialize
• Possible to omit size at declaration
– Compiler figures out size of array
int results [5] = {14, 6, 23, 8, 12 }
float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }
Arrays Initialization: Example
#include<stdio.h>
#include<conio.h>
int main()
{
int age[3] = {25, 30,
35};
for (int j=0; j<3; j++)
printf("%dn",age[j]);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int age[ ] = {25, 30,
35};
for (int j=0; j<3; j++)
printf("%dn",age[j]);
getch();
}
Empty brackets
can take any
size
Arrays: Class
Exercise
Write a C program
using arrays that
accepts five (05)
integers and then
prints them in
reverse order.
#include<stdio.h>
#include<conio.h>
int main()
{
int order[5];
printf("Enter numbers n");
for(int i=0; i<=4; i++)
scanf("%d ", &order[i]);
for (int j=4; j>=0; j--)
printf("%dn", order[j]);
getch();
}
Class work
 WAP to read 10 numbers from the user and display
them.
 WAP to read 20 numbers from the user and find out
the highest number.
Advantage of Array
 Huge amount of data can be stored under single
variable name.
 Searching of data item is faster.
 2 dimension arrays are used to represent the
matrices.
 It is helpful in implementing other data structure
like linked list, queue,stack.
2-Dimensional Arrays
• A collection of a fixed number of components
arranged in two dimensions
– All components are of the same type
• The syntax for declaring a two-dimensional
array is:
dataType arrayName[intexp1][intexp2];
where intexp1 and intexp2 are expressions
yielding positive integer values; e.g., double
sales[10][5]
2-Dimensional Arrays
• The two expressions intexp1 and intexp2 specify
the number of rows and the number of columns,
respectively, in the array
• Two-dimensional arrays are sometimes called
matrices or tables
2-Dimensional Arrays
double sales[10][5];
2-Dimensional Arrays
 The syntax to access a component of a two-
dimensional array is:
arrayName[indexexp1][indexexp2]
where indexexp1 and indexexp2 are expressions
yielding nonnegative integer values
 indexexp1 specifies the row position and
indexexp2 specifies the column position
2-Dimensional Arrays
sales[2][3] = 35.60;
35.60
2-Dimensional Arrays Accessing
 Accessing all of the elements of a two-dimensional array
requires two loops: one for the row, and one for the column.
 Since two-dimensional arrays are typically accessed row by
row, generally the row index is used as the outer loop.
for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
printf(“%d”,anArray[nRow][nCol]);
2 DIM. Arrays: Example
#include<stdio.h>
#include<conio.h>
int main()
{
double sales[2][3];
sales[0][0] = 2.3;
sales[0][1] = 3.5;
sales[0][2] = 4.2;
sales[1][0] = 5.6;
sales[1][1] = 6.7;
sales[1][2] = 7.8;
//complete program
by //printing the
values which look
like this:
2-Dimensional Arrays Initialization
 Like one-dimensional arrays
 Two-dimensional arrays can be initialized when
they are declared
 To initialize a two-dimensional array when it is
declared
1) Elements of each row are enclosed within braces and
separated by commas
2) All rows are enclosed within braces
3) For number arrays, if all components of a row are not
specified, the unspecified components are initialized to
zero
2-Dimensional Arrays Initialization
 Example:
int anArray[3][5] =
{
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};
2 DIM. Arrays: Example
#include<stdio.h>
#include<conio.h>
int main()
{
int matrix[2][2] = {
{2,3,}, //row0
{5,7} //row1
};
printf("n Resultant n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
printf(" %d", matrix[i][j]);
}
printf("n"); }
getch();
}
2 DIM. Arrays: Class Exercise
Write a C program using 2 DIM. arrays that gets 2x2
matrix input from the user and then prints the
resultant matrix. The output should look like this:
2 DIM. Arrays: Exercise Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int matrix[2][2];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++){
printf("Enter values for [%d %d] ",i,j);
scanf("%d",& matrix[i][j]);
printf("n");}}
printf("resultant:n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
printf(" %d " ,matrix[i][j]);
}
printf("n");
}
getch();
}
2 DIM. Arrays: Class Exercise
Write a C program
using 2 DIM. arrays
that gets two 2x2
matrices as an input
from the user and
then prints the sum
of entered matrices.
The output should
look like this:
 #include<conio.h>
 #include<stdio.h>
 int main()
 {
 int a[2][2],b[2][2],i,j;
 printf("n1st MATRIX:nn");
 for(i=0;i<2;i++){
 for(j=0;j<2;j++)
 {
 scanf("%d",&a[i][j]);
 }}
 printf("n2nd MATIX:nn");
 for(i=0;i<2;i++)
 for(j=0;j<2;j++)

 scanf("%d",&b[i][j]);
 printf("nresultant:n");
 for(i=0;i<2;i++){
 for(j=0;j<2;j++)
 {
 printf("%d ",a[i][j]+b[i][j] );

 } printf("n");

 }
 getch();
 return 0;
 }
2 DIM. Arrays: Assignment
1) Write a C program using arrays that
produces the multiplication of two
matrices.

More Related Content

What's hot

Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Sangani Ankur
 
Array
ArrayArray
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 

What's hot (20)

Data types in python
Data types in pythonData types in python
Data types in python
 
Linked list
Linked listLinked list
Linked list
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Strings
StringsStrings
Strings
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Array
ArrayArray
Array
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Structure in C
Structure in CStructure in C
Structure in C
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Strings in C
Strings in CStrings in C
Strings in C
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 

Similar to Array Introduction One-dimensional array Multidimensional array

2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
Arrays
ArraysArrays
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
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
Arrays
ArraysArrays
About Array
About ArrayAbout Array
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
Array
ArrayArray
Array
PRN USM
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
BHARGAVIARRAY.PPT.pptx
BHARGAVIARRAY.PPT.pptxBHARGAVIARRAY.PPT.pptx
BHARGAVIARRAY.PPT.pptx
Sasideepa
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Array
ArrayArray
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Arrays
ArraysArrays
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
DEEPAK948083
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 

Similar to Array Introduction One-dimensional array Multidimensional array (20)

2D Array
2D Array 2D Array
2D Array
 
Arrays
ArraysArrays
Arrays
 
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
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Arrays
ArraysArrays
Arrays
 
About Array
About ArrayAbout Array
About Array
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Array
ArrayArray
Array
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
BHARGAVIARRAY.PPT.pptx
BHARGAVIARRAY.PPT.pptxBHARGAVIARRAY.PPT.pptx
BHARGAVIARRAY.PPT.pptx
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Array
ArrayArray
Array
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays
ArraysArrays
Arrays
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 

More from imtiazalijoono

Embedded systems io programming
Embedded systems   io programmingEmbedded systems   io programming
Embedded systems io programming
imtiazalijoono
 
Embedded systems tools & peripherals
Embedded systems   tools & peripheralsEmbedded systems   tools & peripherals
Embedded systems tools & peripherals
imtiazalijoono
 
Importance of reading and its types.
Importance of reading and its types.Importance of reading and its types.
Importance of reading and its types.
imtiazalijoono
 
Negative amplifiers and its types Positive feedback and Negative feedback
Negative amplifiers and its types Positive feedback  and Negative feedbackNegative amplifiers and its types Positive feedback  and Negative feedback
Negative amplifiers and its types Positive feedback and Negative feedback
imtiazalijoono
 
Multistage amplifiers and Name of coupling Name of multistage amplifier
Multistage amplifiers and Name of coupling Name of multistage amplifierMultistage amplifiers and Name of coupling Name of multistage amplifier
Multistage amplifiers and Name of coupling Name of multistage amplifier
imtiazalijoono
 
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
imtiazalijoono
 
Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge
imtiazalijoono
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
Software Development Software development process
Software Development Software development processSoftware Development Software development process
Software Development Software development process
imtiazalijoono
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
imtiazalijoono
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Programming Fundamentals and Programming Languages Concepts Translators
Programming Fundamentals and Programming Languages Concepts TranslatorsProgramming Fundamentals and Programming Languages Concepts Translators
Programming Fundamentals and Programming Languages Concepts Translators
imtiazalijoono
 
Programming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages ConceptsProgramming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages Concepts
imtiazalijoono
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
imtiazalijoono
 
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
imtiazalijoono
 
Arithmetic and Arithmetic assignment operators
Arithmetic and Arithmetic assignment operatorsArithmetic and Arithmetic assignment operators
Arithmetic and Arithmetic assignment operators
imtiazalijoono
 
INTRODUCTION TO COMPUTER PROGRAMMING
INTRODUCTION TO COMPUTER PROGRAMMINGINTRODUCTION TO COMPUTER PROGRAMMING
INTRODUCTION TO COMPUTER PROGRAMMING
imtiazalijoono
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
imtiazalijoono
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
imtiazalijoono
 

More from imtiazalijoono (20)

Embedded systems io programming
Embedded systems   io programmingEmbedded systems   io programming
Embedded systems io programming
 
Embedded systems tools & peripherals
Embedded systems   tools & peripheralsEmbedded systems   tools & peripherals
Embedded systems tools & peripherals
 
Importance of reading and its types.
Importance of reading and its types.Importance of reading and its types.
Importance of reading and its types.
 
Negative amplifiers and its types Positive feedback and Negative feedback
Negative amplifiers and its types Positive feedback  and Negative feedbackNegative amplifiers and its types Positive feedback  and Negative feedback
Negative amplifiers and its types Positive feedback and Negative feedback
 
Multistage amplifiers and Name of coupling Name of multistage amplifier
Multistage amplifiers and Name of coupling Name of multistage amplifierMultistage amplifiers and Name of coupling Name of multistage amplifier
Multistage amplifiers and Name of coupling Name of multistage amplifier
 
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
 
Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Software Development Software development process
Software Development Software development processSoftware Development Software development process
Software Development Software development process
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Programming Fundamentals and Programming Languages Concepts Translators
Programming Fundamentals and Programming Languages Concepts TranslatorsProgramming Fundamentals and Programming Languages Concepts Translators
Programming Fundamentals and Programming Languages Concepts Translators
 
Programming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages ConceptsProgramming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages Concepts
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
 
Arithmetic and Arithmetic assignment operators
Arithmetic and Arithmetic assignment operatorsArithmetic and Arithmetic assignment operators
Arithmetic and Arithmetic assignment operators
 
INTRODUCTION TO COMPUTER PROGRAMMING
INTRODUCTION TO COMPUTER PROGRAMMINGINTRODUCTION TO COMPUTER PROGRAMMING
INTRODUCTION TO COMPUTER PROGRAMMING
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 

Array Introduction One-dimensional array Multidimensional array

  • 2. Introduction  An array is a sequence of homogenous elements  It holds multiple values of same type.  Each block of array is stored consecutively in memory. SYNTAX: data-type name[size]; Example: int a[6]; Arrays always start with 0 and end with [size-1]
  • 3. One dimensional Array An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index SYNTAX: data-type name[index]; EXAMPLE: int num[10];
  • 4. Initialization  int num[6]={2,4,6,7,8,12};  Individual elements can also be initialize as:  num[0]=2;  num[1]=4;  num[2]=6;  num[3]=7;  num[4]=8;  num[5]=12; A specific element in an array is accessed by an index.
  • 5. Arrays: Example #include<stdio.h> #include<conio.h> int main() { int age[3]; age[0] = 25; age[1] = 30; age[2] = 35; for (int j=0; j<3; j++) printf("%dn",age[j]); getch(); } 25 30 35 age[1] age[0] age[2]
  • 6. Reading Data from User  for loop is used to read data from the user.
  • 7. Arrays: Example #include<stdio.h> #include<conio.h> int main() { int age[3]; age[0] = 25; age[1] = 30; age[2] = 35; printf("Ages are "); for (int j=0; j<3; j++) printf("%dn",age[j]); getch(); } #include<stdio.h> #include<conio.h> int main() { int age[3]; for (int i = 0; i<3; i++) { printf("Enter ages n"); scanf("%d",&age[i]);} printf("Ages are "); for (int j=0; j<3; j++) printf("%d n",age[j]); getch(); }
  • 8. Initializing Arrays in Declarations • Possible to declare the size & initialize • Possible to omit size at declaration – Compiler figures out size of array int results [5] = {14, 6, 23, 8, 12 } float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }
  • 9. Arrays Initialization: Example #include<stdio.h> #include<conio.h> int main() { int age[3] = {25, 30, 35}; for (int j=0; j<3; j++) printf("%dn",age[j]); getch(); } #include<stdio.h> #include<conio.h> int main() { int age[ ] = {25, 30, 35}; for (int j=0; j<3; j++) printf("%dn",age[j]); getch(); } Empty brackets can take any size
  • 10. Arrays: Class Exercise Write a C program using arrays that accepts five (05) integers and then prints them in reverse order. #include<stdio.h> #include<conio.h> int main() { int order[5]; printf("Enter numbers n"); for(int i=0; i<=4; i++) scanf("%d ", &order[i]); for (int j=4; j>=0; j--) printf("%dn", order[j]); getch(); }
  • 11. Class work  WAP to read 10 numbers from the user and display them.  WAP to read 20 numbers from the user and find out the highest number.
  • 12. Advantage of Array  Huge amount of data can be stored under single variable name.  Searching of data item is faster.  2 dimension arrays are used to represent the matrices.  It is helpful in implementing other data structure like linked list, queue,stack.
  • 13. 2-Dimensional Arrays • A collection of a fixed number of components arranged in two dimensions – All components are of the same type • The syntax for declaring a two-dimensional array is: dataType arrayName[intexp1][intexp2]; where intexp1 and intexp2 are expressions yielding positive integer values; e.g., double sales[10][5]
  • 14. 2-Dimensional Arrays • The two expressions intexp1 and intexp2 specify the number of rows and the number of columns, respectively, in the array • Two-dimensional arrays are sometimes called matrices or tables
  • 16. 2-Dimensional Arrays  The syntax to access a component of a two- dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values  indexexp1 specifies the row position and indexexp2 specifies the column position
  • 18. 2-Dimensional Arrays Accessing  Accessing all of the elements of a two-dimensional array requires two loops: one for the row, and one for the column.  Since two-dimensional arrays are typically accessed row by row, generally the row index is used as the outer loop. for (int nRow = 0; nRow < nNumRows; nRow++) for (int nCol = 0; nCol < nNumCols; nCol++) printf(“%d”,anArray[nRow][nCol]);
  • 19. 2 DIM. Arrays: Example #include<stdio.h> #include<conio.h> int main() { double sales[2][3]; sales[0][0] = 2.3; sales[0][1] = 3.5; sales[0][2] = 4.2; sales[1][0] = 5.6; sales[1][1] = 6.7; sales[1][2] = 7.8; //complete program by //printing the values which look like this:
  • 20. 2-Dimensional Arrays Initialization  Like one-dimensional arrays  Two-dimensional arrays can be initialized when they are declared  To initialize a two-dimensional array when it is declared 1) Elements of each row are enclosed within braces and separated by commas 2) All rows are enclosed within braces 3) For number arrays, if all components of a row are not specified, the unspecified components are initialized to zero
  • 21. 2-Dimensional Arrays Initialization  Example: int anArray[3][5] = { { 1, 2, 3, 4, 5, }, // row 0 { 6, 7, 8, 9, 10, }, // row 1 { 11, 12, 13, 14, 15 } // row 2 };
  • 22. 2 DIM. Arrays: Example #include<stdio.h> #include<conio.h> int main() { int matrix[2][2] = { {2,3,}, //row0 {5,7} //row1 }; printf("n Resultant n"); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { printf(" %d", matrix[i][j]); } printf("n"); } getch(); }
  • 23. 2 DIM. Arrays: Class Exercise Write a C program using 2 DIM. arrays that gets 2x2 matrix input from the user and then prints the resultant matrix. The output should look like this:
  • 24. 2 DIM. Arrays: Exercise Solution #include<stdio.h> #include<conio.h> int main() { int matrix[2][2]; for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++){ printf("Enter values for [%d %d] ",i,j); scanf("%d",& matrix[i][j]); printf("n");}} printf("resultant:n"); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { printf(" %d " ,matrix[i][j]); } printf("n"); } getch(); }
  • 25. 2 DIM. Arrays: Class Exercise Write a C program using 2 DIM. arrays that gets two 2x2 matrices as an input from the user and then prints the sum of entered matrices. The output should look like this:
  • 26.  #include<conio.h>  #include<stdio.h>  int main()  {  int a[2][2],b[2][2],i,j;  printf("n1st MATRIX:nn");  for(i=0;i<2;i++){  for(j=0;j<2;j++)  {  scanf("%d",&a[i][j]);  }}  printf("n2nd MATIX:nn");  for(i=0;i<2;i++)  for(j=0;j<2;j++)   scanf("%d",&b[i][j]);  printf("nresultant:n");  for(i=0;i<2;i++){  for(j=0;j<2;j++)  {  printf("%d ",a[i][j]+b[i][j] );   } printf("n");   }  getch();  return 0;  }
  • 27. 2 DIM. Arrays: Assignment 1) Write a C program using arrays that produces the multiplication of two matrices.