SlideShare a Scribd company logo
C L A S S R O L L : 1 7 B S C S 7 6
U N I V E R S I T Y : Q A I D - E - A W A M
E N G I N E E R I N G
U N I V E R S I T Y , N A W A B S H A H , S I N D H , P A K I S T A N
D A T E : 2 4 / 8 / 2 0 1 7
NAVEED JAMALI
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.
WAP to read 10 numbers from the user and
display them.
 #include<stdio.h>
 #include<conio.h>
 int main ()
 {
 int a[10];
 int i;
 printf("please enter ten numbersn");
 for(i=0;i<10;i++)
 {
 scanf("%d",&a[i]);
 }
 for(i=0;i<10;i++)
 {
 printf("%dt",a[i]);
 }
 getch();
 }
WAP to read 20 numbers from the user and find
out the highest number.
 #include <stdio.h>
 int main()
 {
 int array[20], size, i, largest;
 printf("n Enter the size of the array: ");
 scanf("%d", &size);
 printf("n Enter %d elements of the array:
", size);
 for (i = 0; i < size; i++)
 scanf("%d", &array[i]);
 largest = array[0];
 for (i = 1; i < size; i++)
 {
 if (largest < array[i])
 largest = array[i];
 }
 printf("n Highest element present in the
given array is : %d",largest);
 getch();
 }
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);
printf("n");
for(int i =
0; i < 2; i++)
{
for(int j =
0; j < 2; j++)
{
printf("
%d",matrix[i][j]);
}
printf("n");
}
getch();
output:
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:
Ex.1 #include <stdio.h>
 int main()
 {
 int a[2][2], b[2][2],c[2][2];
 int i, j;
 printf("Enter elements of 1st
matrixn");
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 scanf("%d", &a[i][j]);
 }
 printf("Enter elements of 2nd
matrixn");
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 scanf("%d", &b[i][j]);
 }
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
printf("nSum Of Matrix:n");
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
printf("%dt", c[i][j]);
if(j==1) ;
}
printf("n");
}
return 0;
}
OUTPUT: Ex.1
2 D. Arrays: Write a C program using arrays
that produces the multiplication of two
matrices.#include <stdio.h>
#include<conio.h>
int main()
{
int a[2][2], b[2][2],c[2][2];
int i, j;
printf("Enter elements of 1st matrixn");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrixn");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
scanf("%d", &b[i][j]);
}
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] * b[i][j];
}
printf("n Multiplication Of
Matrix:n");
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
printf("%dt", c[i][j]);
if(j==1) ;
}
printf("n");
}
return 0;
}
Ad

More Related Content

What's hot (20)

Array in c
Array in cArray in c
Array in c
AnIsh Kumar
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 
Chap09
Chap09Chap09
Chap09
Terry Yoast
 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Array in C
Array in CArray in C
Array in C
adityas29
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Arrays
ArraysArrays
Arrays
Chirag vasava
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Arrays in c
Arrays in cArrays in c
Arrays in c
AnIsh Kumar
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
Surbhi Yadav
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
Muthukumaran Subramanian
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
Surbhi Yadav
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 

Similar to Array,MULTI ARRAY, IN C (20)

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
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
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
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
C Arrays.ppt
C Arrays.pptC Arrays.ppt
C Arrays.ppt
ssuser0c1819
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
adityavarte
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
Hemantha Kulathilake
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
Arrays
ArraysArrays
Arrays
Saranya saran
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Ch08
Ch08Ch08
Ch08
Arriz San Juan
 
array
arrayarray
array
teach4uin
 
05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Array and its operation in C programming
Array and its operation in C programmingArray and its operation in C programming
Array and its operation in C programming
Rhishav Poudyal
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Chapter 6 arrays part-1
Chapter 6   arrays part-1Chapter 6   arrays part-1
Chapter 6 arrays part-1
Synapseindiappsdevelopment
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Anurag University Hyderabad
 
Ad

Recently uploaded (20)

Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Ad

Array,MULTI ARRAY, IN C

  • 1. C L A S S R O L L : 1 7 B S C S 7 6 U N I V E R S I T Y : Q A I D - E - A W A M E N G I N E E R I N G U N I V E R S I T Y , N A W A B S H A H , S I N D H , P A K I S T A N D A T E : 2 4 / 8 / 2 0 1 7 NAVEED JAMALI
  • 3. 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]
  • 4. 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];
  • 5. 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.
  • 6. 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]
  • 7. Reading Data from User  for loop is used to read data from the user.
  • 8. 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(); }
  • 9. 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 }
  • 10. 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
  • 11. 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(); }
  • 12. 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.
  • 13. WAP to read 10 numbers from the user and display them.  #include<stdio.h>  #include<conio.h>  int main ()  {  int a[10];  int i;  printf("please enter ten numbersn");  for(i=0;i<10;i++)  {  scanf("%d",&a[i]);  }  for(i=0;i<10;i++)  {  printf("%dt",a[i]);  }  getch();  }
  • 14. WAP to read 20 numbers from the user and find out the highest number.  #include <stdio.h>  int main()  {  int array[20], size, i, largest;  printf("n Enter the size of the array: ");  scanf("%d", &size);  printf("n Enter %d elements of the array: ", size);  for (i = 0; i < size; i++)  scanf("%d", &array[i]);  largest = array[0];  for (i = 1; i < size; i++)  {  if (largest < array[i])  largest = array[i];  }  printf("n Highest element present in the given array is : %d",largest);  getch();  }
  • 15. 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.
  • 16. 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]
  • 17. 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
  • 19. 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
  • 21. 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]);
  • 22. 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:
  • 23. 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
  • 24. 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 };
  • 25. 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(); }
  • 26. 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:
  • 27. 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); printf("n"); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { printf(" %d",matrix[i][j]); } printf("n"); } getch();
  • 29. 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:
  • 30. Ex.1 #include <stdio.h>  int main()  {  int a[2][2], b[2][2],c[2][2];  int i, j;  printf("Enter elements of 1st matrixn");  for(i=0; i<2; ++i)  for(j=0; j<2; ++j)  {  scanf("%d", &a[i][j]);  }  printf("Enter elements of 2nd matrixn");  for(i=0; i<2; ++i)  for(j=0; j<2; ++j)  {  scanf("%d", &b[i][j]);  } for(i=0; i<2; ++i) for(j=0; j<2; ++j) { c[i][j] = a[i][j] + b[i][j]; } printf("nSum Of Matrix:n"); for(i=0; i<2; ++i) { for(j=0; j<2; ++j) { printf("%dt", c[i][j]); if(j==1) ; } printf("n"); } return 0; }
  • 32. 2 D. Arrays: Write a C program using arrays that produces the multiplication of two matrices.#include <stdio.h> #include<conio.h> int main() { int a[2][2], b[2][2],c[2][2]; int i, j; printf("Enter elements of 1st matrixn"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { scanf("%d", &a[i][j]); } printf("Enter elements of 2nd matrixn"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) { scanf("%d", &b[i][j]); } for(i=0; i<2; ++i) for(j=0; j<2; ++j) { c[i][j] = a[i][j] * b[i][j]; } printf("n Multiplication Of Matrix:n"); for(i=0; i<2; ++i) { for(j=0; j<2; ++j) { printf("%dt", c[i][j]); if(j==1) ; } printf("n"); } return 0; }