SlideShare a Scribd company logo
EC-102 Computer System &
Programming
Lecture #8
Instructor: Jahan Zeb
Department of Computer Engineering (DCE)
College of E&ME
Introduction
 Arrays
– Structures of related data items
– Static entity (same size throughout program)
Arrays
 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)
– Format: arrayname[ position number ]
– First element at position 0
 N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
Arrays
 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]
Arrays
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
Declaring Arrays
 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 ];
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 };
• If not enough initializers, rightmost elements 0
– To set every element to same value
int n[ 5 ] = { 0 };
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
1
2 // Initializing an array.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main()
13 {
14 int n[ 10 ]; // n is an array of 10 integers
15
16 // initialize elements of array n to 0
17 for ( int i = 0; i < 10; i++ )
18 n[ i ] = 0; // set element at location i to 0
19
20 cout << "Element" << setw( 13 ) << "Value" << endl;
21
22 // output contents of array n in tabular format
23 for ( int j = 0; j < 10; j++ )
24 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
25
Declare a 10-element array of
integers.
Initialize array to 0 using a
for loop. Note that the array
has elements n[0] to n[9].
26 return 0; // indicates successful termination
27
28 } // end main
Element Value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
1
2 // Initializing an array with a declaration.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main()
13 {
14 // use initializer list to initialize array n
15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
16
17 cout << "Element" << setw( 13 ) << "Value" << endl;
18
19 // output contents of array n in tabular format
20 for ( int i = 0; i < 10; i++ )
21 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;
22
23 return 0; // indicates successful termination
24
25 } // end main
Note the use of the initializer
list.
Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37
Examples Using Arrays
 Array size
– Can be specified with constant variable (const)
• const int size = 20;
– Constants cannot be changed
– Constants must be initialized when declared
– Also called named constants or read-only variables
1
2 // Initialize array s to the even integers from 2 to 20.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main()
13 {
14 // constant variable can be used to specify array size
15 const int arraySize = 10;
16
17 int s[ arraySize ]; // array s has 10 elements
18
19 for ( int i = 0; i < arraySize; i++ ) // set the values
20 s[ i ] = 2 + 2 * i;
21
22 cout << "Element" << setw( 13 ) << "Value" << endl;
23
Note use of const keyword.
Only const variables can
specify array sizes.
The program becomes more
scalable when we set the array
size using a const variable.
We can change arraySize,
and all the loops will still
work (otherwise, we’d have to
update every loop in the
program).
24 // output contents of array s in tabular format
25 for ( int j = 0; j < arraySize; j++ )
26 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
27
28 return 0; // indicates successful termination
29
30 } // end main
Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
1
2 // Using a properly initialized constant variable.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int main()
9 {
10 const int x = 7; // initialized constant variable
11
12 cout << "The value of constant variable x is: "
13 << x << endl;
14
15 return 0; // indicates successful termination
16
17 } // end main
The value of constant variable x is: 7
Proper initialization of
const variable.
1
2 // A const object must be initialized.
3
4 int main()
5 {
6 const int x; // Error: x must be initialized
7
8 x = 7; // Error: cannot modify a const variable
9
10 return 0; // indicates successful termination
11
12 } // end main
d:cpphtp4_examplesch04Fig04_07.cpp(6) : error C2734: 'x' :
const object must be initialized
Uninitialized const results
in a syntax error. Attempting
to modify the const is
another error.
1
2 // Compute the sum of the elements of the array.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int main()
9 {
10 const int arraySize = 10;
11
12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13
14 int total = 0;
15
16 // sum contents of array a
17 for ( int i = 0; i < arraySize; i++ )
18 total += a[ i ];
19
20 cout << "Total of array element values is " << total << endl;
21
22 return 0; // indicates successful termination
23
24 } // end main
Total of array element values is 55
1
2 // Histogram printing program.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main()
13 {
14 const int arraySize = 10;
15 int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
16
17 cout << "Element" << setw( 13 ) << "Value"
18 << setw( 17 ) << "Histogram" << endl;
19
20 // for each element of array n, output a bar in histogram
21 for ( int i = 0; i < arraySize; i++ ) {
22 cout << setw( 7 ) << i << setw( 13 )
23 << n[ i ] << setw( 9 );
24
25 for ( int j = 0; j < n[ i ]; j++ ) // print one bar
26 cout << '*';
Prints asterisks corresponding
to size of array element,
n[i].
27
28 cout << endl; // start next line of output
29
30 } // end outer for structure
31
32 return 0; // indicates successful termination
33
34 } // end main
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *

More Related Content

What's hot (20)

Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Pointers
PointersPointers
Pointers
sanya6900
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Sachin Sharma
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
Aditya Nihal Kumar Singh
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
C++ references
C++ referencesC++ references
C++ references
corehard_by
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
Abdul Rehman
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 

Viewers also liked (20)

Hosting Dergi - 9.SAYI
Hosting Dergi - 9.SAYIHosting Dergi - 9.SAYI
Hosting Dergi - 9.SAYI
Hosting Dergi
 
Ejercicio 2 programación algoritmos Valentino Spina.
Ejercicio 2 programación  algoritmos Valentino Spina.Ejercicio 2 programación  algoritmos Valentino Spina.
Ejercicio 2 programación algoritmos Valentino Spina.
Valentino Spina
 
Mi auto biografía
Mi auto biografíaMi auto biografía
Mi auto biografía
dayanna2016ramirez
 
Reglamento interno itei 2014
Reglamento interno itei 2014Reglamento interno itei 2014
Reglamento interno itei 2014
Cultura San Gabriel
 
Olena teliga pr.-konf.
Olena teliga pr.-konf.Olena teliga pr.-konf.
Olena teliga pr.-konf.
TOBM Ternopil
 
Panorama sobre Teste de Software
Panorama sobre Teste de SoftwarePanorama sobre Teste de Software
Panorama sobre Teste de Software
Patrícia Araújo Gonçalves
 
Colgate-Palmolive Company: The Precision Toothbrush
Colgate-Palmolive Company: The Precision ToothbrushColgate-Palmolive Company: The Precision Toothbrush
Colgate-Palmolive Company: The Precision Toothbrush
Priyadarsini Somasundaram
 
2° informe s. gabriel 2014
2° informe s. gabriel 20142° informe s. gabriel 2014
2° informe s. gabriel 2014
Cultura San Gabriel
 
[UNIP2015] Testando a Performance de Aplicações Web com JMeter
[UNIP2015] Testando a Performance de Aplicações Web com JMeter[UNIP2015] Testando a Performance de Aplicações Web com JMeter
[UNIP2015] Testando a Performance de Aplicações Web com JMeter
Júlio de Lima
 
DevQA - Da zona de conforto ao comprometimento com a qualidade
DevQA  - Da zona de conforto ao comprometimento com a qualidadeDevQA  - Da zona de conforto ao comprometimento com a qualidade
DevQA - Da zona de conforto ao comprometimento com a qualidade
Kamilla Queiroz Xavier
 
Dem ham bang odontosil final - hn 042016
Dem ham bang odontosil   final - hn 042016Dem ham bang odontosil   final - hn 042016
Dem ham bang odontosil final - hn 042016
DentechUMP
 
Ch01 introduction
Ch01 introductionCh01 introduction
Ch01 introduction
GRajendra
 
[TICNOVA2015] Palestra sobre Automação de Testes
[TICNOVA2015] Palestra sobre Automação de Testes[TICNOVA2015] Palestra sobre Automação de Testes
[TICNOVA2015] Palestra sobre Automação de Testes
Júlio de Lima
 
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
DentechUMP
 
Colgate Precision - Harvard Business Case Analysis
Colgate Precision - Harvard Business Case AnalysisColgate Precision - Harvard Business Case Analysis
Colgate Precision - Harvard Business Case Analysis
Usha Vijay
 
Scrum for CodeLabs
Scrum for CodeLabsScrum for CodeLabs
Scrum for CodeLabs
Adam Mukharil Bachtiar
 
Mri d stream brochure_final_16 nov 12
Mri d stream brochure_final_16 nov 12Mri d stream brochure_final_16 nov 12
Mri d stream brochure_final_16 nov 12
Jhon Arriaga Cordova
 
Principal Component Analysis and Clustering
Principal Component Analysis and ClusteringPrincipal Component Analysis and Clustering
Principal Component Analysis and Clustering
Usha Vijay
 
Fluidmechanics
Fluidmechanics Fluidmechanics
Fluidmechanics
Bahauddin Zakariya University, Multan
 
Hosting Dergi - 9.SAYI
Hosting Dergi - 9.SAYIHosting Dergi - 9.SAYI
Hosting Dergi - 9.SAYI
Hosting Dergi
 
Ejercicio 2 programación algoritmos Valentino Spina.
Ejercicio 2 programación  algoritmos Valentino Spina.Ejercicio 2 programación  algoritmos Valentino Spina.
Ejercicio 2 programación algoritmos Valentino Spina.
Valentino Spina
 
Olena teliga pr.-konf.
Olena teliga pr.-konf.Olena teliga pr.-konf.
Olena teliga pr.-konf.
TOBM Ternopil
 
Colgate-Palmolive Company: The Precision Toothbrush
Colgate-Palmolive Company: The Precision ToothbrushColgate-Palmolive Company: The Precision Toothbrush
Colgate-Palmolive Company: The Precision Toothbrush
Priyadarsini Somasundaram
 
[UNIP2015] Testando a Performance de Aplicações Web com JMeter
[UNIP2015] Testando a Performance de Aplicações Web com JMeter[UNIP2015] Testando a Performance de Aplicações Web com JMeter
[UNIP2015] Testando a Performance de Aplicações Web com JMeter
Júlio de Lima
 
DevQA - Da zona de conforto ao comprometimento com a qualidade
DevQA  - Da zona de conforto ao comprometimento com a qualidadeDevQA  - Da zona de conforto ao comprometimento com a qualidade
DevQA - Da zona de conforto ao comprometimento com a qualidade
Kamilla Queiroz Xavier
 
Dem ham bang odontosil final - hn 042016
Dem ham bang odontosil   final - hn 042016Dem ham bang odontosil   final - hn 042016
Dem ham bang odontosil final - hn 042016
DentechUMP
 
Ch01 introduction
Ch01 introductionCh01 introduction
Ch01 introduction
GRajendra
 
[TICNOVA2015] Palestra sobre Automação de Testes
[TICNOVA2015] Palestra sobre Automação de Testes[TICNOVA2015] Palestra sobre Automação de Testes
[TICNOVA2015] Palestra sobre Automação de Testes
Júlio de Lima
 
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
Tru phuc hinh ca nhan trong cong nghe CAD/CAM - Ly thuyet va thuc te (P.1)
DentechUMP
 
Colgate Precision - Harvard Business Case Analysis
Colgate Precision - Harvard Business Case AnalysisColgate Precision - Harvard Business Case Analysis
Colgate Precision - Harvard Business Case Analysis
Usha Vijay
 
Mri d stream brochure_final_16 nov 12
Mri d stream brochure_final_16 nov 12Mri d stream brochure_final_16 nov 12
Mri d stream brochure_final_16 nov 12
Jhon Arriaga Cordova
 
Principal Component Analysis and Clustering
Principal Component Analysis and ClusteringPrincipal Component Analysis and Clustering
Principal Component Analysis and Clustering
Usha Vijay
 
Ad

Similar to Lecture#8 introduction to array with examples c++ (20)

Lecture#5-Arrays-oral patholohu hfFoP.ppt
Lecture#5-Arrays-oral patholohu hfFoP.pptLecture#5-Arrays-oral patholohu hfFoP.ppt
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
EdFeranil
 
its arrays ppt for first year students .
its arrays ppt for first year students .its arrays ppt for first year students .
its arrays ppt for first year students .
anilkumaralaparthi6
 
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdfLoops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
info309708
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Notes-10-Array.pdf
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdf
umeshraoumesh40
 
05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
rohassanie
 
2 arrays
2   arrays2   arrays
2 arrays
trixiacruz
 
Arrays
ArraysArrays
Arrays
fahadshakeel
 
Array 1D.................................pptx
Array 1D.................................pptxArray 1D.................................pptx
Array 1D.................................pptx
Hassan Kamal
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
Dr. Md. Shohel Sayeed
 
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
arjurakibulhasanrrr7
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
berekethailu2
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Khan
 
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSELecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
famidrabbifr
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
Tekle12
 
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Lecture#5-Arrays-oral patholohu hfFoP.pptLecture#5-Arrays-oral patholohu hfFoP.ppt
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
EdFeranil
 
its arrays ppt for first year students .
its arrays ppt for first year students .its arrays ppt for first year students .
its arrays ppt for first year students .
anilkumaralaparthi6
 
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdfLoops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
info309708
 
05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Array 1D.................................pptx
Array 1D.................................pptxArray 1D.................................pptx
Array 1D.................................pptx
Hassan Kamal
 
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
arjurakibulhasanrrr7
 
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSELecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
famidrabbifr
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
Tekle12
 
Ad

More from NUST Stuff (20)

Me211 1
Me211 1Me211 1
Me211 1
NUST Stuff
 
Lab LCA 1 7
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
NUST Stuff
 
Me211 2
Me211 2Me211 2
Me211 2
NUST Stuff
 
Me211 4
Me211 4Me211 4
Me211 4
NUST Stuff
 
Me211 3
Me211 3Me211 3
Me211 3
NUST Stuff
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
NUST Stuff
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
NUST Stuff
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
NUST Stuff
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
NUST Stuff
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
NUST Stuff
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
NUST Stuff
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
NUST Stuff
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
NUST Stuff
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
NUST Stuff
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
NUST Stuff
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
NUST Stuff
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
NUST Stuff
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
NUST Stuff
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
NUST Stuff
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
NUST Stuff
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
NUST Stuff
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
NUST Stuff
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
NUST Stuff
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
NUST Stuff
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
NUST Stuff
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
NUST Stuff
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
NUST Stuff
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
NUST Stuff
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
NUST Stuff
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
NUST Stuff
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
NUST Stuff
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
NUST Stuff
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
NUST Stuff
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
NUST Stuff
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
NUST Stuff
 

Recently uploaded (20)

Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 

Lecture#8 introduction to array with examples c++

  • 1. EC-102 Computer System & Programming Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME
  • 2. Introduction  Arrays – Structures of related data items – Static entity (same size throughout program)
  • 3. Arrays  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) – Format: arrayname[ position number ] – First element at position 0  N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ] – Nth element as position N-1
  • 4. Arrays  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]
  • 5. Arrays 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
  • 6. Declaring Arrays  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 ];
  • 7. 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 }; • If not enough initializers, rightmost elements 0 – To set every element to same value int n[ 5 ] = { 0 }; – If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore 5 element array
  • 8. 1 2 // Initializing an array. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 int n[ 10 ]; // n is an array of 10 integers 15 16 // initialize elements of array n to 0 17 for ( int i = 0; i < 10; i++ ) 18 n[ i ] = 0; // set element at location i to 0 19 20 cout << "Element" << setw( 13 ) << "Value" << endl; 21 22 // output contents of array n in tabular format 23 for ( int j = 0; j < 10; j++ ) 24 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; 25 Declare a 10-element array of integers. Initialize array to 0 using a for loop. Note that the array has elements n[0] to n[9].
  • 9. 26 return 0; // indicates successful termination 27 28 } // end main Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0
  • 10. 1 2 // Initializing an array with a declaration. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 // use initializer list to initialize array n 15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 16 17 cout << "Element" << setw( 13 ) << "Value" << endl; 18 19 // output contents of array n in tabular format 20 for ( int i = 0; i < 10; i++ ) 21 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 22 23 return 0; // indicates successful termination 24 25 } // end main Note the use of the initializer list.
  • 11. Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37
  • 12. Examples Using Arrays  Array size – Can be specified with constant variable (const) • const int size = 20; – Constants cannot be changed – Constants must be initialized when declared – Also called named constants or read-only variables
  • 13. 1 2 // Initialize array s to the even integers from 2 to 20. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 // constant variable can be used to specify array size 15 const int arraySize = 10; 16 17 int s[ arraySize ]; // array s has 10 elements 18 19 for ( int i = 0; i < arraySize; i++ ) // set the values 20 s[ i ] = 2 + 2 * i; 21 22 cout << "Element" << setw( 13 ) << "Value" << endl; 23 Note use of const keyword. Only const variables can specify array sizes. The program becomes more scalable when we set the array size using a const variable. We can change arraySize, and all the loops will still work (otherwise, we’d have to update every loop in the program).
  • 14. 24 // output contents of array s in tabular format 25 for ( int j = 0; j < arraySize; j++ ) 26 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; 27 28 return 0; // indicates successful termination 29 30 } // end main Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20
  • 15. 1 2 // Using a properly initialized constant variable. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 const int x = 7; // initialized constant variable 11 12 cout << "The value of constant variable x is: " 13 << x << endl; 14 15 return 0; // indicates successful termination 16 17 } // end main The value of constant variable x is: 7 Proper initialization of const variable.
  • 16. 1 2 // A const object must be initialized. 3 4 int main() 5 { 6 const int x; // Error: x must be initialized 7 8 x = 7; // Error: cannot modify a const variable 9 10 return 0; // indicates successful termination 11 12 } // end main d:cpphtp4_examplesch04Fig04_07.cpp(6) : error C2734: 'x' : const object must be initialized Uninitialized const results in a syntax error. Attempting to modify the const is another error.
  • 17. 1 2 // Compute the sum of the elements of the array. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 const int arraySize = 10; 11 12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 13 14 int total = 0; 15 16 // sum contents of array a 17 for ( int i = 0; i < arraySize; i++ ) 18 total += a[ i ]; 19 20 cout << "Total of array element values is " << total << endl; 21 22 return 0; // indicates successful termination 23 24 } // end main Total of array element values is 55
  • 18. 1 2 // Histogram printing program. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 const int arraySize = 10; 15 int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 16 17 cout << "Element" << setw( 13 ) << "Value" 18 << setw( 17 ) << "Histogram" << endl; 19 20 // for each element of array n, output a bar in histogram 21 for ( int i = 0; i < arraySize; i++ ) { 22 cout << setw( 7 ) << i << setw( 13 ) 23 << n[ i ] << setw( 9 ); 24 25 for ( int j = 0; j < n[ i ]; j++ ) // print one bar 26 cout << '*'; Prints asterisks corresponding to size of array element, n[i].
  • 19. 27 28 cout << endl; // start next line of output 29 30 } // end outer for structure 31 32 return 0; // indicates successful termination 33 34 } // end main Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *