Presentation is loading. Please wait.

Presentation is loading. Please wait.

by: Muhammad Zidny Naf’an;

Similar presentations


Presentation on theme: "by: Muhammad Zidny Naf’an;"— Presentation transcript:

1 by: Muhammad Zidny Naf’an;
Array and String 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 individually referenced 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 the same type, int for example, with a unique identifier.

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

4 Initializing Array When declare an array, it’s possible to assign initial values to each one 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 as if 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 Data_type name_of_array[row][colom];
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 1 2 3 ‘A’ ‘L’ ‘G’ ‘O’ String is array of characters Example:
char word[4] char word[] = “ALGO”; Index 1 2 3 word[index] ‘A’ ‘L’ ‘G’ ‘O’

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?
Jenis Perlakuan Fungsi dalam bahasa C Get length of string strlen(string) Coy string to other variable strcpy(string_tujuan, string_asal) Append one string to another strcat(string1, string2) Compared two strings strcmp(string1, string2) Translate to lower character tolower(char); Translate to upper character toupper(char);

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.


Download ppt "by: Muhammad Zidny Naf’an;"

Similar presentations


Ads by Google