Input Nilai Siswa

#include <iostream>
#include <iomanip>
using namespace std;

struct siswa
{
    char NIS[11];
    char NAMA[30];
    float NILAI;
};

typedef siswa swa[50];

int main() {
    int jumlahsiswa, pilihan;
    swa x;
    while (pilihan != 5) {
        cout << "----------------------------------------------------\n";
        cout << "MENU INPUT DATA SISWA\n";
        cout << "1. Masukan data\n";
        cout << "2. Lihat data\n";
        cout << "3. Tampilkan rata rata\n";
        cout << "4. Tampilkan siswa nilai tertinggi dan terendah\n";
        cout << "5. Keluar\n";
        cout << "----------------------------------------------------\n";
        cin >>pilihan;
        if (pilihan == 1) {
            cout << "Masukan jumlah siswa =";
            cin >> jumlahsiswa;
            for (int i = 0; i < jumlahsiswa; i++) {
                cout << "Siswa ke " << i + 1;
                cout << "\nNomor Induk Siswa : ";
                cin >> x[i].NIS;
                cout << "NAMA : ";
                cin >> x[i].NAMA;
                cout << "NILAI : ";
                cin >> x[i].NILAI;
                cout << endl;
            }
        } else if (pilihan == 2) {
            cout << "\n |" << setw(13) << "NIS |";
            cout << setw(32) << "NAMA |" << setw(7) << "NILAI |" << endl;
            cout <<"------------------------------------------------------------\n";
            for (int i = 0; i < jumlahsiswa; i++) {
                cout << " |" << setw(11) << x[i].NIS << " |";
                cout << setw(30) << x[i].NAMA << " |";
                cout << setw(5) << x[i].NILAI << " |" << endl;

            }
        } else if (pilihan == 3) {
            cout << "Rata - rata dari nilai data tersebut adalah=";
            int jumlah = 0;
            float rata;
            for (int i = 0; i < jumlahsiswa; i++) {
                jumlah += x[i].NILAI;
            }
            rata = jumlah / jumlahsiswa;
            cout << rata << "\n";
        } else if (pilihan == 4) {
            int max = x[0].NILAI, min =x[0].NILAI;
            int indexMax=0,indexMin=0;
            for (int i = 0; i < jumlahsiswa; i++) {
                if (max > x[i].NILAI){
                    max = x[i].NILAI;
                    indexMax=i;
                }
                if (min < x[i].NILAI){
                    min = x[i].NILAI;
                    indexMin=i;
                }
            }
            cout << "Siswa dengan nilai tertinggi adalah\n";
            cout << " |" << setw(11) << x[indexMax].NIS << " |";
            cout << setw(30) << x[indexMax].NAMA << " |";
            cout << setw(5) << x[indexMax].NILAI << " |" << endl;
            cout << "Siswa dengan nilai terendah adalah\n";
            cout << " |" << setw(11) << x[indexMin].NIS << " |";
            cout << setw(30) << x[indexMin].NAMA << " |";
            cout << setw(5) << x[indexMin].NILAI << " |" << endl;
        }

    }

}

No comments:

Post a Comment