Jumat, 09 Mei 2014

Menentukan Panjang Array dengan pendefinisan konstan dan penginsialisasian elemen array dengan kalkulasi perhitungan.

/* Fig. 6.5: fig06_05.c
Initialize the elements of array s to the even integers from 2 to 20 */
#include <stdio.h>
#define SIZE 10 /* maximum size of array */
/* function main begins program execution */
int main( void )
{
   /* symbolic constant SIZE can be used to specify array size */
   int s[ SIZE ]; /* array s has SIZE elements */
   int j; /* counter */
   for ( j = 0; j < SIZE; j++ ) { /* set the values */
          s[ j ] = 2 + 2 * j;
   } /* end for */
   printf( "%s%13s\n", "Element", "Value" );
   /* output contents of array s in tabular format */
   for ( j = 0; j < SIZE; j++ ) {
           printf( "%7d%13d\n", j, s[ j ] );
   } /* end for */
   return 0; /* indicates successful termination */
} /* end main */


“#define SIZE 10” mengindikasikan bahwa nilai maksimum dari SIZE adalah 10, lalu panjang array juga 10 di tentukan dari kode “int s[ SIZE ];”. Nilai dari setiap elemen array juga tergantung dari kalkulasi berikut: “s[ j ] = 2 + 2 * j;”.

Menjumlahkan nilai dari setiap elemen-elemen array

/* Fig. 6.6: fig06_06.c
Compute the sum of the elements of the array */
#include <stdio.h>
#define SIZE 12
/* function main begins program execution */
int main( void )
{
   /* use initializer list to initialize array */
   int a[ SIZE ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 };
   int i; /* counter */
   int total = 0; /* sum of array */
   /* sum contents of array a */
   for ( i = 0; i < SIZE; i++ ) {
        total += a[ i ];
   } /* end for */
   printf( "Total of array element values is %d\n", total );
   return 0; /* indicates successful termination */
} /* end main */

Penggambaran nilai dari elemen array dengan tampilan histogram.
Contoh program dibawah ini akan menampilkan nilai dari elemen array terlebih dahulu, kemudian akan menampilkan histogram dengan menampilkan symbol “*” sebanyak nilai dari elemen array itu sendiri.

/* Fig. 6.8: fig06_08.c
Histogram printing program */
#include <stdio.h>
#define SIZE 10
/* function main begins program execution */
int main( void )
{
   /* use initializer list to initialize array n */
   int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
   int i; /* outer for counter for array elements */
   int j; /* inner for counter counts *s in each histogram bar */
   printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
   /* for each element of array n, output a bar of the histogram */
   for ( i = 0; i < SIZE; i++ ) {
       printf( "%7d%13d ", i, n[ i ]) ;
       for ( j = 1; j <= n[ i ]; j++ ) { /* print one bar */
                printf( "%c", '*' );
        } /* end inner for */
        printf( "\n" ); /* end a histogram bar */
   } /* end outer for */
   return 0; /* indicates successful termination */
} /* end main */

Screenshots Output:









Array of Character
Ada beberapa cara penulisan array untuk karakter string:
1. char string1[] = "first";
      2. char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };
    Sebenarnya cara penulisan nomor 1 dan nomor 2 sama, tapi untuk cara penulisan nomor 2 setiap karakter     dari kata first bisa diakses secara individu, semisal kita ingin menampilkan string1[1] maka akan tampil         huruf “f”, string1[2] maka akan tampil huruf “I”.
3.  char string2[ 20 ]; 
    scanf( "%s", string2 );
   Untuk contoh diatas biasa kita gunakan jika kita ingin menginputkan value dari variable sting2 langsung melalui keyboard ketika program dijalankan. Dan dalam contoh diatas, karakter string hanya dibatasi sebanyak 20.

Contoh program:
/* Fig. 6.10: fig06_10.c
Treating character arrays as strings */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
     char string1[ 20 ]; /* reserves 20 characters */
     char string2[] = "string literal"; /* reserves 15 characters */
     int i; /* counter */
     /* read string from user into array string1 */
     printf("Enter a string: ");
     scanf( "%s", string1 ); /* input ended by whitespace character */
     /* output strings */
     printf( "string1 is: %s\nstring2 is: %s\n"
      "string1 with spaces between characters is:\n",
      string1, string2 );
     /* output characters until null character is reached */
     for ( i = 0; string1[ i ] != '\0'; i++ ) {
           printf( "%c ", string1[ i ] );
     } /* end for */26 printf( "\n" );
     return 0; /* indicates successful termination */
} /* end main */

Screeshots Output:

Pada program diatas, inputan “Hello there” hanya dibaca sebagai “Hello” karena untuk penggunaan type data char tidak boleh menginputkan spasi. Ketika di inputkan spasi, program akan membacanya sebagai enter (\n).


0 komentar:

Posting Komentar