Algo 10

Pertemuan 14 Januari 2016

Pertemuan terakhir sebelum kuis

Contoh coding dari:

http://a-l-g-o-r-i-t-m-a.blogspot.co.id/

Coding(toko baju):

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

struct Baju{
char kodeBaju[7];
char namaBaju[100];
int qty;
int hargaSatuan;
};

int initProgram(Baju baju[]){
FILE *file = fopen(“data.txt”, “r”);
int index = 0;
char tempHarga[100], tempQty[100];
while (!feof(file)){
fscanf(file, “%[^;];%[^;];%[^;];%[^\n]\n”, baju[index].kodeBaju, baju[index].namaBaju, tempQty, tempHarga);
baju[index].qty = atoi(tempQty);
baju[index].hargaSatuan = atoi(tempHarga);
index++;
}
fclose(file);
return index;
}

void generateTable(Baju baju[], int totalItem){
printf(“%-4s%-12s%-20s%-5s%-15s\n”, “No.”, “Code”, “Name”, “Qty”, “Price”);
printf(“============================================================\n”);
for (int i = 0; i < totalItem; i++)
printf(“%-4d%-12s%-20s%-5d%-15d\n”, (i + 1), baju[i].kodeBaju, baju[i].namaBaju, baju[i].qty, baju[i].hargaSatuan);
if(totalItem == 0) printf(“There is no data!”);
printf(“\n\n\n”);
}

char *generateCode(Baju baju[], int totalItem){
static char kode[10], kodeReturn[10];
int count;
strcpy(kode, baju[totalItem – 1].kodeBaju);
kode[0] = ‘0’; kode[1] = ‘0’;
count = atoi(kode) + 1;
if (count < 10) strcpy(kodeReturn, “JF00”);
else if (count < 100) strcpy(kodeReturn, “JF0”);
else if (count < 1000) strcpy(kodeReturn, “JF”);
itoa(count, kode, 10);
strcat(kodeReturn, kode);
return kodeReturn;
}

int checkCode(char key[], Baju baju[], int totalItem){
int left = 0, right = totalItem – 1, mid;
do{
mid = (left + right) / 2;
if (strcmp(baju[mid].kodeBaju, key) == 0) return mid;
else if (strcmp(baju[mid].kodeBaju, key) == -1) left = mid + 1;
else right = mid – 1;
} while (left<=right);
return -1;
}

void sort(Baju baju[], int left, int right, char by[], char option[]){
int i = left, j = right;
int pivotInt;
char pivotChar[100];
Baju tempBaju;
if (strcmp(by, “code”) == 0)strcpy(pivotChar, baju[(left + right) / 2].kodeBaju);
else if (strcmp(by, “name”) == 0)strcpy(pivotChar, baju[(left + right) / 2].namaBaju);
else if (strcmp(by, “qty”) == 0)pivotInt = baju[(left + right) / 2].qty;
else if (strcmp(by, “price”) == 0)pivotInt = baju[(left + right) / 2].hargaSatuan;
while (i <= j){
if (strcmp(by, “code”) == 0) { while (strcmp(baju[i].kodeBaju, pivotChar) < 0) i++; while (strcmp(baju[j].kodeBaju, pivotChar) > 0) j–; }
else if (strcmp(by, “name”) == 0) { while (strcmp(baju[i].namaBaju, pivotChar) < 0) i++; while (strcmp(baju[j].namaBaju, pivotChar) > 0) j–; }
else if (strcmp(by, “qty”) == 0) { while (baju[i].qty < pivotInt) i++; while (baju[j].qty > pivotInt) j–; }
else if (strcmp(by, “price”) == 0) { while (baju[i].hargaSatuan < pivotInt) i++; while (baju[j].hargaSatuan > pivotInt) j–; }
if (i <= j){
tempBaju = baju[i];
baju[i] = baju[j];
baju[j] = tempBaju;
i++; j–;
}
}
if (left < j) sort(baju, left, j, by, option);
if (i < right) sort(baju, i, right, by, option);
}

void saveData(Baju baju[], int totalItem){
FILE *file = fopen(“data.txt”, “w”);
for (int i = 0; i < totalItem; i++)
fprintf(file, “%s;%s;%d;%d\n”, baju[i].kodeBaju, baju[i].namaBaju, baju[i].qty, baju[i].hargaSatuan);
fclose(file);
}

int main(){
Baju baju[100];
int totalItem = initProgram(baju);
int choose, avail;
char tempCode[10], tempBy[10], tempSrt[10];
do{
system(“cls”);
printf(“\t\t\tJackForce Store\n\n”);
generateTable(baju, totalItem);
printf(“Menu\n”);
printf(“====\n”);
printf(“1. Add Item\n”);
printf(“2. Remove Item\n”);
printf(“3. Sort Item\n”);
printf(“4. Exit\n”);
printf(“\nInput menu : “);
scanf(“%d”, &choose); fflush(stdin);
switch (choose){
case 1:
sort(baju, 0, (totalItem-1), “code”, “asc”);
do{
printf(“Input your item’s name [3..20] : “);
gets(baju[totalItem].namaBaju);
} while (strlen(baju[totalItem].namaBaju)<3 || strlen(baju[totalItem].namaBaju)>20);
do{
printf(“Input your item’s qty [1..1000] : “);
scanf(“%d”, &baju[totalItem].qty); fflush(stdin);
} while (baju[totalItem].qty<1 || baju[totalItem].qty>1000);
do{
printf(“Input your item’s price [10000..1000000] : “);
scanf(“%d”, &baju[totalItem].hargaSatuan); fflush(stdin);
} while (baju[totalItem].hargaSatuan<10000 || baju[totalItem].hargaSatuan>1000000);
strcpy(baju[totalItem].kodeBaju, generateCode(baju, totalItem));
totalItem++;
printf(“Success insert the new item!”);
getchar();
break;
case 2:
avail = -99;
do{
printf(“Input your item’s code [input ‘cancel’ to cancel] : “);
scanf(“%s”, tempCode); fflush(stdin);
if (strcmp(tempCode, “cancel”) != 0 && (avail = checkCode(tempCode, baju, totalItem)) == -1){
printf(“Can’t find the item that you mean.. Please check again the code.\n”);
getchar();
}
} while (strcmp(tempCode, “cancel”) != 0 && avail == -1);
if (avail != -99){
for (int i = avail + 1; i >= 1 && i < totalItem; i++){
baju[i-1] = baju[i];
}
totalItem -= 1;
printf(“Success delete an item!”);
getchar();
}
break;
case 3:
do{
printf(“Sort by [code/name/qty/price] : “);
scanf(“%s”, tempBy);
} while (strcmp(tempBy, “code”) != 0 && strcmp(tempBy, “name”) != 0 && strcmp(tempBy, “qty”) != 0
&& strcmp(tempBy, “price”) != 0);
sort(baju, 0, (totalItem-1), tempBy, “asc”);
printf(“The data has been sorted!”);
getchar();
break;
}
} while (choose != 4);
saveData(baju, totalItem);
printf(“Exit from the System.. Press Any Key to Continue…”);
getchar();
return 0;
}

Algoritma 9

Pertemuan 7 Januari 2016

Sorting

-Bubble Sort:Data ditukar sampai data beratur(Ascending atau descending).

-Selection Sort:Memastikan data kecil dan data besar.

-Insertion sort:Algoritna yang efisien dalam mengurutkan angka yang memiliki elemen sedikit.

-Quick Sort

-Merge Sort:Sort yang duilakukan dengan menggunakan teknik menggabungkan 2 buah array kedalam sebuah array yang baru

 

Searching

-Linear Search

-Binary Search

-Interpolation Search

 

Algoritma 8

pertemuan 17 Desember 2015

File Processing

File definiton:-file is a collection of report

-Record is a collection of field

-Field is a block of byte

-Byte is a collection of bit

stdin:standard input stream

stdout:standard output stream

stderr:standard error stream

Text file saved in text format or ASCII File

Open file using fopen():

File*fopen(const char *filename,const char *mode);

Closing.fileusingfclose():

int fclose(fILE*stream);

 

Algoritma 7

Pertemuan tgl 3 Desember 2015

Modular Programming

advantage:

*Top-down designed with sub-goal

*Can be done by more than one programmer

*Easier to debug

-sub-program                  -sub-program

Main Program -sub-program

-sub program                   -sub-program

Best practice:

-High fan-in(frequently used)

-Low fan-out(more specific functionality)

-Self contained

Function in C is divided in 2 types:Library function and user defined

Function prototype:

-To ensure a function is known by the initiator/caller

-Compiler will validate the parameter

Identifier scoping

-Local identifier:Inside function

-Global Identifier:Outside function

Passing Parameter

-By-value:Sent to other is the value

-By location/By Reference:Sent to other module in the adress

Recursive defenition:Calls inside a certain function calling itself

Recursive function

-Base case:Return value (constant) without calling next reculsive call

-Reduction step:Sequence of input value converging to base case

 

Algoritma 6

TGL 12 Novermber 2015

Input>Proses>Output

>Selection

>Repetition

>Storage>file>sql>nosql

 

Input:Proses yang dimasukan user kedalam komputer

Proses: Tahap pemrosesan hasil Inputan user ke dalam komputer

Output:Hasil dari proses

Contoh coding

segitiga tanpa isi:

#include<stdio.h>

int main()

{

int n,a,b;

scanf(“%d”, &n); fflush(stdin);

for(a=1;a<=n;a++){

for(b=1;b<=a;b++){

if(b==1||a==b||a==n){

printf(“*”);

}

else{

printf(” “);

}

}

printf(“\n”);

}

getchar();

return 0;

}

Coding untuk bilangan prima

#include<stdio.h>
int main()
{

int a,b,i,c=0;

scanf(“%d”,&i);
fflush(stdin);
for(a=2;a<i+1;a++){
for(b=1;b<=a;b++)
{if(a%b==0){c++;}}

if (c==2)
{printf(“%d “,a);}c=0;}
getchar();
return 0;
}

Segitiga pascal

#include<stdio.h>
int main(){

int input;
int kolom,baris,segitiga[100][100];
scanf(“%d”,&input);
fflush(stdin);
for (baris=0;baris<input;baris++)
{
for (kolom=0;kolom<=baris;kolom++)
{
if(kolom==0 || kolom==baris)
{
segitiga[baris][kolom]=1;
}
else if(baris>1 && kolom<baris)
{
segitiga[baris][kolom]=segitiga[baris-1][kolom]+segitiga[baris-1][kolom-1];
}
printf(“%d “,segitiga[baris][kolom]);
}

printf(“\n”);
}

return 0;
}

 

Algoritma 5

Pointers & Array (29 Oktober 2015)

Defenition:Pointer is a variable that states the adress of another variable.

2 operators mostly used in pointer:*(content of) and & (adress of)

Example:int i,*ptr;

ptr=&i;

(To assign a new value pointed by the pointer:*ptr=5;/* means i=5*/)

*=konten variabel lain

tanpa *=menyimpan nilai variabel itu tapi harus menggunakan variabel lain

Pointer to pointer:A variable that saver another adress of a pointer.

Array definition

Data saved i a certain structure to be accesed as a group or individually.Some variable saved using the same name distinguish by their index.

Characteristic:

-Homogenous(All elements have similar data type)

-Random Access(Each element can be reached individualy,does not have to be sequential)

Array Initialization

Array can be initialized explicitly without dimensional value declaration.

Accsesing Array

-Two analougous ways of accesing an element 1=2;

*(A+2) or A[2]

-A is equivalent with &A[0] or a constant pointer to the first element of particular array.

Pointer Constant & Pointer Variable

-Pointer Variable:a pointer that can be assigned with new value at run-time

-Pointer Constant:a pointer that cannot be assigned with new value at run-time

One dimensional array

-C compiler does not limit number of dimensional which can be created.Our PC memory does.

Two dimension array

Inialization:using rmo(row major order)

Array of pointer:array filled with pointer/s

Array of character:Array filled with character/s

String:An array of character that ended with null character(’10’ or m ASCII=0).

 

Tugas algoritma 4

Pertemuan 22 Oktober 2015

Repetition

-Repetition is One or more instruction that is repeated for a certain amount of time.

-Number of repetition can be predefined(hard-coded) or defined at run-time.

-Operation:

  • for
  • while
  • do-while

Repetition:FOR

for{exp1;exp2;exp3}statement;

Infinite loop:Stop with break.

Nested loop:Loop within a loop.

Repetition While

Hanya berjalan bila statement true.

Repetition:Do-While:

Melakukan proses dulu lalu memeriksa statement.

Repetition Operation:

Break untuk memaksa menutup proses.

Tugas Algoritma 3

Presentasi 15 Oktober

Selection Defenition:An instruction or block of instructions may be executed (or not) with certain predetermined condition in a algorithm implementation

Selection If:

If boolean expression resulting in True, then a statement or some statements will be executed.

Selection If Else:

If boolean expression resulting in TRUE, then statement1 or  block statement1 will be executed, if FALSE then statement2 or block statement2 be executed

Selection Nested If:Appears if the word IF appears more than once during an IF statement.

Switch Case:Used in exchange of IF-ELSE,when if-else nested number of level is dificult to read.

The operator ?:Simmilar to the IF statement,but returns a value condition ? then- expression:else-expression

Error Type:

  • Compile-Time error
  • Link-Time error
  • Run-Time error
  • Logical error

Tugas Algoritma 2

Dari pertemuan 9 Oktober 2015

Operator:Symbol to process value result for a new value

-Binary operator:2

-Tenary operator:3

-Unary operator:1

Operator Type

  • Assigment operator
  • Logical operator
  • Arithmatic operator
  • Relational operator
  • Bitwise operator
  • Pointer operator

Assigment Operator

-Binary operator

-Used to assign value in a operation

-Syntax: Operand 1(L-value/variable)=Operand 2(constant)

Example:

  • x=2;  //constant
  • x=y;   //other variable
  • x=2*y   //expression
  • x=sin(y);   //function

Arithmetic Operator

Example:

N++;   Post increment

++N;   Pre increment

(equal to N=N+1 if standalone)

N–;   Post decrement

–N;   Pre decrement

(equal N=N-1 if standalone)

if bound sstatemenr(sub expression) then both of them have a diferent meaning

++n > n add by 1 then continue process

Arithmatic Operator

Relational Operator

++ : equality

!= : not equal

< : less than

> : greater than

<= : less equal than

>= : more equal than

Continoual Expression

Statement:if (a>b) z=a;

else z=b

Logical Operator

GG     AND

||     OR

!     NOT

Bitwise Operator

&     AND     A&B;

|     OR     A|B;

^     XOR     A^B;

~     Complement     ~A;

>>     Shift Rigth     A>>3;

<<     Shift Left     B<<2

 

 

 

Tugas Algoritma

1 Oktober 2015

Object Oriented Programing (OOP):

  • Inheritance
  • Abstraction
  • Interface
  • Polymorphism

Tingkat Bahasa Pemograman dari rendah sampai tinggi:

Assembler>C,Pascal,Fortran>Java,C++,C#

Problem>Process>Solution

Develop an algorithm:

  1. Writing
  2. Flowchart

Pseudocode:

  1. Input
  2. Output
  3. Compute
  4. Store
  5. Compiler
  6. Loop

Why C is popular:

  1. Flexibility
  2. Portable
  3. Well known programming langguage
  4.  Library Support

C standard library:

  • <math.h>
  • <stdio.h>
  • <stdlih.h>
  • <string.h>