SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie Array BPK 2

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 

Kürzlich hochgeladen (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

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)