[Basic]Using Array

Thursday, October 1, 2009

Now we'll try to using array to find rate of input values. This is the code:

*/ therudi.com Oct 1, 2009 */

#include <stdio.h>
#define MAKS 5

int main(void){

    int i;
    float total = 0, rata;
    float nilai_tes[MAKS];   /* deklarasi array */

    for(i=0; i<MAKS; i++){
        printf("Nilai tes ke-%d : ", i+1);
        scanf("%f", &nilai_tes[i]);

        /* hitung jumlah total nilai */
        total = total + nilai_tes[i];
    }
    rata = total / MAKS;

    /* cetak nilai rata2 */
    printf("\nNilai rata-rata = %g\n", rata);

    return 0;
}


And, this is the example of its execution:

Nilai tes ke-1 : 5
Nilai tes ke-2 : 6
Nilai tes ke-3 : 7
Nilai tes ke-4 : 4
Nilai tes ke-5 : 5

Nilai rata-rata = 5.4

0 comments:

Post a Comment

 
 
Copyright © Welcome