SlideShare a Scribd company logo
1 of 32
PGT 106 : C PROGRAMMING 1
Lecture 7 – Arrays (1)
PGT 106 : C PROGRAMMING 2
Outline
1. Introduction
2. Arrays of Data
3. Array Declaration
4. Array Initialization
5. Operations on Array
6. Multidimensional Arrays
7. Index out of bound
PGT 106 : C PROGRAMMING 3
What is an Array?
 An array is a collection/group of a fixed number of
components wherein all of the components are of the
same type referred to a same name
PGT 106 : C PROGRAMMING 4
What is an Array? (Example)
 Example: 5, 10, 15, 20, and 25.
 Previously we would declare five
variables:
int iNum1, iNum2, iNum3, iNum4, iNum5;
 By using array,
int aiNum[5];
PGT 106 : C PROGRAMMING 5
What is an Array? (Example)
5
10
15
20
25
aiNum
aiNum[0]
aiNum[1]
aiNum[2]
aiNum[3]
aiNum[4]
 5 components or
elements
 Elements are referred to
index.
 Element aiNum[2] has
index 2 and value 15.
PGT 106 : C PROGRAMMING 6
Arrays of Data
 Engineering applications usually involve large
chunk of data (of common type)
 Arrays provide easy and efficient concept for
data storage or management
 Arrays are usually processed through loops
(processing is very common)
 Arrays are accessed by indicating an address
or index/subscript
PGT 106 : C PROGRAMMING 7
Arrays in C
 Arrays can take any type (including the
primitive data types)
int, char, string, double, float, etc.
 Like any other instances, arrays must
be declared before use.
PGT 106 : C PROGRAMMING 8
Array Declaration
 Format:
 data_type array_name [int value];
 int aiList[5];
 const int Max_Size = 10;
int aiHours[Max_Size];
 const int SIZE = 100;
double adAmount[SIZE];
 const int Max_List_Size = 6;
char acAlp[Max_List_Size];
 #define N 10
double adB[N];
PGT 106 : C PROGRAMMING 9
Multiple Instances vs. Array
// multiple instance
int iValue1, iValue2, iValue3,
iValue4, iValue 5;
printf (“Enter first value: “);
scanf (“%d”, &iValue1);
printf(“Enter second value: “);
scanf(“%d”, &iValue2);
printf (“Enter third value: “);
scanf(“%d”, &iValue3);
printf (“Enter fourth value: “);
scanf(“%d”, &iValue4)
printf (“Enter fifth value: “);
scanf(“%d”, &iValue5)
UniMAP Sem II -
09/10 PGT 106 : C PROGRAMMING 10
// array
int aiValue[3];
for(int iCount=0; iCount<3; iCount++)
{
printf (“Enter value : ”);
printf (“%d : ”, iCount+1);
scanf (“%d”, &aiValue[iCount]);
}
PGT 106 : C PROGRAMMING 11
Arrays - Memory Allocation
 Arrays are allocated
bulk memory
 Single reference used
for multiple locations
 Items are accessed
based on index
(address) with
reference to first item
int aiValue[8];
aiValue[0]=23;
aiValue[1]=56;
aiValue[2]=100;
aiValue[3]=0;
aiValue[4]=12;
aiValue[5]=234;
aiValue[6]=666;
aiValue[7]=4;
23
56
100
0
12
234
666
4
index aiValue
0
1
2
3
4
5
6
7
PGT 106 : C PROGRAMMING 12
Arrays Arithmetic
 Operations on arrays are similar to that on
basic variables.
 iSum = aiNum[0] + aiNum[1] + aiNum[2]
+ aiNum[3];
 iMult = 3 * aiNum[1];
 iRemainder = aiNum[3] % 3;
 iTotal = aiNum[1] * aiNum[2];
PGT 106 : C PROGRAMMING 13
Array Initialization
 Arrays can be initialized directly, but assignments are
done using loops
 Like any other simple variable, arrays can also be
initialized while they are being declared.
double adSales[5] = {12.25, 32.50, 16.90, 23, 45.68};
adSales[0]=12.25, adSales[1]=32.50,
adSales[2]=16.90, adSales[3]=23.00,
adSales[4]=45.68;
PGT 106 : C PROGRAMMING 14
Array Initialization (cont…)
 Initializers:
 If not enough initializers, rightmost element becomes 0
int aiN[ 7 ] = { 1, 2, 3, 4, 5 }; => aiN[5] = aiN[6] = 0
 All elements = 0
int aiN[ 5 ] = { 0 } ;
▪ If size is omitted, initializers determine the size
int aiN[ ] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
PGT 106 : C PROGRAMMING 15
Sample Program
#include <stdio.h>
int main()
{
int aiA[3]= {11,22}, aiB[]={44, 55, 66},iLoop;
double adX[2],adY[10];
printf(“aiA[0]=%2d, aiA[1]=%2d, aiA[2]=%2d n"
“aiB[0]=%2d, aiB[1]=%2d, aiB[2]=%2d nn",
aiA[0],aiA[1],aiA[2],aiB[0],aiB[1],aiB[2]);
printf("Please enter two real numbersn");
scanf("%lf %lf",&adX[0], &adX[1]);
printf(“adX[0] = %.1lf adX[1] = %.1lfnn", adX[0], adX[1]);
for (iLoop=0;iLoop<10;iLoop++)
{
adY[iLoop]= iLoop*100.0;
printf(“adY[%1d]=%.2lfn", iLoop, adY[i]);
}
return 0;
}
Using a loop to fill all the
elements of the adY[] array.
Initializes the first 2 elements of the
aiA[]array. All the other elements are
then automatically set to zero
Because no array size is given (the
brackets are empty) and three values are
given in braces, the array is
automatically declared to have a size of
3 with the value shown being the initial
element values.
PGT 106 : C PROGRAMMING 16
Sample Program
 Output:
aiA[0]=11, aiA[1]=22, aiA[2]= 0
aiB[0]=44, aiB[1]=55, aiB[2]=66
Please enter two real numbers
77.0 88.0
adX[0] = 77.0 adX[1] = 88.0
adY[0]=0.00
adY[1]=100.00
adY[2]=200.00
adY[3]=300.00
adY[4]=400.00
adY[5]=500.00
adY[6]=600.00
adY[7]=700.00
adY[8]=800.00
adY[9]=900.00
PGT 106 : C PROGRAMMING 17
Array Initialization During
Declaration
 When declaring and initializing arrays, it is not
necessary to specify the size of the array.
 The size of the array is determined by the
number of initial values in the braces.
double adSales[] = {12.25, 32.50, 16.90, 23, 45.68};
PGT 106 : C PROGRAMMING 18
A simple example
 The program declares and initializes the
array aiY. It uses a ‘for’ loop with index iLoop
to access the successive elements of aiY. For
each loop iteration, the value accessed id is
added to the variable iTotal which is finally
displayed. Note that the loop index iLoop
starts from 0 to 4 (not from 1 to 5). Also,
note that the array size n is declared in the
define statement.
PGT 106 : C PROGRAMMING 19
A simple example (cont..)
#include<stdio.h>
#define n 5 // define number of n in the array
void main()
{
int iLoop, iTotal = 0; // variable declaration
int aiY[n]={9,6,20,5,12}; // array declaration and
// initialization
for (iLoop=0;iLoop<n;iLoop++)
iTotal = iTotal + aiY[i];
printf ("nTotal = %dn”, iTotal);
}
PGT 106 : C PROGRAMMING 20
Notes
 The defined constants, #define is used to ease
any future amendments of the codes, for
instance, if the array is to be widen to an n of 10
instead of 5, it would be adequate by modifying
the line:
#define n 5  #define n 10
there is no need to make any other changes to
the program, thus making the life of programmer
easier.
PGT 106 : C PROGRAMMING 21
Operations on Array
 Reading data in an array
for (iIndex = 0; iIndex < 10; iIndex++)
scanf (“%d”, &aiSale[iIndex]);
 Printing an array
for (iIndex = 0; iIndex < 10; iIndex++)
printf (“%d ”, aiSale[iIndex]);
PGT 106 : C PROGRAMMING 22
Parallel Arrays
 Two (or more) arrays are called
parallel if their corresponding
components hold related information.
int aiStudentId[50];
char acStudentGrade[50];
PGT 106 : C PROGRAMMING 23
Multi-Dimensional Arrays
 Arrays can have multiple dimensions
 Most used is the 2-dimensional array
(for matrix implementation)
 Actual implementation is a single array
(segmented)
 Nested loop structure usually used to
access items
PGT 106 : C PROGRAMMING 24
2-Dimensional Array
(Example)
5
index aiValue
0
1
2
3
4
5
6
7
5
Column
0 1
0
1
2
3
Row
Address Resolution = Row*(MaxCol) + Col
Row 0
Row 1
int aiValue[4][2];
aiValue[2][1]=5;
PGT 106 : C PROGRAMMING 25
Multi-Dimensional Arrays
(cont..)
 A collection of the same type of data
stored in contiguous and increasing
memory locations.
 Declaration of multi-dimensional array:
 int aiB[2][3] = {51, 52, 53, 54, 55, 56};
array_type array_name Array dimension = 2
two rows
three columns first row
initial values
second row
initial values
PGT 106 : C PROGRAMMING 26
Multi-Dimensional Arrays
(cont..)
 Multi-dimensional array can be initialized directly in
the declaration statement.
 For example:
 int aiB[2][3] = {51, 52, 53, 54, 55, 56};
which initializes the elements to be
aiB[0][0] = 51 aiB[0][1] = 52 aiB[0][2] = 53
aiB[1][0] = 54 aiB[1][1] = 55 aiB[1][2] = 56
* note that C begins its subscripts at 0. The rightmost
subscript is incremented first.
PGT 106 : C PROGRAMMING 27
Multi-Dimensional Arrays
(cont..)
 can use braces ({ }) to separate rows in 2-dimensional arrays.
 For example:
 int aiC [4][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10,11,12}};
 int aiC [4][3] = {{1, 2},
{4, 5, 6},
{7},
{10,11,12}};
initializes aiC[0][2], aiC[2][1] and aiC[2][2] to be zero
 int aiC [ ][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10,11,12}};
implicitly declares the number of rows to be 4
4 rows
3 columns
rows
columns
PGT 106 : C PROGRAMMING 28
Notes on Arrays
 Arrays enable better and easier data
management system
 Closely related to loops
 Indexing is zero-based
(0 to n-1 for an array with n locations)
 Multi-dimensional arrays require
nested loop structure
(e.g. 2-dimensional array)
PGT 106 : C PROGRAMMING 29
Index out of bounds
 ‘Out of bounds’ is when (index < 0) or
(index > arraySize - 1)
 It is a run-time error, happens when an
index is outside the valid boundaries of the
array. Example:
int aiA[10]; int iX = 10
aiA[9] = 3 ; //ok
aiA[iX] = 4 ; //10 is not within the range 0..9
PGT 106 : C PROGRAMMING 30
Index out of bound
 In C, no guard against this problem
 Does not check whether index value is
within range or not
 Can result in accessing data of wrong
memory location
PGT 106 : C PROGRAMMING 31
How to overcome?
 Use defined loops
for (iLoop = 0; iLoop < 10; iLoop ++)
aiList [ iLoop ] = 0;
PGT 106 : C PROGRAMMING 32
End – Arrays (1)
Q & A!

More Related Content

Similar to C Arrays.ppt (20)

Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Ansi c
Ansi cAnsi c
Ansi c
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Arrays
ArraysArrays
Arrays
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chapter 6 arrays part-1
Chapter 6   arrays part-1Chapter 6   arrays part-1
Chapter 6 arrays part-1
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
02 arrays
02 arrays02 arrays
02 arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 

Recently uploaded

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 

Recently uploaded (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 

C Arrays.ppt

  • 1. PGT 106 : C PROGRAMMING 1 Lecture 7 – Arrays (1)
  • 2. PGT 106 : C PROGRAMMING 2 Outline 1. Introduction 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index out of bound
  • 3. PGT 106 : C PROGRAMMING 3 What is an Array?  An array is a collection/group of a fixed number of components wherein all of the components are of the same type referred to a same name
  • 4. PGT 106 : C PROGRAMMING 4 What is an Array? (Example)  Example: 5, 10, 15, 20, and 25.  Previously we would declare five variables: int iNum1, iNum2, iNum3, iNum4, iNum5;  By using array, int aiNum[5];
  • 5. PGT 106 : C PROGRAMMING 5 What is an Array? (Example) 5 10 15 20 25 aiNum aiNum[0] aiNum[1] aiNum[2] aiNum[3] aiNum[4]  5 components or elements  Elements are referred to index.  Element aiNum[2] has index 2 and value 15.
  • 6. PGT 106 : C PROGRAMMING 6 Arrays of Data  Engineering applications usually involve large chunk of data (of common type)  Arrays provide easy and efficient concept for data storage or management  Arrays are usually processed through loops (processing is very common)  Arrays are accessed by indicating an address or index/subscript
  • 7. PGT 106 : C PROGRAMMING 7 Arrays in C  Arrays can take any type (including the primitive data types) int, char, string, double, float, etc.  Like any other instances, arrays must be declared before use.
  • 8. PGT 106 : C PROGRAMMING 8 Array Declaration  Format:  data_type array_name [int value];  int aiList[5];  const int Max_Size = 10; int aiHours[Max_Size];  const int SIZE = 100; double adAmount[SIZE];  const int Max_List_Size = 6; char acAlp[Max_List_Size];  #define N 10 double adB[N];
  • 9. PGT 106 : C PROGRAMMING 9 Multiple Instances vs. Array // multiple instance int iValue1, iValue2, iValue3, iValue4, iValue 5; printf (“Enter first value: “); scanf (“%d”, &iValue1); printf(“Enter second value: “); scanf(“%d”, &iValue2); printf (“Enter third value: “); scanf(“%d”, &iValue3); printf (“Enter fourth value: “); scanf(“%d”, &iValue4) printf (“Enter fifth value: “); scanf(“%d”, &iValue5)
  • 10. UniMAP Sem II - 09/10 PGT 106 : C PROGRAMMING 10 // array int aiValue[3]; for(int iCount=0; iCount<3; iCount++) { printf (“Enter value : ”); printf (“%d : ”, iCount+1); scanf (“%d”, &aiValue[iCount]); }
  • 11. PGT 106 : C PROGRAMMING 11 Arrays - Memory Allocation  Arrays are allocated bulk memory  Single reference used for multiple locations  Items are accessed based on index (address) with reference to first item int aiValue[8]; aiValue[0]=23; aiValue[1]=56; aiValue[2]=100; aiValue[3]=0; aiValue[4]=12; aiValue[5]=234; aiValue[6]=666; aiValue[7]=4; 23 56 100 0 12 234 666 4 index aiValue 0 1 2 3 4 5 6 7
  • 12. PGT 106 : C PROGRAMMING 12 Arrays Arithmetic  Operations on arrays are similar to that on basic variables.  iSum = aiNum[0] + aiNum[1] + aiNum[2] + aiNum[3];  iMult = 3 * aiNum[1];  iRemainder = aiNum[3] % 3;  iTotal = aiNum[1] * aiNum[2];
  • 13. PGT 106 : C PROGRAMMING 13 Array Initialization  Arrays can be initialized directly, but assignments are done using loops  Like any other simple variable, arrays can also be initialized while they are being declared. double adSales[5] = {12.25, 32.50, 16.90, 23, 45.68}; adSales[0]=12.25, adSales[1]=32.50, adSales[2]=16.90, adSales[3]=23.00, adSales[4]=45.68;
  • 14. PGT 106 : C PROGRAMMING 14 Array Initialization (cont…)  Initializers:  If not enough initializers, rightmost element becomes 0 int aiN[ 7 ] = { 1, 2, 3, 4, 5 }; => aiN[5] = aiN[6] = 0  All elements = 0 int aiN[ 5 ] = { 0 } ; ▪ If size is omitted, initializers determine the size int aiN[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array
  • 15. PGT 106 : C PROGRAMMING 15 Sample Program #include <stdio.h> int main() { int aiA[3]= {11,22}, aiB[]={44, 55, 66},iLoop; double adX[2],adY[10]; printf(“aiA[0]=%2d, aiA[1]=%2d, aiA[2]=%2d n" “aiB[0]=%2d, aiB[1]=%2d, aiB[2]=%2d nn", aiA[0],aiA[1],aiA[2],aiB[0],aiB[1],aiB[2]); printf("Please enter two real numbersn"); scanf("%lf %lf",&adX[0], &adX[1]); printf(“adX[0] = %.1lf adX[1] = %.1lfnn", adX[0], adX[1]); for (iLoop=0;iLoop<10;iLoop++) { adY[iLoop]= iLoop*100.0; printf(“adY[%1d]=%.2lfn", iLoop, adY[i]); } return 0; } Using a loop to fill all the elements of the adY[] array. Initializes the first 2 elements of the aiA[]array. All the other elements are then automatically set to zero Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values.
  • 16. PGT 106 : C PROGRAMMING 16 Sample Program  Output: aiA[0]=11, aiA[1]=22, aiA[2]= 0 aiB[0]=44, aiB[1]=55, aiB[2]=66 Please enter two real numbers 77.0 88.0 adX[0] = 77.0 adX[1] = 88.0 adY[0]=0.00 adY[1]=100.00 adY[2]=200.00 adY[3]=300.00 adY[4]=400.00 adY[5]=500.00 adY[6]=600.00 adY[7]=700.00 adY[8]=800.00 adY[9]=900.00
  • 17. PGT 106 : C PROGRAMMING 17 Array Initialization During Declaration  When declaring and initializing arrays, it is not necessary to specify the size of the array.  The size of the array is determined by the number of initial values in the braces. double adSales[] = {12.25, 32.50, 16.90, 23, 45.68};
  • 18. PGT 106 : C PROGRAMMING 18 A simple example  The program declares and initializes the array aiY. It uses a ‘for’ loop with index iLoop to access the successive elements of aiY. For each loop iteration, the value accessed id is added to the variable iTotal which is finally displayed. Note that the loop index iLoop starts from 0 to 4 (not from 1 to 5). Also, note that the array size n is declared in the define statement.
  • 19. PGT 106 : C PROGRAMMING 19 A simple example (cont..) #include<stdio.h> #define n 5 // define number of n in the array void main() { int iLoop, iTotal = 0; // variable declaration int aiY[n]={9,6,20,5,12}; // array declaration and // initialization for (iLoop=0;iLoop<n;iLoop++) iTotal = iTotal + aiY[i]; printf ("nTotal = %dn”, iTotal); }
  • 20. PGT 106 : C PROGRAMMING 20 Notes  The defined constants, #define is used to ease any future amendments of the codes, for instance, if the array is to be widen to an n of 10 instead of 5, it would be adequate by modifying the line: #define n 5  #define n 10 there is no need to make any other changes to the program, thus making the life of programmer easier.
  • 21. PGT 106 : C PROGRAMMING 21 Operations on Array  Reading data in an array for (iIndex = 0; iIndex < 10; iIndex++) scanf (“%d”, &aiSale[iIndex]);  Printing an array for (iIndex = 0; iIndex < 10; iIndex++) printf (“%d ”, aiSale[iIndex]);
  • 22. PGT 106 : C PROGRAMMING 22 Parallel Arrays  Two (or more) arrays are called parallel if their corresponding components hold related information. int aiStudentId[50]; char acStudentGrade[50];
  • 23. PGT 106 : C PROGRAMMING 23 Multi-Dimensional Arrays  Arrays can have multiple dimensions  Most used is the 2-dimensional array (for matrix implementation)  Actual implementation is a single array (segmented)  Nested loop structure usually used to access items
  • 24. PGT 106 : C PROGRAMMING 24 2-Dimensional Array (Example) 5 index aiValue 0 1 2 3 4 5 6 7 5 Column 0 1 0 1 2 3 Row Address Resolution = Row*(MaxCol) + Col Row 0 Row 1 int aiValue[4][2]; aiValue[2][1]=5;
  • 25. PGT 106 : C PROGRAMMING 25 Multi-Dimensional Arrays (cont..)  A collection of the same type of data stored in contiguous and increasing memory locations.  Declaration of multi-dimensional array:  int aiB[2][3] = {51, 52, 53, 54, 55, 56}; array_type array_name Array dimension = 2 two rows three columns first row initial values second row initial values
  • 26. PGT 106 : C PROGRAMMING 26 Multi-Dimensional Arrays (cont..)  Multi-dimensional array can be initialized directly in the declaration statement.  For example:  int aiB[2][3] = {51, 52, 53, 54, 55, 56}; which initializes the elements to be aiB[0][0] = 51 aiB[0][1] = 52 aiB[0][2] = 53 aiB[1][0] = 54 aiB[1][1] = 55 aiB[1][2] = 56 * note that C begins its subscripts at 0. The rightmost subscript is incremented first.
  • 27. PGT 106 : C PROGRAMMING 27 Multi-Dimensional Arrays (cont..)  can use braces ({ }) to separate rows in 2-dimensional arrays.  For example:  int aiC [4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}};  int aiC [4][3] = {{1, 2}, {4, 5, 6}, {7}, {10,11,12}}; initializes aiC[0][2], aiC[2][1] and aiC[2][2] to be zero  int aiC [ ][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; implicitly declares the number of rows to be 4 4 rows 3 columns rows columns
  • 28. PGT 106 : C PROGRAMMING 28 Notes on Arrays  Arrays enable better and easier data management system  Closely related to loops  Indexing is zero-based (0 to n-1 for an array with n locations)  Multi-dimensional arrays require nested loop structure (e.g. 2-dimensional array)
  • 29. PGT 106 : C PROGRAMMING 29 Index out of bounds  ‘Out of bounds’ is when (index < 0) or (index > arraySize - 1)  It is a run-time error, happens when an index is outside the valid boundaries of the array. Example: int aiA[10]; int iX = 10 aiA[9] = 3 ; //ok aiA[iX] = 4 ; //10 is not within the range 0..9
  • 30. PGT 106 : C PROGRAMMING 30 Index out of bound  In C, no guard against this problem  Does not check whether index value is within range or not  Can result in accessing data of wrong memory location
  • 31. PGT 106 : C PROGRAMMING 31 How to overcome?  Use defined loops for (iLoop = 0; iLoop < 10; iLoop ++) aiList [ iLoop ] = 0;
  • 32. PGT 106 : C PROGRAMMING 32 End – Arrays (1) Q & A!