Category Archives: Algorithm & Data Structure [C & PASCAL]

Algorithm & programming part VII (Data Structure)

This is 7th part of our meeting. In this meeting we’re gonna talk about pointer. But, before we go to the point , we have to know first about static and dynamic variable. As we know before , we only use static variable to save the data and it can’t be changed when the program is executed. So, static variable is a variable which is the size and sequence can’t be changed when the program is executed. And it is always shown by identifier. On the other hand, Dynamic variable is more flexible, because we can change it’s size and sequence even though the program is executed. And especially for dynamic variable, it’s always shown by pointer.

Pointer is very important for managing memory in a big program. A programmer won’t be a great one if he/ she still doesn’t master pointer first. Because it’s a basic in a structure of a big program.

On the other side, if we wrong to use pointer, it’s gonna mash up our program. It’s usually called access violation.

In this part and later, I am not gonna make sample from Pascal programming language. Because after this i’d like to focus in C programming language. Alright, let’s declare the pointer now.

For example I wanna declare a pointer In integer type.

Int *p;

it means that I declare a dynamic variable (pointer) with type integer. After this, in main program, we have to order an allocation of memory for the pointer. This Is the way

p=(int*)malloc(sizeof (int));

it means that we have ordered an area as large as integer (2 byte) and we save the address in pointer p

oh…ya I almost forget that malloc function is only exist in stdlib header file so we have to declare it first in the beginning of the program.

Here is the example pointer to integer


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

int main(){
int a,*p;

printf("Program Pointer Ke Integer\n");
p=(int*)malloc(sizeof(int));
puts("Masukkan sebuah angka ke variabel A = ");scanf("%d",&a);
p=&a;
printf("Hasil peng-Copy-an data dari variabel A ke pointer P adalah = %d\n",*p);
return (0);
}

Here is the example pointer to struct

 

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

typedef struct data{
char lokasi[20];
int luas;
}data;

typedef struct data node;
node *p;

int main(){

system ("clear");
p=(node*)malloc(sizeof(node));
printf("Masukkan nama lokasi sebuah tempat = ");scanf("%s",p->lokasi);
printf("Masukkan luas lokasi tempat tersebut= ");scanf("%d",&p->luas);
printf("Sebelum pointer P dibuat NULL\n");
printf("Nama lokasi tersebut adalah = %s\n",p->lokasi);
printf("Nama luas tersebut adalah = %d\n",p->luas);
free(p);
printf("Sesudah pointer P dibuat NULL\n");
printf("Nama lokasi tersebut adalah = %s\n",p->lokasi);
printf("Nama luas tersebut adalah = %d\n",p->luas);
return (0);
}

 

Advertisements

Algorithm & programming part VI (Data Structure)

This is part 6th of our meeting. Part 6th will focus on struct (in C ) or record (in pascal). But, before talking about that we have to talk about main (real) type and defined type. Main type is standard types which has been prepared by certain programming languages. Defined type is combination of certain types which is made by programmer by using certain programming language. Therefore, we can conclude that every programmer can modify their own type for certain variables in a program.

Alhamdulillah, now we have known about main and defined type. All of that has important effect in making record/ struct. Let’s go to record. Actually, record is a group of elements from array. By using record , we can make a variable with combination of different types. However, there are differences between C and PASCAL about record. In C we call struct in PASCAL we call record. But, they both are same in function.

Below is examples :

C


#include <stdio.h>

void input();
void appearance();
 struct{
 char nama[20];
 int nrp;
 }mahasiswa[20];

//typedef struct mahasiswa data;
//data profil[20];
int a;
int main()
{

 input();
 appearance();

}

void input(void){

int b;

printf("Masukkan jumlah data total = ");scanf("%d",&a);
 for (b=1;b<=a;b++){
 printf("Masukkan nama mahasiswa ke-%d = ",b);scanf("%s",mahasiswa.nama);
 printf("Masukkan nrp Mahasiswa ke-%d  = ",b);scanf("%d",&mahasiswa.nrp);
 }
}

void appearance(void){
int a,b;
puts("=========================================================================\n");
puts("|No  |        Name            |        Nrp        |\n");
puts("=========================================================================\n");
 for (b=1;b<=a;b++){
 printf("  %d\t\t  %s\t\t\t\t  %d\t\t\t\n",b,mahasiswa->nama,mahasiswa->nrp);
 }
puts("=========================================================================\n");
}

Oh ya…i forget that inside C programming language also there is something same like struct but, in some case it is different in it’s name and function also in it’s use. It is Called union. Union is not only work in single structured variable but also work in multi structured variable. But, in this case, I don’t want to give you example about that.

Algorithm & programming part V (Data Structure)

Huh….guys, In this meeting i’m gonna share with you about a little bit of my experience. Some hours ago, i’ve typed this articles until finish. Then my friend invite me to go to campus. A few minutes later, I resume my computer and reopen my articles. You know what happen ??? the data is dissapear, so that i’ve to write type it again. Hmm…. that’s unpleasure experience.

Actually 2 days ago the holiday has been begun for my major, but unfortunatelly I still have so many kinds of activities such as regional english seminar. Alhamdulillah the seminar run well. Guys , after the seminar attended, I plan to go home immediately but the leader of my organization said to me that “Bro , don’t go home, we’ll go to Pare”. Wow Kediri pare, Directly I know the purpose which is besides enjoying holiday, we’re gonna take english course . So, if you’re interested to join with us,you can contact me through facebook,okay…

Well, actually now we were in Data Structure topic. Data Structure is a fundamental item for a programmer to master. In data Structure we have so many topics . Below is the list :

  1. array
  2. Struct/ record
  3. Union (only in C)
  4. Pointer and static Variable
  5. stack
  6. Queue
  7. linked List
  8. Sorting
  9. Searching
  10. Tree
  11. Graph

That’s all the subtopics for data structure. The first we’re gonna talk about Array. Array is a finite ordered set of homogeneous elements. It means that , Array contains of a group of elements which has same type and. The amount of elements are determined and arranged. Array has 3 types : 1st is one dimension array, 2nd is two dimension array , 3rd is three dimension array. All of them have their own function. The 1st type is usually used for displaying common data. The 2nd type is usually used for displaying matrix and point location. The last type is usually used for displaying point location of 3 dimension object.

Below is examples for each types :

example program array 1 dimension :

PASCAL

program array_1_dimension;
uses crt;
var
a:array [1..10]of integer;
b,c:integer;
begin
write('input a number = ');read(b);
{input data}
 for c:=1 to b do
 begin
 write('index[',c,']=');read(a[c]);
 end;
{display data}
 for c:=1 to b do
 begin
 writeln(a[c]);
 end;
end.

C

#include<stdio.h>

int main(){
int c[10];
int a,b;
 printf("input a number = ");scanf("%d",&a);
 //input
 for (b=1; b<=a;b++){
 printf("index[%d] = ",b);scanf("%d",&c[b]);
 }
 //display
 for (b=1; b<=a;b++){
 printf("%d\n",c[b]);
 }
}

example array 2 dimension (Matrix) :

PASCAL

program array_2_dimension(matrix);
uses crt;
var
mat:array [1..10,1..10]of integer;
a,b,row,coloumn:integer;
begin
write('input a row = ');read(row);
write('input a coloumn = ');read(coloumn);
{input data}
 for a:=1 to row do
 begin
 for b:=1 to coloumn do
 begin
 write('index[',a,',',b,']=');read(mat[a,b]);
 end;
 end;
{display data}
 for a:=1 to row do
 begin
 for b:=1 to coloumn do
 begin
 write(mat[a,b]:3);
 end;
 writeln;
 end;
end.


C

#include<stdio.h>

int main(){
int mat[10][10],a,b,row,coloumn;
 printf("please input a row = ");scanf("%d",&row);
 printf("please input a coloumn = ");scanf("%d",&coloumn);
 for (a=1;a<=row;a++)    {
 for (b=1;b<=coloumn;b++){
 printf("index[%d,%d]=",a,b);scanf("%d",&mat[a][b]);
 }
 }
 for (a=1;a<=row;a++)    {
 for (b=1;b<=coloumn;b++){
 printf("%d  ",mat[a][b]);
 }
 printf("\n");
 }
}

example for 3 dimensions array :

PASCAL

program array_3_dimension(matrix);
uses crt;
var
mat:array [1..10,1..10,1..10]of integer;
a,b,c,x,y,z:integer;
begin
write('input x coordinate = ');read(x);
write('input y coordinate= ');read(y);
write('input z coordinate= ');read(z);
{input data}
 for a:=1 to x do
 begin
 for b:=1 to y do
 begin
 for c:=1 to z do
 begin
 write('index[',a,',',b,',',c,']=');read(mat[a,b,c]);
 end;
 end;
 end;
{display data}
 for a:=1 to x do
 begin
 for b:=1 to y do
 begin
 for c:=1 to z do
 begin
 write(mat[a,b,c]:3);
 end;
 writeln;
 end;
 writeln;
 end;
 writeln;
end.


C

#include<stdio.h>

int main(){
int mat[10][10][10],a,b,c,x,y,z;
 printf("please input x coordinate = ");scanf("%d",&x);
 printf("please input y coordinate = ");scanf("%d",&y);
 printf("please input z coordinate = ");scanf("%d",&z);
 for (a=1;a<=x;a++){
 for (b=1;b<=y;b++){
 for (c=1;c<=z;c++){
 printf("index[%d,%d,%d]=",a,b,c);scanf("%d",&mat[a][b][c]);
 }
 }
 }

 for (a=1;a<=x;a++){
 for (b=1;b<=y;b++){
 for (c=1;c<=z;c++){
 printf("%d  ",mat[a][b][c]);
 }
 printf("\n");
 }
 printf("\n");
 }
}


Algorithm & programming part IV

Hhmm……., in this part we’re gonna talk about procedure and function. Procedure and function is used to make program can be easier to be read and understood. Not only that, procedure and function can be used to make program easier to be analyzed weather there is mistake or not. So, when there is long codes of program, it can be separated into small parts as procedure or function. And, procedure and function will make you easy to find out error and fix it (error handling). Well, actually there are differences between procedure and function. Procedure doesn’t has return value. However, function has it. Not only that, pascal has procedure and function. But, C doesn’t have it . On the other side, function in C able to creat return value or not. Depend on the type of function. Pascal has parameter for procedure and function and C is either.

Below is some examples the use of function and procedure by parameter or not

example program without parameter :

PASCAL

program table;
uses crt;

procedure appearance;
begin
writeln('this program is made for procedure without parameter');
writeln('this program only contains of appearance and main program');
writeln('this program will use two procedures');
writeln('this 1st procedure for apperance ');
writeln('the 2nd procedure for main program ');
writeln('start ');
writeln('press ENTER ');
readln;
end;

procedure main;
var
a:byte;
begin
for a:=1 to 10 do
writeln('ACHMAD FAUZI');
end;

var
a:byte;
begin
appearance;
main;
end.

C

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

void appearance();
void primary();

int main(){
appearance();
primary();
}
void appearance(){
printf("this program is made for procedure without parameter\n");
printf("this program only contains of appearance and main program\n");
printf("this program will use two procedures\n");
printf("this 1st procedure for apperance \n");
printf("the 2nd procedure for main program\n");
printf("start\n");
printf("press ENTER \n");
getchar();
}
void primary(){
int a;
for (a=1;a<=10;a++)
printf("ACHMAD FAUZI\n");
}

example program by using input parameter :

Pascal

PASCAL
program sum;
uses crt;
var
a:integer;

procedure add(x:integer);
var
b,c,d:integer;
begin
d:=0;
 for b:=1 to x do
 begin
 write('index[',b,']=');read(c);
 d:=d+c;
 end;
writeln('total = ',d);
end;

begin
write('please input a number =');read(a);
add(a);
end.

C

#include<stdio.h>

void out();
int main(){
out();
}

void out(){
int a,b,c,d;
printf("input a number = ");scanf("%d",&a);
d=0;
 for (b=1;b<=a;b++){
 printf("index[%d] :",b);scanf("%d",&c);
 d+=c;
 }
printf("total = %d\n",d);
};

example program by using output parameter :

PASCAL

program average_by_output_parameter;
uses crt;
var
b,c,d,e:integer;
a:real;
procedure ave(var a:real);
begin
e:=0;
write('input a number = ');read(b);
 for c:=1 to b do
 begin
 write('index[',c,']=');read(d);
 e:=e+d;
 end;
a:=e/b;
end;

begin
ave(a);
writeln('average = ',a:2:2);
end.


C

#include<stdio.h>

int out (int b,int d);


void main(){
int b,d;
float a;
a=out (b,d);
printf("average = %2.2f\n",a);
}


int out (int b,int d){
int c,e;
float a;
e=0;

printf("input a number  =");scanf("%d",&b);
 for (c=1;c<=b;c++){
 printf("index[%d] = ",c);scanf("%d",&d);
 e+=d;
 }
a=e/b;
return (a);
}


example program by using input and output parameter :

PASCAL

program average_by_output_and_output_parameter;
uses crt;
var
b:integer;
a:real;

procedure ave(b:integer ;var a:real);
var
c,d,e:integer;
begin
e:=0;
 for c:=1 to b do
 begin
 write('index[',c,']=');read(d);
 e:=e+d;
 end;
a:=e/b;
end;

begin
write('input a number = ');read(b);
ave(b,a);
writeln('average = ',a:2:2);
end.

C

#include <stdio.h>

int jalan(int a);

void main(){
int a;
float e;
printf("input a number = ");scanf("%d",&a);
e=jalan(a);
printf("average = %2.2f\n",e);
}

int jalan(int a){
int b,c,d;
float e;
d=0;
 for (b=1;b<=a;b++){
 printf("index[%d]",b);scanf("%d",&c);
 d+=c;
 }
e=d/a;
return (e);
} 10.c


Algorithm & programming part III

Alhamdulillah, in this part , I feel so glad because I’ve already build the appearance for my old weblog. In this part we’re gonna discuss about looping. Looping is a repeated statements based on centain condition which is determined by the programmer.Pascal and C have same models of looping. Below is models of looping in Pascal and C

Model Pascal C
1. For For [condition] to [condition] do…[action]…. For (condition;conditon;terms){…….[action]………..

}

2. While While [condition] dobegin

…..[action]…………..

end;

While (conditon){……[action]…………….

}

3. Do …while repeat….until Repeat[action]

until

[condition]

Do{

[action]

}

while (condition)

Looping used to make programmer think harder, because in creating a program, looping must be tried repeatedly, in order to get match value. Moreover nested loop. Nested loop means looping inside looping.Looping is separated into descending and ascending loop.

Example (program to count average value):

PASCAL

 

program average;
uses crt;
var
a,b,c,d:integer;
e:real;
begin
d:=0;
write('input sum of number :');read(a);
 for b:=1 to a do
 begin
 write('index[',b,']=');read(c);
 d:=d+c;
 end;
e:=d/a;
writeln('average = ',e:2:2);
end.

C

 

#include <stdio.h>

int main(){
int a,b,c,d;
float e;
d=0;
printf("input sum of a number = ");scanf("%d",&a);
 for (b=1;b<=a;b++){
 printf("index[%d]=",b);scanf("%d",&c);
 d+=c;
 }
e=d/a;
printf("average=%2.2f\n",e);
}

PASCAL

program average_by_while;
uses crt;
var
a,b,c,d:integer;
e:real;
begin
d:=0;
write('input sum of number :');read(a);
b:=1;
 while b<=a do
 begin
 write('index[',b,']=');read(c);
 d:=d+c;
 b:=b+1;
 end;

 e:=d/a;
 writeln('average = ',e:2:2);

end.

C

#include <stdio.h>

int main(){
int a,b,c,d;
float e;
d=0;
printf("input sum of a number = ");scanf("%d",&a);
b=1;
 while (b<=a){
 printf("index[%d]=",b);scanf("%d",&c);
 d+=c;
 b+=1;
 }

e=d/a;
printf("average=%2.2f\n",e);
}

PASCAL

program average_by_repeat;
uses crt;
var
a,b,c,d:integer;
e:real;
begin
d:=0;
write('input sum of number :');read(a);
b:=0;
 repeat
 begin
 write('index[',b,']=');read(c);
 d:=d+c;
 b:=b+1;
 end;
 until b=a;

e:=d/a;
writeln('average=',e:2:2);

end.

C

#include <stdio.h>

int main(){
int a,b,c,d;
float e;
d=0;
printf("input sum of a number = ");scanf("%d",&a);
b=1;
 do{
 printf("index[%d]=",b);scanf("%d",&c);
 d+=c;
 b+=1;
 }
 while(b<=a);

e=d/a;
printf("average=%2.2f\n",e);
}

Okay, that’s all example for models of looping in PASCAL and C . So, have fun with your algorithm…

Algorithm & programming part II

Alhamdulillah, we meet again in this 2nd meeting.

In this case, I;m gonna tell you about Branching. Branching is a way to make decision when it is true or false. You can determine what should be done by program when it’s true, and when it’s false.There are 3 types of branching in pascal and C

type pascal C
1 If [….condition…] then [statement…] If (…condition…) statement….
2 If [….condition…] thenbegin[statement…][statement…]

end;

If (…condition…){statement….statement….

}

3 Case [var] ofbegin1 :….;…..;

end;

Swith (var){case 1:….;……………..;

}

program to determine odd and even

pascal


program number;
uses crt;
var
a,b:integer;
begin
write('please input a number :');read(a);
b=a mod 2;
if b=0 then
writeln('the number is genap');
else
writeln('the number is ….');
end.

C

#include <stdio.h.>
int main(){
int a,b;
printf(“input a number :”);scanf(”%d”,&a);
b=a%2;
if (b==0)
printf(“the number you entered is genap\n”);
else
printf(“the number you entered is ganjil”);
}

program to determine maximum and minimum value

Pascal

program maxmin;
uses crt;
var
number1,number2,number3:integer;
begin

write('input 1st number :');read(number1);
write('input 2nd number :');read(number2);
write('input 3rd number :');read(number3);

if (number1>number2) and (number1>number3) then
 writeln('1st number is the maximum :',number1)
else if (number2>number1) and (number2>number3) then
 writeln('2nd number is the maximum :',number2)

else if (number3>number1) and (number3>number2) then
 writeln('3rd number is the maximum :',number3)
else
writeln('there is same number');
end.

C

#include <stdio.h>

int main(){
int number1,number2,number3;
 printf("number1 = ");scanf("%d",&number1);
 printf("number2 = ");scanf("%d",&number2);
 printf("number3 = ");scanf("%d",&number3);

 if (number1>number2 && number1>number3)
 printf("max=number1 :%i\n",number1);
 else if (number2>number1 && number2>number3)
 printf("max=number2 :%i\n",number2);
 else if (number3>number2 && number3>number1)
 printf("max=number3 :%i\n",number3);
 else
 printf("there is same number");
}

program to solve 3 or more conditions

Pascal

 

program month;
uses crt;
var
number:integer;
begin

write('input a number :');read(number);
case number of
1:writeln('january');
2:writeln('february');
3:writeln('march');
4:writeln('april');
5:writeln('mei');
6:writeln('june');
7:writeln('july');
8:writeln('august');
9:writeln('september');
10:writeln('october');
11:writeln('november');
12:writeln('december');
else
writeln('out of range');
end;

end.

C

#include <stdio.h>

int main(){
int number;
 printf("number = ");scanf("%d",&number);
 switch (number){
 case 1:printf("january");break;
 case 2:printf("february");break;
 case 3:printf("march");break;
 case 4:printf("april");break;
 case 5:printf("mei");break;
 case 6:printf("june");break;
 case 7:printf("july");break;
 case 8:printf("august");break;
 case 9:printf("september");break;
 case 10:printf("october");break;
 case 11:printf("november");break;
 case 12:printf("december");break;
 default :printf("out of range");break;
 }
 printf("\n");
}

Algorithm & programming part I

Hi, guys….

This is my first post since a year ago , I become member of wordpress. But, in this occasion, i’d like to share with you about anything not only about knowledge ,but also about anything that i’ve got from my study

Ok, now i’m gonna tell you about algorithm. Actually algorithm is a sequence commands of program. Algorithm is not only can be applied in Programming but also, it can be applied in our daily life. For example :Algoritm for making fried Tempe. 1st prepare the tempe, 2nd prepare the spicy, 3rd prepare equipments, 4th fry tempe , 5th service on plate. Algorithm can be describe in 3 ways. 1st by using descriptive explanation, 2nd by using flowchart, 3rd by using pseudocode (programming language). In the example above I use descriptive explanation.

Algorithm is a basic item for a programmer, if you want to be a great programmer, you must be the expert in algorithm, if you have already done it, you will be able and easy to move to another programming language(Syntax). It doesn’t matter what kind of programming language if you have master it. Anyway, before I tell you so many thing about programming, i’m gonna tell you first about program, programming and programmer. Program is sequence of command that is made by human to handle problematics in the world and solve it in computer. Programming is an activity to make program. Programmer is somebody who make program.

In this case , i’m gonna use pascal and C language programming to describe every part of my explanation andI use linux operating system to compile programs. You can use tpw(turbo pascal for windows) and C++ to compile programs in windows. It depens on you, which one do you want to choose. So, before we learn about that, we must know about pascal and C first.

Pascal and C is the 3rd generation of language programming. They both are include in high level language programming. It means that both of them mostly use same language like us (means : English). Both of them are CLI (command Line Interface) language programming. It means that both of them only use typed command. There is no graphic (IDE) when creating, moreover when running. So that , I warn you before you learn visual programming , you have to learn about command line programming first. Because it is main basic. And , don’t be bored when you face so many codes inside a program. That is our environment.

Pascal and C have same parts of their main syntax. 1st is header, 2nd declaration, 3rd main codes.

Header contains of library for syntax. Example :pascal → uses crt

C → #include <stdio.h>

Both of them have same meaning for input and output of a program.

Declaration contains of constants, variables, etc.

Main codes contain of your commands in program to solve a problematics.

Below is some diffences in declaring between pascal and C in type of variable :

Type Pascal C
Integer Integer int
string string char[]
Real Real
Float Float Float
Double Double Double

Well, I think it’s enough for our 1st sharing. Bye………..