03: Strings, arrays, and loops

Example program

#define NUMBER_OF_PLANETS 9

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char** argv ) {

/*
 average distance of planets from sun
 mercury:   57 million km.
 venus:    108 million km.
 earth:    150 million km.
 mars:     228 million km.
 jupiter:  779 million km.
 saturn;  1430 million km.
 uranus:  2880 million km.
 neptune: 4500 million km.
 pluto:   5910 million km.
*/

    // "char" means "character"
    // a character can be a letter, a digit, punctuation
    // trivia: punched cards had 80 columns
    //         some early video terminals displayed 80 char

    // names is an array of arrays of characters
    // another way to say this: it is an array of strings
    char names[9][80];

    // "str" stands for "string"
    // "cpy" stands for "copy"
    // strcpy() is a function that copies a string
    // "mercury" is a literal (a value)
    // function call stores "mercury" in the location names[0]
    strcpy( names[0], "mercury" );
    strcpy( names[1], "venus" );
    strcpy( names[2], "earth" );
    strcpy( names[3], "mars" );
    strcpy( names[4], "jupiter" );
    strcpy( names[5], "saturn" );
    strcpy( names[6], "uranus" );
    strcpy( names[7], "neptune" );
    strcpy( names[8], "pluto" );

    // distances is an array of integers
    int distances[9];
    distances[0] = 57;
    distances[1] = 108;
    distances[2] = 150;
    distances[3] = 228;
    distances[4] = 779;
    distances[5] = 1430;
    distances[6] = 2880;
    distances[7] = 4500;
    distances[8] = 5910;

    for( int i = 0; i < NUMBER_OF_PLANETS; i++ ) {
        printf( 
            "distance of %s from the sun is %4d km.\n",
            names[i], distances[i] * 1000000 );
    } // for

    exit( 0 );
} // main( int, char** )