SlideShare a Scribd company logo
Arrays
CS 308 – Data Structures
One-Dimensional Arrays
• A list of values with the same data type that
are stored using a single group name (array
name).
• General array declaration statement:
data-type array-name[number-of-items];
• The number-of-items must be specified
before declaring the array.
const int SIZE = 100;
float arr[SIZE];
• Individual elements of the array can be
accessed by specifying the name of the
array and the element's index:
arr[3]
• Warning: indices assume values from 0 to
number-of-items -1!!
One-Dimensional Arrays (cont.)
One-Dimensional Arrays (cont.)
arr[0] arr[1] arr[2] arr[3] arr[4]
Skip over 3 elements to get
the starting location of
element 3
The array name arr identifies
the starting location of the array
Start here
element 3
1D Array Initialization
• Arrays can be initialized during their declaration
int arr[5] = {98, 87, 92, 79, 85};
int arr[5] = {98, 87} - what happens in this case??
• What is the difference between the following two
declarations ?
char codes[] = {'s', 'a', 'm', 'p', 'l', 'e'};
char codes[] = "sample";
codes[0] codes[1] codes[2] codes[3] codes[4] codes[5] codes[6]
s a m p l e 0
Two-dimensional Arrays
• A two-dimensional array consists of both rows and
columns of elements.
• General array declaration statement:
data-type array-name[number-of-rows][number-of-columns];
• The number-of-rows and number-of-columns must
be specified before declaring the array.
const int ROWS = 100;
const int COLS = 50;
float arr2D[ROWS][COLS];
• Individual elements of the array can be accessed
by specifying the name of the array and the
element's row, column indices.
arr2D[3][5]
Two-dimensional Arrays (cont.)
2D Array Initialization
• Arrays can be initialized during their
declaration
int arr2D[3][3] = { {98, 87, 92}, {79, 85, 19},
{32, 18, 2} };
• The compiler fills the array row by row
(elements are stored in the memory in the
same order).
1D Arrays as Arguments
• Individual array elements are passed to a
function in the same manner as other
variables.
max = find_max(arr[1], arr[3]);
• To pass the whole array to a function, you
need to specify the name of the array only!!
#include <iostream.h>
float find_average(int [], int);
void main()
{
const numElems = 5;
int arr[numElems] = {2, 18, 1, 27, 16};
cout << "The average is " << find_average(arr, numElems) << endl;
}
float find_average(int vals[], int n)
{
int i;
float avg;
avg=0.0;
for(i=0; i<n; i++)
avg += vals[i];
avg = avg/n;
return avg;
}
• Important: this is essentially "call by
reference":
a) The name of the array arr stores the address of the
first element of the array arr[0] (i.e., &arr[0]).
b) Every other element of the array can be accessed by
using its index as an offset from the first element.
1D Arrays as Arguments (cont.)
arr[0] arr[1] arr[2] arr[3] arr[4]
The starting address of arr array is &arr[0].
This is passed to the function find_average()
2D Arrays as Arguments
• Individual array elements are passed to a function
in the same manner as other variables.
max = find_max(arr2D[1][1], arr2D[1][2]);
• To pass the whole array to a function, you need to
specify the name of the array only!!
• The number of columns must be specified in the
function prototype and function header.
#include <iostream.h>
float find_average(int [][2], int, int);
void main()
{
const numRows = 2;
const numCols = 2;
int arr2D[numRows][numCols] = {2, 18, 1, 27};
float average;
average = find_average(arr2D, numRows, numCols);
cout << "The average is " << average << endl;
}
float find_average(int vals[][2], int n, int m)
{
int i,j;
float avg;
avg=0.0;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
avg += vals[i][j];
avg = avg/(n*m);
return avg;
}
• Important: this is essentially "call by
reference":
a) The name of the array arr2D stores the
address of arr2D[0] (i.e., &arr2D[0])
b) arr2D[0] stores the address of the first
element of the array arr2D[0][0]
(&arr2D[0][0])
c) Every other element of the array can be
accessed by using its indices as an offset from
the first element.
2D Arrays as Arguments (cont.)

More Related Content

Similar to ReviewArrays.ppt

Arrays
ArraysArrays
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 
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
 
Arrays
ArraysArrays
Arrays
ArraysArrays
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
MianSaeedAkbar1
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Arrays
ArraysArrays
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
2D arrays
2D arrays2D arrays
Module1_arrays.pptx
Module1_arrays.pptxModule1_arrays.pptx
Module1_arrays.pptx
HishamE1
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
7.basic array
7.basic array7.basic array
7.basic array
Mir Riyanul Islam
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 

Similar to ReviewArrays.ppt (20)

Arrays
ArraysArrays
Arrays
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling 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
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Unit 2
Unit 2Unit 2
Unit 2
 
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
 
2D Array
2D Array 2D Array
2D Array
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays
ArraysArrays
Arrays
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
2D arrays
2D arrays2D arrays
2D arrays
 
Module1_arrays.pptx
Module1_arrays.pptxModule1_arrays.pptx
Module1_arrays.pptx
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
7.basic array
7.basic array7.basic array
7.basic array
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 

Recently uploaded

Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Enterprise Wired
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 

Recently uploaded (20)

Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 

ReviewArrays.ppt

  • 1. Arrays CS 308 – Data Structures
  • 2. One-Dimensional Arrays • A list of values with the same data type that are stored using a single group name (array name). • General array declaration statement: data-type array-name[number-of-items]; • The number-of-items must be specified before declaring the array. const int SIZE = 100; float arr[SIZE];
  • 3. • Individual elements of the array can be accessed by specifying the name of the array and the element's index: arr[3] • Warning: indices assume values from 0 to number-of-items -1!! One-Dimensional Arrays (cont.)
  • 4. One-Dimensional Arrays (cont.) arr[0] arr[1] arr[2] arr[3] arr[4] Skip over 3 elements to get the starting location of element 3 The array name arr identifies the starting location of the array Start here element 3
  • 5. 1D Array Initialization • Arrays can be initialized during their declaration int arr[5] = {98, 87, 92, 79, 85}; int arr[5] = {98, 87} - what happens in this case?? • What is the difference between the following two declarations ? char codes[] = {'s', 'a', 'm', 'p', 'l', 'e'}; char codes[] = "sample"; codes[0] codes[1] codes[2] codes[3] codes[4] codes[5] codes[6] s a m p l e 0
  • 6. Two-dimensional Arrays • A two-dimensional array consists of both rows and columns of elements. • General array declaration statement: data-type array-name[number-of-rows][number-of-columns];
  • 7. • The number-of-rows and number-of-columns must be specified before declaring the array. const int ROWS = 100; const int COLS = 50; float arr2D[ROWS][COLS]; • Individual elements of the array can be accessed by specifying the name of the array and the element's row, column indices. arr2D[3][5] Two-dimensional Arrays (cont.)
  • 8. 2D Array Initialization • Arrays can be initialized during their declaration int arr2D[3][3] = { {98, 87, 92}, {79, 85, 19}, {32, 18, 2} }; • The compiler fills the array row by row (elements are stored in the memory in the same order).
  • 9. 1D Arrays as Arguments • Individual array elements are passed to a function in the same manner as other variables. max = find_max(arr[1], arr[3]); • To pass the whole array to a function, you need to specify the name of the array only!!
  • 10. #include <iostream.h> float find_average(int [], int); void main() { const numElems = 5; int arr[numElems] = {2, 18, 1, 27, 16}; cout << "The average is " << find_average(arr, numElems) << endl; } float find_average(int vals[], int n) { int i; float avg; avg=0.0; for(i=0; i<n; i++) avg += vals[i]; avg = avg/n; return avg; }
  • 11. • Important: this is essentially "call by reference": a) The name of the array arr stores the address of the first element of the array arr[0] (i.e., &arr[0]). b) Every other element of the array can be accessed by using its index as an offset from the first element. 1D Arrays as Arguments (cont.) arr[0] arr[1] arr[2] arr[3] arr[4] The starting address of arr array is &arr[0]. This is passed to the function find_average()
  • 12. 2D Arrays as Arguments • Individual array elements are passed to a function in the same manner as other variables. max = find_max(arr2D[1][1], arr2D[1][2]); • To pass the whole array to a function, you need to specify the name of the array only!! • The number of columns must be specified in the function prototype and function header.
  • 13. #include <iostream.h> float find_average(int [][2], int, int); void main() { const numRows = 2; const numCols = 2; int arr2D[numRows][numCols] = {2, 18, 1, 27}; float average; average = find_average(arr2D, numRows, numCols); cout << "The average is " << average << endl; }
  • 14. float find_average(int vals[][2], int n, int m) { int i,j; float avg; avg=0.0; for(i=0; i<n; i++) for(j=0; j<m; j++) avg += vals[i][j]; avg = avg/(n*m); return avg; }
  • 15. • Important: this is essentially "call by reference": a) The name of the array arr2D stores the address of arr2D[0] (i.e., &arr2D[0]) b) arr2D[0] stores the address of the first element of the array arr2D[0][0] (&arr2D[0][0]) c) Every other element of the array can be accessed by using its indices as an offset from the first element. 2D Arrays as Arguments (cont.)