by: Muhammad Zidny Naf’an;

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Data Types Session 2.  Primitive data types  int, float, double, char  Aggregate data types  Arrays come.
Strings.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Fundamental Programming: Fundamental Programming Introduction to C++
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 05 (Part III) Control Statements: Part II.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Objective: Students will be able to: Declare and use variables Input integers.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C++ Programming Basics
Strings, Slide Fundamental Programming Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Objectives You should be able to describe: One-Dimensional Arrays
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Variabel Dan Tipe data
Basic concepts of C++ Presented by Prof. Satyajit De
Variables Mr. Crone.
LESSON 2 Basic of C++.
INC 161 , CPE 100 Computer Programming
Fundamentals of Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
Standard Input - Output
Programming Fundamental
Computing Fundamentals
BASIC ELEMENTS OF A COMPUTER PROGRAM
A First Book of ANSI C Fourth Edition
Variabel Dan Tipe data
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
Engineering Problem Solving with C++, Etter/Ingber
Lecture 8b: Strings BJ Furman 15OCT2012.
Arrays Skill Area 315 Part A
7 Arrays.
Introduction to C++ Programming
Programming Funamental slides
CNG 140 C Programming (Lecture set 8)
INC 161 , CPE 100 Computer Programming
Strings A collection of characters taken as a set:
Programming Funamental slides
String What it is Why it’s useful
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
elementary programming
QUIZ.
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Computer Security Password Policy.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CS31 Discussion 1D Winter19: week 4
Presentation transcript:

by: Muhammad Zidny Naf’an; 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.

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

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

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

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 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’

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.