Diktat_ProgKecilBhsC

C latihan array dinamik ukuran tabel ditentukan dari

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ke struct */ printf ("Titik P2, dengan P2->x dan P2->y :\n"); P2 = (Point *) malloc (sizeof (Point)); /* Alokasi */ P2->x = 9.12; /* Isi nilai komponen */ P2->y = 2.567; printf ("P2.x = %f \nP2.y = %f\n", P2->x, P2->y); /* Cara kedua : Perhatikanlah tanda kurung, lihat prioritas () dibdk . */ (*P3).x = 0.5; /* Mendefinisikan isi */ (*P3).x = 10.5; /* Mendefinisikan isi */ return 0; } /* File : list1.c */ /* contoh deklarasi list dan pemakaian makro */ #include <stdlib.h> /* Definisi akses komponen type, standard kuliah Algoritma & Pemrograman */ #define info(P) (P)->info #define next(P) (P)->next #define Nil NULL /** Definisi TYPE Global (sebenarnya utk soal ini tidak perlu global */ /* Elemen list linier */ typedef int infotype; typedef struct tElmtlist *address; typedef struct tElmtlist { infotype info; address next; } ElmtList; /*** PROGRAM UTAMA */ int main () {/* Kamus */ address First; address P, Q; /* Create list kosong */ First = Nil; /* alokasi, insert as first element */ P = (address) malloc (sizeof (ElmtList)); info (P) = 10; next (P) = Nil; First = P; /* alokasi, insert as first element */ Q = (address) malloc (sizeof (ElmtList)); info (Q) = 20; next (Q) = Nil; next (Q) = First; First = Q; /* alokasi, insert as first element */ P = (address) malloc (sizeof (ElmtList)); info (P) = 30; next (P) = Nil; next (P) = First; First = P; printf ("%d\n", info (next (next (First)))); return 0; } ARRAY /* File : tabel.c */ /* latihan array statis : mengisi dg assignment, menulis */ int main () { /* Kamus */ int i; int tab[10]; /* Cara mengacu elemen ke-i: tab[i] */ int N; /* Program */ N = 5; printf ("Isi dan print tabel untuk indeks 1..5 \n"); /* isi dengan assignment */ for (i = 1; i <= N; i++) { tab[i] = i; }; /* traversal: print */ for (i = 1; i <= N; i++) { printf ("i=%d tab[i]=%d \n", i, tab[i]); }; return 0; } /* File : tabdin.c */ /* mendeklarasi dan alokasi variabel dinamik */ #include <stdlib.h> int main () { /* KAMUS */ int i; int *tab = (int *) malloc (10 * sizeof (int)); /* deklarasi, alokasi */ /* Cara mengacu elemen ke-i: tab[i], atau *(tab...
View Full Document

Ask a homework question - tutors are online