Rata-rata dan Max ( Struct )
Program kali ini akan mencari rata-rata dari sebuah nilai dan mencari nilai terbesarnya.Berikut ini koding sebelum di tambahkan rata-rata dan max.
#include <stdio.h>
#define MAX 20
int main(void) {
typedef struct {
char nama[80];
char nim[80];
char kelas[80];
int nilai;
} mahasiswa;
mahasiswa mhs[MAX];
int i, jml;
char strbuff[80];
printf("jumlah data: ");
gets(strbuff);
jml = atoi(strbuff);
system("cls");
for(i = 0; i < jml; i++) {
printf("Data #%d\n", i + 1);
printf("Nama : ");
gets(mhs[i].nama);
printf("NIM : ");
gets(mhs[i].nim);
printf("Kelas: ");
gets(mhs[i].kelas);
printf("Nilai: ");
gets(strbuff);
mhs[i].nilai = atoi(strbuff);
system("cls");
}
for(i = 0; i < jml; i++) {
printf("Data #%d\n", i + 1);
printf("Nama : %s\n", mhs[i].nama);
printf("NIM : %s\n", mhs[i].nim);
printf("Kelas : %s\n", mhs[i].kelas);
printf("Nilai : %d\n", mhs[i].nilai);
printf("-------------------\n");
}
return 0;
}
Rumus rata-rata :
rata = tot / n;
Rumus Maksimal :
if(nilai[i] > nilai[max]){
max = i;
}
Koding setelah di tambah rata-rata dan max:
#include <stdio.h>
#define MAX 20
int main(void){
typedef struct {
char nama[80];
char nim[80];
char kelas[80];
int nilai;
} mahasiswa;
mahasiswa mhs[MAX];
int i, jml, max;
float tot, rata;
char strbuff[MAX];
printf("Jumlah data : ");
gets(strbuff);
jml = atoi(strbuff);
system("cls");
max = 0;
tot = 0;
for(i = 0; i < jml; i++){
printf("Data #%d\n", i + 1);
printf("Nama : ");
gets(mhs[i].nama);
printf("NIM : ");
gets(mhs[i].nim);
printf("Kelas : ");
gets(mhs[i].kelas);
printf("Nilai : ");
gets(strbuff);
mhs[i].nilai = atoi(strbuff);
tot += mhs[i].nilai;
if(mhs[i].nilai > mhs[max].nilai){
max = i;
}
system("cls");
}
rata = tot / jml;
for(i = 0; i < jml; i++){
printf("Data #%d\n", i + 1);
printf("Nama : %s\n", mhs[i].nama);
printf("NIM : %s\n", mhs[i].nim);
printf("Kelas : %s\n", mhs[i].kelas);
printf("Nilai : %d\n", mhs[i].nilai);
printf("-------------------\n");
}
system("pause & cls");
printf("----------------------\n");
printf("Rata-rata kelas : %.2f\n", rata);
printf("----------------------\n");
printf("Nilai tertinggi\n");
printf("----------------------\n");
printf("Data #%d\n", max + 1);
printf("Nama : %s\n", mhs[max].nama);
printf("NIM : %s\n", mhs[max].nim);
printf("Kelas : %s\n", mhs[max].kelas);
printf("Nilai : %d\n", mhs[max].nilai);
printf("----------------------\n");
return 0;
}
Untuk download koding lengkapnya klik link di bawah ini:
Download tanpa fungsi
Download dengan fungsi
0 komentar:
Posting Komentar