Solution for Exercise 12-24

ex1224

#include <stdio.h>

#define SIZE 5

void showarray(int array[]);
void arrayinc(int array[]);

int main()
{
    int n[] = { 2, 3, 5, 7, 11 };

    puts("Here's your array:");
    showarray(n);
    arrayinc(n);
    puts("After calling the arrayinc() function:");
    showarray(n);
    return(0);
}

void showarray(int array[])
{
    int x;

    for(x=0;x<SIZE;x++)
        printf("%d ",array[x]);
    putchar('\n');
}

void arrayinc(int array[])
{
    int x;

    for(x=0;x<SIZE;x++)
        array[x]++;
}

Output

Here's your array:
2 3 5 7 11
After calling the arrayinc() function:
3 4 6 8 12

Notes

* Because the array's size (5) s referenced in both functions, I declare a defined constant SIZE at Line 3.

* The code uses the ++ increment operator at Line 34 to add one to the value of each array element. I could have also used the += assignment operator, as in array[x]+=1, but ++ is more to the point.