array and string

25
Array and String by: Muhammad Zidny Naf’an;

Upload: layne

Post on 22-Feb-2016

51 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Array  and String

Array and String

by:Muhammad Zidny Naf’an;

Page 2: Array  and String

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.

Page 3: Array  and String

Deklarasi Array

int x[5];

Contoh:

Data type

variable

Number of

element

index

0 1 2 3 4

value

100 200 300 400 500

x[0] = 100;x[1] = 200;x[2] = 300;x[3] = 400;x[4] = 500;

Page 4: Array  and String

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};

Page 5: Array  and String

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];

Page 6: Array  and String

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;

Page 7: Array  and String

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

Page 8: Array  and String

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();}

Page 9: Array  and String

Exercise 2 From exercise 1, create one function to count

the average with an array in parameter. And passing array to function.

Page 10: Array  and String

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

Page 11: Array  and String

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

many colom

Page 12: Array  and String

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} };

Page 13: Array  and String

Example 1

Page 14: Array  and String

Passing Multidimension Array to Function

Example:

Page 15: Array  and String

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]);

}}

Page 16: Array  and String

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;

}}

Page 17: Array  and String

String String is array of characters Example:

char word[4]char word[] = “ALGO”;

Index 0 1 2 3word[index] ‘A’ ‘L’ ‘G’ ‘O’

Page 18: Array  and String

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();}

Page 19: Array  and String

What we can do with string?

Jenis Perlakuan Fungsi dalam bahasa CGet 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);

Page 20: Array  and String

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

Page 21: Array  and String

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();}

Page 22: Array  and String

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”

Page 23: Array  and String

Beberapa Permasalahan lain yang berkaitan dengan Array Mencari nilai maksimum dan minimum dalam

larik Mengurutkan bilangan (sorting) Operasi matrik

SELAMAT MENCOBA!

Page 24: Array  and String

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!

Page 25: Array  and String

Soal Kompetisi ACMA common typing error is to place your hands on the keyboard one row to the rightof 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.

InputInput consists of several lines of text. Each line may contain digits, spaces, uppercaseletters (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.

OutputYou are to replace each letter or punctuation symbol by the one immediately to its lefton the QWERTY keyboard shown above. Spaces in the input should be echoed in theoutput.

Sample InputO S, GOMR YPFSU/

Sample OutputI AM FINE TODAY.