Buat Program untuk menghitung nilai rata-rata tiap siswa dan nilai rata-rata tiap mata kuliah.
Note :Menggunakan array 2 dimensi
Kode
#include <stdio.h>
#include <stdlib.h>
#define maks 999
void main()
{
int v, totalmhs = 5;
float rata2mhs[5], rata2mk[4], mhs[5][4] = {
{8.4, 8.4, 9.4, 6.4},
{7.6, 6.6, 7.6, 8.6},
{5.4, 8.4, 5.4, 8.4},
{4.6, 6.6, 8.6, 7.6},
{7.6, 6.6, 6.6, 7.6}
};
char nama[5][maks] = {"Andi", "Hasan", "Dudi", "Adib", "Rahma"};
printf("Nama\t\t Fisika \t Mtmtika \t Kimia \t Biologi \t Rata2Mhs\n");
for (v=0; v <= totalmhs-1; v++)
{
rata2mhs[v] = (mhs[v][0] + mhs[v][1] + mhs[v][2] + mhs[v][3]) / 4;
if (v == 0) {
rata2mk[0] = mhs[v][0];
rata2mk[1] = mhs[v][1];
rata2mk[2] = mhs[v][2];
rata2mk[3] = mhs[v][3];
} else {
rata2mk[0] += mhs[v][0];
rata2mk[1] += mhs[v][1];
rata2mk[2] += mhs[v][2];
rata2mk[3] += mhs[v][3];
}
printf("%-16s %4.1f \t\t %4.1f \t\t %4.1f \t\t %4.1f \t\t %4.2f \t \n", nama[v], mhs[v][0], mhs[v][1], mhs[v][2], mhs[v][3], rata2mhs[v]);
}
printf("%-16s %4.2f \t\t %4.2f \t\t %4.2f \t\t %4.2f", "Rata2MK", rata2mk[0]/5, rata2mk[1]/5, rata2mk[2]/5, rata2mk[3]/5);
}
hasil
Nama | Nilai Fisika | Nilai Matematika | Nilai Kimia | Nilai Biologi |
Jimin | 8.4 | 8.4 | 9.4 | 6.4 |
Jin | 7.6 | 6.6 | 7.6 | 8.6 |
J-Hope | 5.4 | 8.4 | 5.4 | 8.4 |
Suga | 4.6 | 6.6 | 8.6 | 7.6 |
V | 7.6 | 6.6 | 7.6 | 6.6 |
Kode
#include <stdio.h>
#include <stdlib.h>
#define maks 999
void main()
{
int v, totalmhs = 5;
float rata2mhs[5], rata2mk[4], mhs[5][4] = {
{8.4, 8.4, 9.4, 6.4},
{7.6, 6.6, 7.6, 8.6},
{5.4, 8.4, 5.4, 8.4},
{4.6, 6.6, 8.6, 7.6},
{7.6, 6.6, 6.6, 7.6}
};
char nama[5][maks] = {"Andi", "Hasan", "Dudi", "Adib", "Rahma"};
printf("Nama\t\t Fisika \t Mtmtika \t Kimia \t Biologi \t Rata2Mhs\n");
for (v=0; v <= totalmhs-1; v++)
{
rata2mhs[v] = (mhs[v][0] + mhs[v][1] + mhs[v][2] + mhs[v][3]) / 4;
if (v == 0) {
rata2mk[0] = mhs[v][0];
rata2mk[1] = mhs[v][1];
rata2mk[2] = mhs[v][2];
rata2mk[3] = mhs[v][3];
} else {
rata2mk[0] += mhs[v][0];
rata2mk[1] += mhs[v][1];
rata2mk[2] += mhs[v][2];
rata2mk[3] += mhs[v][3];
}
printf("%-16s %4.1f \t\t %4.1f \t\t %4.1f \t\t %4.1f \t\t %4.2f \t \n", nama[v], mhs[v][0], mhs[v][1], mhs[v][2], mhs[v][3], rata2mhs[v]);
}
printf("%-16s %4.2f \t\t %4.2f \t\t %4.2f \t\t %4.2f", "Rata2MK", rata2mk[0]/5, rata2mk[1]/5, rata2mk[2]/5, rata2mk[3]/5);
}
hasil
EmoticonEmoticon