SlideShare a Scribd company logo
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

PPSX
C Programming : Arrays
PPTX
Array in c#
PPTX
Array,MULTI ARRAY, IN C
PPTX
Array in C
PDF
Lecture17 arrays.ppt
PPTX
PPTX
Arrays in c
C Programming : Arrays
Array in c#
Array,MULTI ARRAY, IN C
Array in C
Lecture17 arrays.ppt
Arrays in c

What's hot (20)

PPTX
Array in C# 3.5
PPTX
Array in c
PPTX
Array in c language
PPTX
Array in c
PPTX
Arrays in java language
PPT
Two dimensional array
PPTX
Data Structures - Lecture 3 [Arrays]
PPTX
Array Introduction One-dimensional array Multidimensional array
PDF
Chapter12 array-single-dimension
PPTX
Array lecture
PPT
Arrays Class presentation
PPTX
C++ lecture 04
PPT
Multidimensional array in C
PPTX
Array in-c
PPTX
Introduction to Array ppt
PPTX
Array in C
PPTX
A Presentation About Array Manipulation(Insertion & Deletion in an array)
PPT
One dimensional 2
PPTX
PPT
Data Structure Midterm Lesson Arrays
Array in C# 3.5
Array in c
Array in c language
Array in c
Arrays in java language
Two dimensional array
Data Structures - Lecture 3 [Arrays]
Array Introduction One-dimensional array Multidimensional array
Chapter12 array-single-dimension
Array lecture
Arrays Class presentation
C++ lecture 04
Multidimensional array in C
Array in-c
Introduction to Array ppt
Array in C
A Presentation About Array Manipulation(Insertion & Deletion in an array)
One dimensional 2
Data Structure Midterm Lesson Arrays
Ad

Similar to Array BPK 2 (20)

PPTX
Arrays in C++
PPTX
Array
PPT
Arrays in c programing. practicals and .ppt
PPTX
Unit 3
PDF
Arrays and function basic c programming notes
PPT
Fp201 unit4
PPTX
Intro to C# - part 2.pptx emerging technology
PPT
SP-First-Lecture.ppt
DOCX
Array assignment
PDF
PPTX
Array and string in C++_093547 analysis.pptx
PDF
SlideSet_4_Arraysnew.pdf
PDF
Arrays and strings in c++
PPTX
Chapter1.pptx
PPT
2DArrays.ppt
PPTX
arrays in c# including Classes handling arrays
PDF
Programming Fundamentals Arrays and Strings
PPT
PPTX
2D Array
Arrays in C++
Array
Arrays in c programing. practicals and .ppt
Unit 3
Arrays and function basic c programming notes
Fp201 unit4
Intro to C# - part 2.pptx emerging technology
SP-First-Lecture.ppt
Array assignment
Array and string in C++_093547 analysis.pptx
SlideSet_4_Arraysnew.pdf
Arrays and strings in c++
Chapter1.pptx
2DArrays.ppt
arrays in c# including Classes handling arrays
Programming Fundamentals Arrays and Strings
2D Array
Ad

More from Riki Afriansyah (9)

PPTX
Function BPK2
PPT
Database
PPTX
Latar belakang industri dan perdagangan
PDF
Pendahuluan Sistem E-Persuratan
PDF
Modul 6 Perulangan
PDF
Modul 5 Kondisional
PDF
Modul 3 Cara Membuat View Pada CodeIgniter
PDF
Modul 2 Cara Membuat Controller Pada CodeIgniter
PDF
Modul1 Penjelasan Mengenai Codeigniter
Function BPK2
Database
Latar belakang industri dan perdagangan
Pendahuluan Sistem E-Persuratan
Modul 6 Perulangan
Modul 5 Kondisional
Modul 3 Cara Membuat View Pada CodeIgniter
Modul 2 Cara Membuat Controller Pada CodeIgniter
Modul1 Penjelasan Mengenai Codeigniter

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Pharma ospi slides which help in ospi learning
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pharma ospi slides which help in ospi learning
GDM (1) (1).pptx small presentation for students
Open folder Downloads.pdf yes yes ges yes
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Final Stretch: How to Release a Game and Not Die in the Process.
Cardiovascular Pharmacology for pharmacy students.pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
master seminar digital applications in india

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)