1 / 25

Array and String

Array and String. by: Muhammad Zidny Naf’an; . Array / Larik. An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

layne
Download Presentation

Array and String

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array andString by: Muhammad Zidny Naf’an;

  2. Array / Larik • An array is a series of elements of the same type placed in contiguous memory locations that can be individuallyreferenced by adding an index to a unique identifier. • So.. we can store 5 values of type int in an array without having to declare 5 different variables • using an array we can store 5 different values of thesame type, int for example, with a unique identifier.

  3. Deklarasi Array intx[5]; • Contoh: x[0] = 100; x[1] = 200; x[2] = 300; x[3] = 400; x[4] = 500; Number of element Data type variable

  4. Initializing Array • When declare an array, it’spossibleto assign initial values to eachone of its elements by enclosing the values in braces { } • Example: int x[ ] = {4, 3,6,1,8,9,10,8,2,5};

  5. Accessing the values of an array • we can access the value of any of its elements individually asif it was a normal variable • For example, to store the value 75 in the third element of x, we could write the following statement: x[2] = 50 • to pass the value of the third element of x to a variable called a, we can use: a = x[2];

  6. Accessing the values of an array • Some other valid operations with arrays: x[0] = a; x[a] = 75; b = x [a+2]; x[x[a]] = x[2] + 5;

  7. Exercise 1 • There are10 elements in an array. • Each value of element are: 4, 3,6,1,8,9,10,8,2,5. • Count average from 10 elements above. • Create program in C++ for this problem

  8. Passing Array to Function • use passing by reference #include <stdio.h> #include <iostream.h> void cetak_array(int index, int *Array) { printf(“Array[%d]=%d\n”, index, Array[index]); } int main() { int Array[] = {1, 6, 2, 8, 12}; cetak_array (2, Array); cin.get(); }

  9. Exercise 2 • From exercise 1, create one function to count the average with an array in parameter. • And passing array to function.

  10. Exercise 3 • Create a program to search a number in array • For example, value of element in array: 4, 3,6,1,8,9,10,8,2,5 • IF number finded then show the index of it’s element

  11. Multidimension Array • It’s a matrix. • Multidimension Array consist of many row and many colom

  12. Declaration Data_type name_of_array[row][colom]; • Example: int matrix[3][4]; int matrix[3][4] = { {5,20,1,11}, {4, 7, 67, -9}, {9,0,45,3} };

  13. Example 1

  14. Passing Multidimension Array to Function • Example:

  15. Fill The Matrix void fill_matriks(int MatX[][MATSIZE],int row, int col) { int i,j; for(i=0;i<row;i++) for(j=0;j<col;j++){ cout << "Element Matriks [“<<i<<”,”<<j; cin >> MatX[i][j]); } }

  16. Print out the Matrix void print_matrix(int MatX[][MATSIZE],int row,int co) { int i,j; for(i=0;i<row;i++){ for(j=0;j<col;j++) cout << MatX[i][j]); cout << endl; } }

  17. String • String is array of characters • Example: char word[4] char word[] = “ALGO”;

  18. Exercise 4 • Create ASCII table in C #include <conio.h> #include <stdio.h> #include <string.h> void main() { int i = 0; for(i=0;i<=255;i++) { printf("%c = %d\n",char(i),i); } getch(); }

  19. What we can do with string?

  20. Array of String • Declaration array of string: char nama[10][50]; • It’s mean, there are 10 string, which each of string contents maximal 50 characters

  21. Example Array of String #include <stdio.h> #include <conio.h> int main() { int x,y; char Bulan[12][4] = {"Jan", "Peb", "Mar", "Apr", "Mei", "Jun", "Jul", "Ags", "Sep", "Okt", "Nop", "Des"}; for(x=0;x<12;x++) { for(y=0;y<3;y++) cout << Bulan[x][y]); printf(" "); } getch(); }

  22. Exercise • Dengan menggunakan array dan string, buat program untuk mengganti karakter tertentu pada string dengan karakter lain. • Contoh: “Karakter-karakter dalam komputer biasanya merujuk pada tabel ASCII” Ganti semua karakter ‘a’ dengan karakter ‘o’, sehingga output dari program adalah: “Korokter-korokter dolom komputer biosonyo merujuk podo tobel ASCII”

  23. Beberapa Permasalahan lain yang berkaitan dengan Array • Mencari nilai maksimum dan minimum dalam larik • Mengurutkan bilangan (sorting) • Operasi matrik SELAMAT MENCOBA!

  24. Beberapa Permasalahan lain yang berkaitan dengan Array • Mengurutkan string • Mencari kata dalam string • Menghitung jumlah kata tertentu dalam string • Menggantikan suatu kata/karakter dengan karakter lain SELAMAT MENCOBA!

  25. Soal Kompetisi ACM A common typing error is to place your hands on the keyboard one row to the right of the correct position. Then “Q” is typed as “W” and “J” is typed as “K” and so on. Your task is to decode a message typed in this manner. • Input Input consists of several lines of text. Each line may contain digits, spaces, uppercase letters (except “Q”, “A”, “Z”), or punctuation shown above [except back-quote (‘)]. Keys labeled with words [Tab, BackSp, Control, etc.] are not represented in the input. • Output You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output. • Sample Input O S, GOMR YPFSU/ • Sample Output I AM FINE TODAY.

More Related