Contoh I
#include<stdio.h>
#include<conio.h>
#define MAXNILAI 10
#define MAXMHS 100
main(){
int nilaimhs[MAXMHS][MAXNILAI];
int i, j, M, N, nilai;
float rata2[MAXMHS], total;
printf(“nama :\t ………….\n”);
printf(“nim :\t …………….\n\n”);
for(i=0;i<MAXMHS;i++)
for(j=0;j<MAXNILAI;j++)
nilaimhs[MAXMHS][MAXNILAI]= 0;
printf(“jumlah mahasiswa: “);
scanf(“%d”, &M);
printf(“jumlah nilai: “);
scanf(“%d”, &N);
for(i=0;i<M;i++)
{
total=0.0;
printf(“Mahasiswa ke-%d: \n”,i+1);
for(j=0;j<N;j++)
{
printf(“Nilai ke-%d: “, j+1);
scanf(“%d”, &nilai);
nilaimhs[nilai];
total=total+nilai;
}
rata2[i]=total/N;
}
printf(“daftar nilai mhasiswa\n”);
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
printf(“rata-rata: %5.2f\t”, rata2[i]);
printf(“\n\n”);
}
getch();
}
CONTOH II

#include <stdio.h>
#include <conio.h>
#define SIZE 5

void ubahArray(int a[], int n);
int ubahElemen (int z);
main(){
int a[]={-2, 0, 5, 11, 17};
int b[]={0};
int c[SIZE]={5, 17};
int i;
printf(“nama :\t ………..\n”);
printf(“nim :\t …………..\n\n”);
puts(“\nNilai awal array:”);
puts(“element\t a[i]\tb[i]\tc[i]”);

for(i=0; i<SIZE;i++)
printf(“%d\t%d\t%d\t%d\n”, i, a[i], b[i], c[i]);
puts(“\n masukkan nilai array:”);
for(i=0;i<SIZE;i++)
{
printf(“c[%d]=”,i);
scanf(“%d”, &c[i]);
}
ubahArray(b, SIZE);
ubahElemen(a[2]);
puts(“\n Nilai akhir array:”);
puts(“Elemen \t a[i]\tb[i]\tc[i]”);

for(i=0;i<SIZE;i++){
printf(“%d\t%d\t%d\t%d\t\n”, i, a[i], b[i], c[i]);}
getch();
}
void ubahArray (int a[], int n)
{
int j;
for(j=0;j<n;j++)
a[j]=j;
}
int ubahElemen (int a)
{
a*=2;
printf(“\nNilai dalam ubah elemen() adalah %d\n”, a);
return a;
}

CONTOH III

#include <stdio.h>
#include <conio.h>
#define MAX 2
int bacaArray(int [], int);
void bubleSort(int[], int);
float median(int[], int);
void cetakArray(int[], int);

main(){
int size;
float med;
int data[MAX];
puts(“masukkan data, dan diakhiri dengan ctrl-Z.”);
size=bacaArray(data, MAX);
if(size !=0)
{
bubleSort(data, MAX);
med=median(data, MAX);
puts(“Data setelah diurutkan:”);
cetakArray(data, MAX);
printf(“\n\nBanyaknya data =%d, median=%f\n”, size, med);
}
else
puts(“Error!Tidak ada data yang valid.”);
getch();
}
int bacaArray(int array[], int max){
int j=0;
while (j<max && scanf(“%d”, &array[j])>=1)
j++;
return(j);
}
void cetakArray(int array[], int size)
{
int j;
for(j=0;j<size;j++)
printf(“Array[%d]=%d\n”, j, array[j]);
}
void bubleSort(int array[], int size)
{
int putaran, j, temp;
for (putaran=1;putaran<size;putaran++)
for(j=0;j<size-1;j++);
if (array[j]>array[j+1])
{
temp=array[j+1];
}
}
float median(int urut[], int size){
if (size%2!=0)
return(size/2+1);
else
return (urut[size/2-1]+urut[size/2])/2;
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s