Sedikit mengulang materi Algoritma dan Pemrograman tentang aritmatika array.
Contoh programnya Manipulasi Array :
#include <conio.h>
#include <stdio.h>
#define array_size 10
int a[]={5,10,6,0,4,7,14,19,22,3};
int b[array_size];
void garis()
{
for(int x=0;x<70;x++)
{
printf("%c",196);
}
printf("\n");
}
void print_a()
{
int i;
printf(" Index : ");
for(i=0;i
{
printf("%4d",i);
}
printf("\n Nilai : ");
for(i=0;i
{
printf("%4d",a[i]);
}
printf("\n");
}
void print_b()
{
int i;
printf(" Index : ");
for(i=0;i
{
printf("%4d",i);
}
printf("\n Nilai : ");
for(i=0;i
{
printf("%4d",b[i]);
}
printf("\n");
}
void print_b2()
{
int i;
printf(" Index : ");
for(i=0;i
{
printf("%4d",i);
}
printf("\n Nilai : ");
for(i=0;i
{
if(b[i]==0)
printf(" _");
else
printf("%4d",b[i]);
}
printf("\n");
}
void reset_b()
{
int i;
for(i=0;i
{
b[i]=NULL;
}
}
void main()
{
int i;
int counter;
garis();
printf("Ini adalah array A :\n\n");
print_a();
for(i=0;i
{
b[i]=a[i];
}
garis();
printf("Ini adalah array B : \n\n");
print_b();
reset_b();
garis();
//--------------------------------------------
printf("Ini adalah array B dengan posisi terbalik : \n\n");
for(i=0;i
{
b[i]=a[array_size-1-i];
}
print_b();
reset_b();
garis();
//----------------------------------------------
printf("Ini adalah array B dengan elemen GENAP : \n\n");
for(i=0;i
{
if((a[i] % 2 == 0) && (a[i] > 0))
{
b[i]=a[i];
counter++;
}
}
print_b2();
reset_b();
garis();
//-----------------------------------------------
printf("Ini adalah array B dengan penghapusan elemen: \n\n");
counter=0; // counter untuk menambah index b...
for(i=0;i
{
if((a[i] %2 == 0) && (a[i] > 0))
{
b[counter]=a[i];
counter++;
}
}
print_b2();
garis();
}
0 comments:
Post a Comment