Bermain dengan Array dan String

advertisement
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.

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.
Deklarasi Array
int x[5];
x[0] = 100;
x[1] = 200;
x[2] = 300;
x[3] = 400;
x[4] = 500;
Number
of
element
Data
type
variable

Contoh:
index
value
0
100
1
200
2
300
3
400
4
500
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};
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];
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;
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
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();
}
Exercise 2


From exercise 1, create one function to count the
average with an array in parameter.
And passing array to function.
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

Multidimension Array


It’s a matrix.
Multidimension Array consist of many row and many
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} };
Example 1
Passing Multidimension Array to Function

Example:
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]);
}
}
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;
}
}
String


String is array of characters
Example:
char word[4]
char word[] = “ALGO”;
Index
word[index]
0
‘A’
1
‘L’
2
‘G’
3
‘O’
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();
}
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);
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
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();
}
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”
Beberapa Permasalahan lain yang berkaitan
dengan Array



Mencari nilai maksimum dan minimum dalam larik
Mengurutkan bilangan (sorting)
Operasi matrik
SELAMAT MENCOBA!
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!
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