SlideShare a Scribd company logo
1 of 27
Download to read offline
© 2003 Prentice Hall, Inc. All rights reserved.
1
Arrays
© 2003 Prentice Hall, Inc. All rights reserved.
2
Introduction
• Arrays
– Collection of similar data items
– Static entity (same size throughout program)
© 2003 Prentice Hall, Inc. All rights reserved.
Array Declaration
• Syntax:
type arrayName[array_size]
Ex. int Ar[10];
• The array elements are (all) same type <type>.
• The size of the array is indicated by array_size, the
number of elements in the array.
• array_size must be an int constant or a constant
expression. Note that an array can have multiple dimensions.
© 2003 Prentice Hall, Inc. All rights reserved.
Array Declaration
// array of 10 uninitialized ints
int Ar[10];
-- -- --
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
© 2003 Prentice Hall, Inc. All rights reserved.
5
• When declaring arrays, specify
– Name
– Type of array
• Any data type
– Number of elements
– type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
• Declaring multiple arrays of same type
– Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
© 2003 Prentice Hall, Inc. All rights reserved.
Subscripting
• Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
• To access an individual element we must apply a subscript
to array named Ar.
– A subscript is a bracketed expression.
• The expression in the brackets is known as the index.
– First element of array has index 0.
Ar[0]
– Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
– Last element has an index one less than the size of the array.
Ar[9]
• Incorrect indexing is a common error.
© 2003 Prentice Hall, Inc. All rights reserved.
7
• Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
• To refer to an element
– Specify array name and position number (index)
– Syntax: arrayname[ Index]
– First element at position 0 (elements start from index 0)
• N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
© 2003 Prentice Hall, Inc. All rights reserved.
Subscripting
// array of 10 uninitialized ints
int Ar[10];
Ar[3] = 1;
int x = Ar[3];
-- -- 1
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
Ar[4]Ar[5]Ar[6]
Ar[3]
Ar[0] Ar[2] Ar[8]Ar[9]
Ar[7]
Ar[1]
1
-- -- --
--
--
© 2003 Prentice Hall, Inc. All rights reserved.
9
• Array elements like other variables
– Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
• Can perform operations inside subscript
c[ 5 – 2 ] same as c[3]
© 2003 Prentice Hall, Inc. All rights reserved.
10
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all
elements of this array have the
same name, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the element
within array c
© 2003 Prentice Hall, Inc. All rights reserved.
11
Examples Using Arrays
• Initializing arrays
– For loop
• Set each element
– Initializer list
• Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– To set every element to same value
int n[ 5 ] = { 0 };
// int n [5] = {1} ?????
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
int n[ 10 ]; // n is an array of 10 integers
// initialize elements (all) of array n to 0
for ( int i = 0; i < 10; i++ )
n[ i ] = 0; // set element at location i to 0
cout<<"Element Value";
for ( int j = 0; j < 10; j++ )
cout<<"n"<< n [j];
}
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
13
Element values
0
0
0
0
0
0
0
0
0
0
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
14
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
15
Element
32
27
64
18
95
14
90
70
60
37
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
16
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32, 27, 64, 18, 95 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
17
Element
32
27
64
18
95
0
0
0
0
0
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
18
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {32 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
19
Element
32
0
0
0
0
0
0
0
0
0
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
20
#include <iostream>using
namespace std;
int main ()
{
int n[ 10 ] = {0 };
cout<<"Element" ;
for ( int i = 0; i < 10; i++ )
cout<<"n"<<n[i];
}
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
21
Element
0
0
0
0
0
0
0
0
0
0
© 2003 Prentice Hall, Inc. All rights reserved.
Program to print negative elements in array
#include <iostream>
using namespace std;
int main ()
{
int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 };
cout<<"Negative Elements are" ;
for ( int i = 0; i < 10; i++ )
if (n[i]<0)
{ cout<<"n"<<n[i]; }
}
22
© 2003 Prentice Hall, Inc. All rights reserved.
O/P
23
Negative Elements are
-27
-10
-56
-1
© 2003 Prentice Hall, Inc. All rights reserved.
Program to count even or odd elements in array
#include <iostream>
using namespace std;
int main ()
{ int even=0;
int odd=0;
int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 };
for ( int i = 0; i < 10; i++ )
if(n[i]%2 == 0)
{ even++; }
else
{ odd++; }
cout<<"Number of Even Elements"<<even ;
cout<<"nNumber of odd Elements"<<odd ;
}
24
© 2003 Prentice Hall, Inc. All rights reserved.
O/P
• Number of Even Elements8
• Number of odd Elements2
25
© 2003 Prentice Hall, Inc. All rights reserved.
Program to copy all array elements to another array
#include <iostream>
using namespace std;
int main ()
{
int p[10];int
n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1,76 };
for ( int i = 0; i < 10; i++ )
{p[i]=n[i];}
cout<<"second array elements aren";
for ( int j = 0; j < 10; j++ )
{ cout<<"n"<<p[j];
}
}
26
© 2003 Prentice Hall, Inc. All rights reserved.
O/P
27
32
-27
64
18
-10
78
-56
100
-1
76

More Related Content

Similar to Notes-10-Array.pdf (20)

Data structure array
Data structure  arrayData structure  array
Data structure array
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
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
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays
ArraysArrays
Arrays
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
7.basic array
7.basic array7.basic array
7.basic array
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Arrays
ArraysArrays
Arrays
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
2-Arrays.pdf
2-Arrays.pdf2-Arrays.pdf
2-Arrays.pdf
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceanilsa9823
 
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfKen Fuller
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...ranjana rawat
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)Delhi Call girls
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...robinsonayot
 
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130Suhani Kapoor
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)Soham Mondal
 
OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理cowagem
 
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfExperience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfSoham Mondal
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjLewisJB
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...Pooja Nehwal
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girlsshivangimorya083
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...poojakaurpk09
 
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceSanjay Bokadia
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectPriyanshuRawat56
 
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
 
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Patparganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
 
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Alandi Road Call Me 7737669865 Budget Friendly No Advance Booking
 
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)
 
OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理
 
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfExperience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbj
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Sa...
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
 
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector Experience
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effect
 
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 

Notes-10-Array.pdf

  • 1. © 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays
  • 2. © 2003 Prentice Hall, Inc. All rights reserved. 2 Introduction • Arrays – Collection of similar data items – Static entity (same size throughout program)
  • 3. © 2003 Prentice Hall, Inc. All rights reserved. Array Declaration • Syntax: type arrayName[array_size] Ex. int Ar[10]; • The array elements are (all) same type <type>. • The size of the array is indicated by array_size, the number of elements in the array. • array_size must be an int constant or a constant expression. Note that an array can have multiple dimensions.
  • 4. © 2003 Prentice Hall, Inc. All rights reserved. Array Declaration // array of 10 uninitialized ints int Ar[10]; -- -- -- -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1
  • 5. © 2003 Prentice Hall, Inc. All rights reserved. 5 • When declaring arrays, specify – Name – Type of array • Any data type – Number of elements – type arrayName[ arraySize ]; int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats • Declaring multiple arrays of same type – Use comma separated list, like regular variables int b[ 100 ], x[ 27 ];
  • 6. © 2003 Prentice Hall, Inc. All rights reserved. Subscripting • Declare an array of 10 integers: int Ar[10]; // array of 10 ints • To access an individual element we must apply a subscript to array named Ar. – A subscript is a bracketed expression. • The expression in the brackets is known as the index. – First element of array has index 0. Ar[0] – Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… – Last element has an index one less than the size of the array. Ar[9] • Incorrect indexing is a common error.
  • 7. © 2003 Prentice Hall, Inc. All rights reserved. 7 • Array – Consecutive group of memory locations – Same name and type (int, char, etc.) • To refer to an element – Specify array name and position number (index) – Syntax: arrayname[ Index] – First element at position 0 (elements start from index 0) • N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ] – Nth element as position N-1
  • 8. © 2003 Prentice Hall, Inc. All rights reserved. Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- -- 1 -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1 Ar[4]Ar[5]Ar[6] Ar[3] Ar[0] Ar[2] Ar[8]Ar[9] Ar[7] Ar[1] 1 -- -- -- -- --
  • 9. © 2003 Prentice Hall, Inc. All rights reserved. 9 • Array elements like other variables – Assignment, printing for an integer array c c[ 0 ] = 3; cout << c[ 0 ]; • Can perform operations inside subscript c[ 5 – 2 ] same as c[3]
  • 10. © 2003 Prentice Hall, Inc. All rights reserved. 10 c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c
  • 11. © 2003 Prentice Hall, Inc. All rights reserved. 11 Examples Using Arrays • Initializing arrays – For loop • Set each element – Initializer list • Specify each element when array declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; – To set every element to same value int n[ 5 ] = { 0 }; // int n [5] = {1} ????? – If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore 5 element array
  • 12. © 2003 Prentice Hall, Inc. All rights reserved. Outline #include <stdio.h> #include <iostream> using namespace std; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements (all) of array n to 0 for ( int i = 0; i < 10; i++ ) n[ i ] = 0; // set element at location i to 0 cout<<"Element Value"; for ( int j = 0; j < 10; j++ ) cout<<"n"<< n [j]; }
  • 13. © 2003 Prentice Hall, Inc. All rights reserved. Outline 13 Element values 0 0 0 0 0 0 0 0 0 0
  • 14. © 2003 Prentice Hall, Inc. All rights reserved. Outline 14 #include <iostream>using namespace std; int main () { int n[ 10 ] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; cout<<"Element" ; for ( int i = 0; i < 10; i++ ) cout<<"n"<<n[i]; }
  • 15. © 2003 Prentice Hall, Inc. All rights reserved. Outline 15 Element 32 27 64 18 95 14 90 70 60 37
  • 16. © 2003 Prentice Hall, Inc. All rights reserved. Outline 16 #include <iostream>using namespace std; int main () { int n[ 10 ] = {32, 27, 64, 18, 95 }; cout<<"Element" ; for ( int i = 0; i < 10; i++ ) cout<<"n"<<n[i]; }
  • 17. © 2003 Prentice Hall, Inc. All rights reserved. Outline 17 Element 32 27 64 18 95 0 0 0 0 0
  • 18. © 2003 Prentice Hall, Inc. All rights reserved. Outline 18 #include <iostream>using namespace std; int main () { int n[ 10 ] = {32 }; cout<<"Element" ; for ( int i = 0; i < 10; i++ ) cout<<"n"<<n[i]; }
  • 19. © 2003 Prentice Hall, Inc. All rights reserved. Outline 19 Element 32 0 0 0 0 0 0 0 0 0
  • 20. © 2003 Prentice Hall, Inc. All rights reserved. Outline 20 #include <iostream>using namespace std; int main () { int n[ 10 ] = {0 }; cout<<"Element" ; for ( int i = 0; i < 10; i++ ) cout<<"n"<<n[i]; }
  • 21. © 2003 Prentice Hall, Inc. All rights reserved. Outline 21 Element 0 0 0 0 0 0 0 0 0 0
  • 22. © 2003 Prentice Hall, Inc. All rights reserved. Program to print negative elements in array #include <iostream> using namespace std; int main () { int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 }; cout<<"Negative Elements are" ; for ( int i = 0; i < 10; i++ ) if (n[i]<0) { cout<<"n"<<n[i]; } } 22
  • 23. © 2003 Prentice Hall, Inc. All rights reserved. O/P 23 Negative Elements are -27 -10 -56 -1
  • 24. © 2003 Prentice Hall, Inc. All rights reserved. Program to count even or odd elements in array #include <iostream> using namespace std; int main () { int even=0; int odd=0; int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1 }; for ( int i = 0; i < 10; i++ ) if(n[i]%2 == 0) { even++; } else { odd++; } cout<<"Number of Even Elements"<<even ; cout<<"nNumber of odd Elements"<<odd ; } 24
  • 25. © 2003 Prentice Hall, Inc. All rights reserved. O/P • Number of Even Elements8 • Number of odd Elements2 25
  • 26. © 2003 Prentice Hall, Inc. All rights reserved. Program to copy all array elements to another array #include <iostream> using namespace std; int main () { int p[10];int n[ 10 ] = { 32, -27, 64, 18, -10,78,-56,100,-1,76 }; for ( int i = 0; i < 10; i++ ) {p[i]=n[i];} cout<<"second array elements aren"; for ( int j = 0; j < 10; j++ ) { cout<<"n"<<p[j]; } } 26
  • 27. © 2003 Prentice Hall, Inc. All rights reserved. O/P 27 32 -27 64 18 -10 78 -56 100 -1 76