Diktat_ProgKecilBhsC

# C latihan array dinamik ukuran tabel ditentukan dari

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ke struct */ printf (&quot;Titik P2, dengan P2-&gt;x dan P2-&gt;y :\n&quot;); P2 = (Point *) malloc (sizeof (Point)); /* Alokasi */ P2-&gt;x = 9.12; /* Isi nilai komponen */ P2-&gt;y = 2.567; printf (&quot;P2.x = %f \nP2.y = %f\n&quot;, P2-&gt;x, P2-&gt;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 &lt;stdlib.h&gt; /* Definisi akses komponen type, standard kuliah Algoritma &amp; Pemrograman */ #define info(P) (P)-&gt;info #define next(P) (P)-&gt;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 (&quot;%d\n&quot;, 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 (&quot;Isi dan print tabel untuk indeks 1..5 \n&quot;); /* isi dengan assignment */ for (i = 1; i &lt;= N; i++) { tab[i] = i; }; /* traversal: print */ for (i = 1; i &lt;= N; i++) { printf (&quot;i=%d tab[i]=%d \n&quot;, i, tab[i]); }; return 0; } /* File : tabdin.c */ /* mendeklarasi dan alokasi variabel dinamik */ #include &lt;stdlib.h&gt; 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