UIU: Learn C by Examples : Arrays


Program 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a [5] = {5, 1, 15, 20, 25};

    int i = ++a [1];

    printf ("%d\n", i);

    printf ("%d\n", a [1]++);

    printf ("%d\n", a [i++]);

    printf ("%d\n", a [++i]);

    return 0;
}

// @END_OF_SOURCE_CODE

Program 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

void fun (int n, int arr [])
{
    int *p = 0;
    int i = 0;

    while ( i++ < n )
        p = &arr [i];

    *p = 0;
}

int main ()
{
    int arr [] = {1, 2, 3, 4};

    fun (4, arr);

    for ( int i = 0; i < 4; i++ )
        printf ("%d, ", arr [i]);

    return 0;
}

// @END_OF_SOURCE_CODE

Program 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int arr [5], i = 0;

    while ( i < 5)
        arr [i] = ++i;

    for ( i = 0; i < 5; i++ )
        printf ("%d, ", arr [i]);

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

2 thoughts on “UIU: Learn C by Examples : Arrays

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s