SlideShare a Scribd company logo
1 of 12
Bahasa
Pemograman
Komputer (BPK) 2
Riki Afriansyah
Contents
Contents
What is a
Array?
Syntax of
Array
Two-
Dimensional
Arrays
Passing
Arrays to
Functions
What is a Array?
 An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
 An array must be defined before it can be used to store information.
 An array definition specifies a variabel type and a name.
Syntax of Function
dataType arrayName[arraySize];
Example
C++ program to store and print 5 number entered by the user
using arrays.
Two-Dimensional Arrays
you can create an array of an array known as multi-dimensional array.
data_type array_name[x][y]; For example: int x[3][4];
Here, x is a two dimensional array. It can hold a maximum of 12 elements. You can think
this array as table with 3 rows and each row has 4 columns as shown below.
Two-Dimensional Arrays
#include <iostream>
using namespace std;
int main () {
// an array with 2 rows and 2 columns.
int a[2][2];
Int i,j;
// input each array element's value
for ( i = 0; i < 2; i++ )
for ( j = 0; j < 2; j++ ) {
cout << "a[" << i << "][" << j << "]: ";
cin>> a[i][j];
cout<< endl;
}
// output each array element's value
cout<< "PRINT" <<endl;
for ( i = 0; i < 2; i++ )
for ( j = 0; j < 2; j++ ) {
cout << "a[" << i << "][" << j << "]: ";
cout<<a[i][j]<< endl;
}
}
Passing Arrays to Functions
C program to pass an array containing age of person to a function. This function should
find average age and display the average age in main function.
#include <iostream>
#include <iomanip>
using namespace std;
float average(float age[]);
int main()
{
float avg, age[6];
cout<<"age:"<<endl;
for (int i=0;i<6;i++){
cin>>age[i];
}
avg = average(age); /* Only name of array is passed as
argument. */
cout<<"Average age="<<setprecision(2)<<avg;
}
float average(float age[])
{
int i;
float avg, sum = 0.0;
for (i = 0; i < 6; ++i) {
sum += age[i];
}
avg = (sum / 6);
return avg;
}
EXERCISES 1
Write a program that to store and print odd & even number entered
by the user (Storing 10 number entered by user in an array)
EXERCISES 2
Write a program using function and array that to store and print value of f(x) = x2+
3x + 1, then find the values. (Storing 5 number entered by user in an array)
EXERCISES 3
Write a program that calculates Two-Dimensional
Arrays (Add & Substract)
Array BPK 2

More Related Content

What's hot

Array in c language
Array in c languageArray in c language
Array in c languageumesh patil
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimensionDeepak Singh
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentationNeveen Reda
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in CSmit Parikh
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)Imdadul Himu
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2Rajendran
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 

What's hot (20)

Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Array in c
Array in cArray in c
Array in c
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Array in c
Array in cArray in c
Array in c
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
Array lecture
Array lectureArray lecture
Array lecture
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Array in-c
Array in-cArray in-c
Array in-c
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in C
Array in CArray in C
Array in C
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Arrays
ArraysArrays
Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 

Similar to Array BPK 2

Similar to Array BPK 2 (20)

Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Array
ArrayArray
Array
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays
ArraysArrays
Arrays
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
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
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
2D Array
2D Array 2D Array
2D Array
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 

More from Riki Afriansyah

Latar belakang industri dan perdagangan
Latar belakang industri dan perdaganganLatar belakang industri dan perdagangan
Latar belakang industri dan perdaganganRiki Afriansyah
 
Pendahuluan Sistem E-Persuratan
Pendahuluan Sistem E-PersuratanPendahuluan Sistem E-Persuratan
Pendahuluan Sistem E-PersuratanRiki Afriansyah
 
Modul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniterModul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniterRiki Afriansyah
 
Modul 2 Cara Membuat Controller Pada CodeIgniter
Modul 2 Cara Membuat Controller Pada CodeIgniterModul 2 Cara Membuat Controller Pada CodeIgniter
Modul 2 Cara Membuat Controller Pada CodeIgniterRiki Afriansyah
 
Modul1 Penjelasan Mengenai Codeigniter
Modul1 Penjelasan Mengenai CodeigniterModul1 Penjelasan Mengenai Codeigniter
Modul1 Penjelasan Mengenai CodeigniterRiki Afriansyah
 

More from Riki Afriansyah (9)

Function BPK2
Function BPK2Function BPK2
Function BPK2
 
Database
DatabaseDatabase
Database
 
Latar belakang industri dan perdagangan
Latar belakang industri dan perdaganganLatar belakang industri dan perdagangan
Latar belakang industri dan perdagangan
 
Pendahuluan Sistem E-Persuratan
Pendahuluan Sistem E-PersuratanPendahuluan Sistem E-Persuratan
Pendahuluan Sistem E-Persuratan
 
Modul 6 Perulangan
Modul 6 PerulanganModul 6 Perulangan
Modul 6 Perulangan
 
Modul 5 Kondisional
Modul 5 KondisionalModul 5 Kondisional
Modul 5 Kondisional
 
Modul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniterModul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniter
 
Modul 2 Cara Membuat Controller Pada CodeIgniter
Modul 2 Cara Membuat Controller Pada CodeIgniterModul 2 Cara Membuat Controller Pada CodeIgniter
Modul 2 Cara Membuat Controller Pada CodeIgniter
 
Modul1 Penjelasan Mengenai Codeigniter
Modul1 Penjelasan Mengenai CodeigniterModul1 Penjelasan Mengenai Codeigniter
Modul1 Penjelasan Mengenai Codeigniter
 

Recently uploaded

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
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
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
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
 
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
 

Recently uploaded (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
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
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
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
 
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
 

Array BPK 2

  • 2. Contents Contents What is a Array? Syntax of Array Two- Dimensional Arrays Passing Arrays to Functions
  • 3. What is a Array?  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.  An array must be defined before it can be used to store information.  An array definition specifies a variabel type and a name.
  • 4. Syntax of Function dataType arrayName[arraySize];
  • 5. Example C++ program to store and print 5 number entered by the user using arrays.
  • 6. Two-Dimensional Arrays you can create an array of an array known as multi-dimensional array. data_type array_name[x][y]; For example: int x[3][4]; Here, x is a two dimensional array. It can hold a maximum of 12 elements. You can think this array as table with 3 rows and each row has 4 columns as shown below.
  • 7. Two-Dimensional Arrays #include <iostream> using namespace std; int main () { // an array with 2 rows and 2 columns. int a[2][2]; Int i,j; // input each array element's value for ( i = 0; i < 2; i++ ) for ( j = 0; j < 2; j++ ) { cout << "a[" << i << "][" << j << "]: "; cin>> a[i][j]; cout<< endl; } // output each array element's value cout<< "PRINT" <<endl; for ( i = 0; i < 2; i++ ) for ( j = 0; j < 2; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout<<a[i][j]<< endl; } }
  • 8. Passing Arrays to Functions C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function. #include <iostream> #include <iomanip> using namespace std; float average(float age[]); int main() { float avg, age[6]; cout<<"age:"<<endl; for (int i=0;i<6;i++){ cin>>age[i]; } avg = average(age); /* Only name of array is passed as argument. */ cout<<"Average age="<<setprecision(2)<<avg; } float average(float age[]) { int i; float avg, sum = 0.0; for (i = 0; i < 6; ++i) { sum += age[i]; } avg = (sum / 6); return avg; }
  • 9. EXERCISES 1 Write a program that to store and print odd & even number entered by the user (Storing 10 number entered by user in an array)
  • 10. EXERCISES 2 Write a program using function and array that to store and print value of f(x) = x2+ 3x + 1, then find the values. (Storing 5 number entered by user in an array)
  • 11. EXERCISES 3 Write a program that calculates Two-Dimensional Arrays (Add & Substract)