SlideShare a Scribd company logo
1 of 18
1
One Dimensional Arrays
2
Arrays
Arrays are Structured Data Types
They have a means of accessing
individual components
Values can be retrieved from and stored
in the structure
scores : 85 79 92 57 68 80scores : 85 79 92 57 68 80
0 1 2 3 4 5
cout << scores[2];
scores[0] = 100;
cout << scores[2];
scores[0] = 100;
3
One Dimensional Array
Structured collection of components
» All of the same type
Structure given a single name
Individual elements accessed by index
indicating relative position in collection
Type of elements stored in an array can
be “just about” anything
Index of an array must be an integer
4
Use of Array for Our Problem
Store elements in array as read in
Go back and access for deviations
Note declarationNote declaration
5
Declaring Arrays
Syntax:
Data_type Array_name [constant];
Note declaration from our example
Tells how many elements set asideTells how many elements set aside
6
Declaring Arrays
Example specifies an array…
» each element is an integer
» there is space for 100 elements
» the are numbered 0 through 99
scores : 85 79 92 57 68 80 . . .scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
7
Accessing Individual
Components
Use the name of the array
Followed by an integer expression
inside the square brackets [ ]
scores : 85 79 92 57 68 80 . . .scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
max = scores[0];
for (x = 0; x < 100; x++)
if (scores[x] > max)
max = scores[x];
max = scores[0];
for (x = 0; x < 100; x++)
if (scores[x] > max)
max = scores[x];
Index can be:
- constant
- variable
- expression
MUST be an integer
Index can be:
- constant
- variable
- expression
MUST be an integer
8
Out of Bounds Index
What happens if …
C++ does NOT check for index out of
range
Possible to walk off into “far reaches” of
memory -- clobbers ...
» other variable locations
» .exe code
» the operating system (??)
float f_list [50];
f_list [100] = 123.456;
float f_list [50];
f_list [100] = 123.456;
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 }int results [5] = {14, 6, 23, 8, 12 }
float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }
10
Aggregate Operations
Defn => an operation on the data
structure as a whole
» as opposed to operation on a SINGLE
element within the structure
Example
» would be nice to read in a WHOLE
array
11
Lack of Aggregate Operations
Would be nice but . . .
C++ does NOT have . . .
Assignment operator for whole array
Arithmetic operations for whole array
(think matrix)
Comparisons for arrays (not even = =)
Return of an array type by a function
12
How to Accomplish
Aggregate Operations?
Most such tasks (assignment, read,
write) can be performed some other way
» CS II course will write “classes” to
provide these functions
Otherwise
» these operations must be performed
by the programmer
» element by element in a loop
13
Arrays as Parameters
This is one task that CAN be done to the
WHOLE array
C++ always passes arrays by reference
14
Arrays as Parameters
The name of the array is a pointer
constant
The address of the array is passed to
the function
Size of the
array also
passed to
control loop
15
Arrays as Parameters
Note the empty brackets in parameter
list
» A number can be placed here but it
will be
ignored
16
Sub-array Processing
Note we specified an array size of 100
» but we don’t anticipate that many scores
Array always declared larger than needed
Must keep track of how many have been
used
» this is our limit when doing other things to
the array
17
C-Strings or Character Arrays
We have learned that the elements of
an array can be just about anything
Consider an array whose elements are
all characters
» Called a C-String
» Has a collection of special routines
» Treated differently for I/O than other
types of arrays
18
Declaration of C-Strings
Similar to declaration of any array
char name[30];
// no initialization
char title [20] = "Le Grande Fromage";
// initialized at declaration
// with a string
char chList [10] = {'a', 'b', 'c', 'd'};
// initialized with list of char
// values

More Related Content

What's hot (20)

Array ppt
Array pptArray ppt
Array ppt
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
2D Array
2D Array 2D Array
2D Array
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Java arrays
Java arraysJava arrays
Java arrays
 
Array in C
Array in CArray in C
Array in C
 
Python set
Python setPython set
Python set
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 

Viewers also liked

Viewers also liked (11)

searching
searchingsearching
searching
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
Structured data type
Structured data typeStructured data type
Structured data type
 
Arrays
ArraysArrays
Arrays
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
 
One Dimentional Array
One Dimentional ArrayOne Dimentional Array
One Dimentional Array
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Array in c language
Array in c languageArray in c language
Array in c language
 

Similar to One dimensional 2 (20)

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Ch08
Ch08Ch08
Ch08
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
2 arrays
2   arrays2   arrays
2 arrays
 
Array assignment
Array assignmentArray assignment
Array assignment
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Arrays
ArraysArrays
Arrays
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Day2
Day2Day2
Day2
 
Array
ArrayArray
Array
 

More from Rajendran

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower boundsRajendran
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsRajendran
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower boundsRajendran
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theoremRajendran
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theoremRajendran
 
Master method
Master method Master method
Master method Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of QuicksortRajendran
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 

More from Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hash table
Hash tableHash table
Hash table
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
computer languages
computer languagescomputer languages
computer languages
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

One dimensional 2

  • 2. 2 Arrays Arrays are Structured Data Types They have a means of accessing individual components Values can be retrieved from and stored in the structure scores : 85 79 92 57 68 80scores : 85 79 92 57 68 80 0 1 2 3 4 5 cout << scores[2]; scores[0] = 100; cout << scores[2]; scores[0] = 100;
  • 3. 3 One Dimensional Array Structured collection of components » All of the same type Structure given a single name Individual elements accessed by index indicating relative position in collection Type of elements stored in an array can be “just about” anything Index of an array must be an integer
  • 4. 4 Use of Array for Our Problem Store elements in array as read in Go back and access for deviations Note declarationNote declaration
  • 5. 5 Declaring Arrays Syntax: Data_type Array_name [constant]; Note declaration from our example Tells how many elements set asideTells how many elements set aside
  • 6. 6 Declaring Arrays Example specifies an array… » each element is an integer » there is space for 100 elements » the are numbered 0 through 99 scores : 85 79 92 57 68 80 . . .scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99
  • 7. 7 Accessing Individual Components Use the name of the array Followed by an integer expression inside the square brackets [ ] scores : 85 79 92 57 68 80 . . .scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99 max = scores[0]; for (x = 0; x < 100; x++) if (scores[x] > max) max = scores[x]; max = scores[0]; for (x = 0; x < 100; x++) if (scores[x] > max) max = scores[x]; Index can be: - constant - variable - expression MUST be an integer Index can be: - constant - variable - expression MUST be an integer
  • 8. 8 Out of Bounds Index What happens if … C++ does NOT check for index out of range Possible to walk off into “far reaches” of memory -- clobbers ... » other variable locations » .exe code » the operating system (??) float f_list [50]; f_list [100] = 123.456; float f_list [50]; f_list [100] = 123.456;
  • 9. 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 }int results [5] = {14, 6, 23, 8, 12 } float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }
  • 10. 10 Aggregate Operations Defn => an operation on the data structure as a whole » as opposed to operation on a SINGLE element within the structure Example » would be nice to read in a WHOLE array
  • 11. 11 Lack of Aggregate Operations Would be nice but . . . C++ does NOT have . . . Assignment operator for whole array Arithmetic operations for whole array (think matrix) Comparisons for arrays (not even = =) Return of an array type by a function
  • 12. 12 How to Accomplish Aggregate Operations? Most such tasks (assignment, read, write) can be performed some other way » CS II course will write “classes” to provide these functions Otherwise » these operations must be performed by the programmer » element by element in a loop
  • 13. 13 Arrays as Parameters This is one task that CAN be done to the WHOLE array C++ always passes arrays by reference
  • 14. 14 Arrays as Parameters The name of the array is a pointer constant The address of the array is passed to the function Size of the array also passed to control loop
  • 15. 15 Arrays as Parameters Note the empty brackets in parameter list » A number can be placed here but it will be ignored
  • 16. 16 Sub-array Processing Note we specified an array size of 100 » but we don’t anticipate that many scores Array always declared larger than needed Must keep track of how many have been used » this is our limit when doing other things to the array
  • 17. 17 C-Strings or Character Arrays We have learned that the elements of an array can be just about anything Consider an array whose elements are all characters » Called a C-String » Has a collection of special routines » Treated differently for I/O than other types of arrays
  • 18. 18 Declaration of C-Strings Similar to declaration of any array char name[30]; // no initialization char title [20] = "Le Grande Fromage"; // initialized at declaration // with a string char chList [10] = {'a', 'b', 'c', 'd'}; // initialized with list of char // values