[su_expand more_text=”Show code” less_text=”Hide code” height=”30″]1) Atur cara 8.1
[/su_expand]
#include <stdio.h>
struct nombor{
int n1;
double n2;
float n3;
};
int main() {
struct nombor nom = {4, 6.5, 15.0};
nom.n2 += 5;
nom.n3 = nom.n2 - nom.n1;
nom.n1 = 8;
printf("%0.2f %d %0.2f", nom.n2 ,nom.n1, nom.n3);
return 0;
}
[su_expand more_text=”Show code” less_text=”Hide code” height=”30″]2) Atur cara 8.2
[/su_expand]
#include <stdio.h>
typedef struct {
char fakulti[50];
char universiti[50];
char singkatan[10];
int poskod;
char bandar[30];
char negeri[30];
} Alamat;
int main(){
Alamat utmFC = {"Fakulti Komputeran",
"Universiti Teknologi Malaysia", "UTM", 81310,
"Skudai", "Johor"};
Alamat fakultiKomputer[20] = {
{"Fakulti Teknologi & Sains Maklumat",
"Universiti Kebangsaan Malaysia", "UKM", 43600,
"Bangi", "Selangor"},
{"Pusat Pengajian Sains Komputer",
"Universiti Sains Malaysia", "USM", 11800,
"\0", "Pulau Pinang"},
{"Fakulti Komputer & Sains Matematik",
"Universiti Teknologi Mara", "UiTM", 40450,
"Shah Alam", "Selangor"}
};
int j, bil=3;
fakultiKomputer[bil++] = utmFC;
for (j=0; j<bil; j++){
printf("\n%s\n", fakultiKomputer[j].fakulti);
printf("%s\n", fakultiKomputer[j].universiti);
printf("%d ", fakultiKomputer[j].poskod);
printf("%s \n", fakultiKomputer[j].singkatan);
printf("%s, ", fakultiKomputer[j].bandar);
printf("%s\n",fakultiKomputer[j].negeri);
}
return 0;
}
[su_expand more_text=”Show code” less_text=”Hide code” height=”30″]3) Atur cara 8.3
[/su_expand]
#include <stdio.h>
#include<process.h>
struct rekod_pelajar {
int no_id;
float tugasan;
float kuiz;
float ujian;
char gred;
};
void masuk_data(struct rekod_pelajar *p);
float kira_jumlah(struct rekod_pelajar p);
void tentu_gred(float jum_mar, char *gred);
void cetak(char gred, int id);
int main(){
struct rekod_pelajar amir,amirNaim[2];
float jum_mar;
int i;
masuk_data(&amir);
jum_mar=kira_jumlah(amir);
tentu_gred(jum_mar, &amir.gred);
cetak(amir.gred, amir.no_id);
return 0;
}
void masuk_data(struct rekod_pelajar *p){
printf("Masukkan ID pelajar: ");
scanf("%d", &(*p).no_id);
printf("Masukkan markah tugasan dan kuiz: ");
scanf("%f %f",&p->tugasan, &p->kuiz);
printf("Masukkan markah ujian: ");
scanf("%f", &p->ujian);
}
float kira_jumlah(struct rekod_pelajar p){
float jum;
jum = p.tugasan + p.kuiz + p.ujian;
return jum;
}
void tentu_gred(float jum_mar, char *gred){
if (jum_mar >= 60)
*gred = 'L';
else
*gred = 'G';
}
void cetak(char gred, int id){
printf("Gred pelajar %d ialah %c\n", id, gred);
}
[su_expand more_text=”Show code” less_text=”Hide code” height=”30″]4) Atur cara 8.4
[/su_expand]
#include <stdio.h>
union nombor {
char x;
int y;
};
int main( ){
union nombor nilai;
printf("saiz union ialah %d\n\n", sizeof(nilai));
nilai.x = 0x41; /*ASCII 'A'*/
printf("char: %X \nint: %X\n", nilai.x, nilai.y);
printf("alamat bermula pada %d\n\n", &nilai.x);
nilai.y = 0x1234;
printf("char: %X \nint: %X\n", nilai.x, nilai.y);
printf("alamat bermula pada %d\n", &nilai.y);
return 0;
}
[su_expand more_text=”Show code” less_text=”Hide code” height=”30″]5) Atur cara 8.5
[/su_expand]
#include <stdio.h>
enum duit
{
sen5 = 5, sen10 = 10, sen20 = 20, sen50 = sen5*sen10, rm1 = 2*sen50, rm5 = 5*rm1
} duit_masuk;
int main( ){
int jumlah = 0;
printf("**Mesin Air Minuman**\n");
while (jumlah<160) {
printf("Masukkan duit dalam nilai sen: ");
scanf("%d", &duit_masuk);
switch(duit_masuk) {
case sen5:
jumlah+=sen5; break;
case sen10:
jumlah+=sen10; break;
case sen20:
jumlah+=sen20; break;
case sen50:
jumlah+=sen50; break;
case rm1:
jumlah+=rm1; break;
case rm5:
jumlah+=rm5; break;
default:
printf("Duit yang diterima hanya Syiling: ");
printf("5 10 20 50 dan Wang kertas RM1 RM5\n");
}
printf("Jumlah Duit masuk: %d\n", jumlah);
}
if (jumlah>0)
printf("Duit baki %dsen\n", jumlah-160);
printf("**Terima Kasih**");
return 0;
}