Page 90 - MODUL ALGORTIMA DAN PEMROGRAMAN
P. 90

KODE PROGRAM BAHASA C
                  #include <stdio.h>

                  typedef union daftar_nilai {
                      float pengetahuan;
                      float sikap;
                      float psikomotorik;
                  } WILLY;

                  int main() {
                      WILLY willy;

                      willy.sikap = 80.5f; // Assigning a value to the 'sikap' member
                      willy.pengetahuan = 87.5f; // Assigning a value to the 'pengetahuan'
                  member

                      printf("Nilai pengetahuan: %f\n", willy.pengetahuan); // Printing the
                  'pengetahuan' value
                      printf("Nilai sikap: %f\n", willy.sikap); // Printing the 'sikap'
                  value
                      printf("Nilai psikomotorik: %f\n", willy.psikomotorik); // Printing
                  the 'psikomotorik' value (which is still 0 since it hasn't been assigned)

                      printf("Ukuran objek: %d\n", sizeof(willy)); // Printing the size of
                  the union object

                      return 0;
                  }

                                               KODE PROGRAM BAHASA C++
                  #include <iostream>
                  using namespace std;

                  union daftar_nilai {
                         float pengetahuan;
                         float sikap;
                         float psikomotorik;
                  }willy;

                  int main(){
                         willy.sikap = 80.5f;
                         willy.pengetahuan = 87.5f;
                         cout<<"nilai pengetahuan : "<<willy.pengetahuan<<endl;
                         cout<<"nilai sikap : "<<willy.sikap<<endl;
                         cout<<"nilai psikomotorik : "<<willy.psikomotorik<<endl;
                         cout<<"ukuran objek : "<<sizeof(willy)<<endl;
                         return 0;
                  }

                  Output:

                  nilai pengetahuan : 87.5
                  nilai sikap : 87.5

                  nilai psikomotorik : 87.5
                  ukuran objek : 4




                                                                                                          67
   85   86   87   88   89   90   91   92   93   94   95