Senin, 19 Desember 2011

Latihan Pemrograman C (1)

/* Program membalik Array */

#include <stdio.h>

int main ()
{
                int data[] = {2, 2, 7, 9, 3, 1};

                int i;
                for (i = 5; i > 0; i--)
                printf("%d", data[i]);

                                printf("\n");

                return 0;
}
//Menjumlahkan Matriks

#include <stdio.h>
#include <conio.h>
main()
{
                                int a[4]={3,7,4,6};
                                int b[4]={8,0,5,2};
                                int c[4],i;

                                for (i=0;i<4;i++) printf("a[%d]= %d\n",i+1,a[i]);
                                printf("\n\n");
                                for (i=0;i<4;i++) printf("a[%d]= %d\n",i+1,b[i]);
                                printf("\n\n");
                                printf("  A   +    B  =    C\n\n");
                                for (i=0;i<4;i++)
                                {
                                                 c[i]=a[i]+b[i];
                                                 if (i==1) printf("|%3d| + |%3d| = |%3d|\n",a[i],b[i],c[i]);
                                                 else printf("|%3d|   |%3d|   |%3d|\n",a[i],b[i],c[i]);
                                }
                                getch();
}


//Menjumlahkan Matriks A + Matriks B hasilnya Matriks C

#include <stdio.h>
main()
{
  int i,j;
  int A[4][3],B[4][3],C[4][3]; /* deklarasi array */
  /* Membaca A data dari keyboard */
  printf("\tMasukkan matriks A : \n");
  for (i=0;i<4;i++)
                 {
                  for(j=0;j<3;j++)
                                 {
                                  printf("\t\tA(%d,%d): ",i,j);
                                  scanf("%d", &A[i][j]);
                                 }
                 }

  printf("\n\tMasukkan matriks B : \n");
  for (i=0;i<4;i++)
                 {
                  for(j=0;j<3;j++)
                                 {
                                  printf("\t\tB(%d,%d): ",i,j);
                                  scanf("%d", &B[i][j]);
                                 }
                 }

                for (i=0;i<4;i++)
                 {
                  for(j=0;j<3;j++)
                                                C[i][j]=B[i][j]+A[i][j];
                 }

printf("\n\n");
/* Menampilkan data matriks yang telah dimasukkan */
printf("\n   Matriks A    +      Matriks B   =                 Matriks C\n");
for (i=0;i<4;i++)
  {
                 for(j=0;j<3;j++)
                                  printf("%4d",A[i][j]);
                 printf("        ");
                 for(j=0;j<3;j++)
                                  printf("%4d",B[i][j]);
                 printf("        ");
                 for(j=0;j<3;j++)
                                  printf("%4d",C[i][j]);
                 printf("\n");


}
}


/* Program membalik Array */

#include <stdio.h>

int main ()
{
int data[] = {3, 1, 7, 9, 2};

int tmp;
int i;
for (i = 0; i < 4; i +=2)
{
tmp = data [i];
data[i] = data[i+1];
data [i+1] = tmp;
}

/* Tampilkan isi Larik*/

for (i = 0; i < 5; i++)

printf("%d", data[i]);

printf("\n");

return 0;
}


/* ------------------------------------------------ */
/* File program : baca.c */
/* Contoh pengaksesan array satu dimensi */
/* ------------------------------------------------ */
#include <stdio.h>
#define maks_tes 5
main()
{
int i;
float nilai_tes[maks_tes]; /* deklarasi array */
/* Membaca data dari keyboard */
for (i=0;i<maks_tes;i++)
{
printf("Nilai tes ke-%d: ",i+1);
scanf("%f", &nilai_tes[i]);
}
/* Menampilkan data yang telah dimasukkan */
for (i=0;i<maks_tes;i++)
{
printf("Nilai tes ke-%d: %4.2f\n",i+1,nilai_tes[i]);
}
}
/* ------------------------------------------------ */
/* File program : baca2.c */
/* Contoh pengaksesan array dua dimensi */
/* ------------------------------------------------ */
#include <stdio.h>
main()
{
int i,j;
int matriks[4][3]; /* deklarasi array */
/* Membaca data dari keyboard */
for (i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("matriks (%d, %d): ",i,j);
scanf("%d", &matriks[i][j]);
}
}
printf("\n\n");
/* Menampilkan data matriks yang telah dimasukkan */
for (i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(" Matriks(%d, %d): %d",i,j,matriks[i][j]);
}
printf("\n");
}
}
/***************************************************
*  TUGAS PEMROGRAMAN   *
*  ----------------------------------------------  *
*    Tugas  : Mencari Rerata Semua Bilangan    *
***************************************************/
#include <stdio.h>
#define max 5

int main()
{
                float A[max], jumlah=0, rata_rata;
                int j;

                /*Memasukkan nilai ke dalam elemen array*/

                printf("Memasukkan Nilai :\n\n");
                for (j=0; j<max; j++)
                {
                                printf("Nilai Ke-%d = ",j+1);
                                scanf("%f", &A[j]);
                                jumlah += A[j];
                }
                /*Melakukan proses perhitungan rata-rata*/

                rata_rata = jumlah/max;

                /*Menampilkan hasil perhitungan*/
                printf("\nNilai Rata-Rata = %.2f", rata_rata);
                return 0;
}
/*Cari Nilai Yang Terbesar*/

#include <stdio.h>
#define max 5

main()
{
                int i, besar;
                int A[max];   /*deklarasi array*/


                /*Membaca data dari keyboard*/

                for (i=0; i<max; i++)
                {
                                printf("Nilai Ke-%d : ",i+1);
                                scanf("%d", &A[i]);
                }

                /*Menampilkan nilai terbesar*/
                besar = A[0];
                for (i=1; i<max; i++)
                if (besar<A[i])
                                besar = A[i];

                printf("\n\nNilai Terbesar  = %d", besar);
}

1 komentar:

  1. untuk masalah pemprograman, kalo bisa arti dari istilah seperi if, int, printf dLL mhon di jelaskan apa tujuan/fungsi dari kita menggunakan istilah tersebut dan mengapa kita harus menggunakannya...

    BalasHapus