Textbook Solutions
0%

Array Some Problems โ€”
Complete Solutions

Describing array definitions, identifying initial values, and writing new array definitions.

๐Ÿ“‹
Part A โ€” Describe the Array (Different examples with details)
Identify type, size, storage class from declarations
(a)

char name[30];

One-dimensional character array named name. Size = 30 elements. Each element holds one character. Storage class = automatic (defined inside a function). Can store a string up to 29 characters + null terminator '\0'.

desc_a.c
C
char name[30];           /* 30-element char array, auto storage */
                          /* stores strings up to 29 chars + '\0' */
scanf("%s", name);       /* read a name */
printf("%s", name);      /* print it */
(b)

float c[6];

One-dimensional float array named c. Size = 6 elements. Each element is a floating-point number. Storage class = automatic. Indices run from c[0] to c[5]. Not initialised โ€” contains garbage values.

(c)

#define N 50 โ†’ static int a[N];

#define N 50 creates a symbolic constant. static int a[N] is a static one-dimensional integer array of 50 elements. Static means it is initialised to 0 automatically and retains its value between function calls.

desc_c.c
C
#define N 50
static int a[N];   /* 50-element static int array */
                   /* all 50 elements auto-init to 0 */
                   /* persists between function calls */
(d)

int a[N]; int params[5][5];

int a[N] โ€” one-dimensional int array of N elements (N from #define).

int params[5][5] โ€” two-dimensional integer array, 5 rows ร— 5 columns = 25 elements total. Stored in row-major order in memory.

(e)

#define A 88 ยท #define B 155

Two symbolic constants โ€” A = 88 and B = 155. Can be used as array sizes: int table[A][B] creates an 88ร—155 = 13,640 element 2D integer array. The constants make changing the size easy.

(f)

char sens[4][10]; double accounts[50][25][80];

char sens[4][10] โ€” 2D char array, 4 rows ร— 10 cols. Stores 4 strings of up to 9 characters each.

double accounts[50][25][80] โ€” three-dimensional double array. Total elements = 50 ร— 25 ร— 80 = 100,000 doubles. Each double = 8 bytes โ†’ total memory = 800,000 bytes = ~781 KB!

๐Ÿ”ข
Part B โ€” What Values Are Assigned? (Problem 9.28 second part)
Trace through each initialiser and identify element values
(a)

float c[8] = {0., 5., 3., -4., 12., 12., 0., 8.};

8-element float array. Values assigned in order: c[0]=0.0, c[1]=5.0, c[2]=3.0, c[3]=-4.0, c[4]=12.0, c[5]=12.0, c[6]=0.0, c[7]=8.0. All 8 elements specified so none default to 0.

(b)

float c[8] = {2., 0., 5., 0., 3., -4.};

8-element float array, only 6 values given. First 6 elements: c[0]=2.0, c[1]=0.0, c[2]=5.0, c[3]=0.0, c[4]=3.0, c[5]=-4.0. Remaining 2 elements automatically set to 0: c[6]=0.0, c[7]=0.0.

vals_b.c
C
float c[8] = {2., 0., 5., 0., 3., -4.};
/* c[0]=2.0  c[1]=0.0  c[2]=5.0  c[3]=0.0
   c[4]=3.0  c[5]=-4.0  c[6]=0.0  c[7]=0.0  (auto) */
(c)

int s[12] = {0, 0, 8, 0, 0, 6};

12-element int array, only 6 values given. s[0]=0, s[1]=0, s[2]=8, s[3]=0, s[4]=0, s[5]=6. Remaining 6 elements s[6] through s[11] = 0 automatically.

(d)

char flag[4] = {'T', 'R', 'U', 'E'};

4-element character array. Values: flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E'. Note: This is NOT a null-terminated string because there is no '\0' at the end. It is just 4 characters. To make it a string, declare char flag[5].

(e)

char flag[5] = {'T', 'R', 'U', 'E'};

5-element char array, 4 values given. flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E', flag[4]='\0' (auto zero = null terminator). This IS a valid null-terminated string "TRUE".

(f)

char flag[] = "TRUE";

Auto-sized char array initialised with string literal. Compiler sets size = 5 (4 chars + '\0'). Values: flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E', flag[4]='\0'. This is the standard way to declare a string.

vals_f.c
C
char flag[] = "TRUE";      /* size=5, auto-adds '\0' */
char flag[] = "FALSE";     /* size=6 */
printf("%s\n", flag);      /* prints: FALSE */
(g)

int p[2][4] = {1, 3, 5, 7};

2ร—4 = 8 element integer array, only 4 values given. Filled row by row: p[0][0]=1, p[0][1]=3, p[0][2]=5, p[0][3]=7. Remaining 4 elements of row 1 all become 0: p[1][0]=p[1][1]=p[1][2]=p[1][3]=0.

(h)

int p[2][4] = {1, 1, 3, 3, 5, 5, 7, 7};

All 8 elements specified. Filled row by row: Row 0: p[0][0]=1, p[0][1]=1, p[0][2]=3, p[0][3]=3. Row 1: p[1][0]=5, p[1][1]=5, p[1][2]=7, p[1][3]=7.

(i)

int p[2][4] = {{1,3,5,7},{2,4,6,8}};

All 8 elements with row braces. Row 0: p[0] = {1,3,5,7}. Row 1: p[1] = {2,4,6,8}. Cleanest and most readable format for 2D array initialisation.

vals_i_to_k.c
C
/* (i) Both rows fully specified with inner braces */
int p[2][4] = {{1,3,5,7},{2,4,6,8}};

/* (k) Only 2 values in row 0, only 2 in row 1 */
int p[2][4] = {
    {1, 3},     /* p[0][0]=1, p[0][1]=3, p[0][2]=0, p[0][3]=0 */
    {5, 7}      /* p[1][0]=5, p[1][1]=7, p[1][2]=0, p[1][3]=0 */
};
(l)

int c[2][3][4] = {{{1,2,3},{4,5},{6,7,8,9}},{{10,11},{},{12,13,14}}};

3D array โ€” 2 layers, 3 rows each, 4 columns each. Total = 24 elements. Filled layer by layer:

  • Layer 0, Row 0: {1,2,3,0} โ€” 3 given, 4th defaults to 0
  • Layer 0, Row 1: {4,5,0,0} โ€” 2 given, rest 0
  • Layer 0, Row 2: {6,7,8,9} โ€” all 4 given
  • Layer 1, Row 0: {10,11,0,0} โ€” 2 given
  • Layer 1, Row 1: {0,0,0,0} โ€” empty braces, all 0
  • Layer 1, Row 2: {12,13,14,0} โ€” 3 given
vals_l.c
C
int c[2][3][4] = {
    {                    /* Layer 0 */
        {1, 2, 3},      /* row 0: {1,2,3,0} */
        {4, 5},         /* row 1: {4,5,0,0} */
        {6, 7, 8, 9}   /* row 2: {6,7,8,9} */
    },
    {                    /* Layer 1 */
        {10, 11},       /* row 0: {10,11,0,0} */
        {},              /* row 1: {0,0,0,0} */
        {12, 13, 14}   /* row 2: {12,13,14,0} */
    }
};
(m)

char colors[3][6] = {{'R','E','D'},{'G','R','E','E','N'},{'B','L','U','E'}};

2D char array โ€” 3 rows ร— 6 cols. Each row stores one colour name (as chars, NOT strings unless null-terminated):

  • Row 0: 'R','E','D','\0','\0','\0' โ€” "RED" + 3 auto zeros (= null terminators)
  • Row 1: 'G','R','E','E','N','\0' โ€” "GREEN" + 1 auto zero
  • Row 2: 'B','L','U','E','\0','\0' โ€” "BLUE" + 2 auto zeros

Because unused positions become 0 (= '\0'), each row is a valid null-terminated string!

vals_m.c
C
char colors[3][6] = {
    {'R','E','D'},
    {'G','R','E','E','N'},
    {'B','L','U','E'}
};

/* Each row IS a null-terminated string because */
/* unused positions auto-fill with 0 = '\0'     */
printf("%s\n", colors[0]);   /* prints RED   */
printf("%s\n", colors[1]);   /* prints GREEN */
printf("%s\n", colors[2]);   /* prints BLUE  */
โœ๏ธ
Part C โ€” Write Array Definitions (Problem 9.29)
aโ€“d: write appropriate array declarations and initialisations
9.29a

12-element int array called c โ€” values 1, 4, 7, 10, ..., 34

Problem
Define a one-dimensional, 12-element integer array called c. Assign the values 1, 4, 7, 10, ..., 34 to the array elements.

Pattern: starts at 1, increases by 3 each time. Values: 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34. That is 12 values (check: 1 + 11ร—3 = 34 โœ“).

prob9_29a.c
C
#include <stdio.h>

int main() {
    /* Method 1: Direct initialisation */
    int c[12] = {1, 4, 7, 10, 13, 16,
                 19, 22, 25, 28, 31, 34};

    /* Method 2: Using formula c[i] = 1 + i*3 */
    int c2[12], i;
    for(i=0; i<12; i++) c2[i] = 1 + i*3;

    printf("c: ");
    for(i=0; i<12; i++) printf("%d ", c[i]);
    printf("\n");
    return 0;
}
terminal
output
c: 1 4 7 10 13 16 19 22 25 28 31 34
9.29b

1D char array called point โ€” string "NORTH" with null

Problem
Define a one-dimensional character array called point. Assign the string "NORTH" to the array elements. End the string with the null character.
prob9_29b.c
C
char point[] = "NORTH";               /* Best: auto-adds '\0' */
                                        /* size = 6 automatically */

/* Alternative: explicit chars with '\0' */
char point2[6] = {'N','O','R','T','H','\0'};

printf("%s\n", point);   /* NORTH */
9.29c

4-element char array called letters โ€” 'S', 'E', 'W' (and more?)

Problem
Define a one-dimensional, four-element character array called letters. Assign the characters 'S', 'E' and 'W' to the array elements.

Four-element array, 3 characters given. The 4th element defaults to '\0' (null) automatically. So this stores the string "SEW".

prob9_29c.c
C
char letters[4] = {'S', 'E', 'W'};
/* letters[0]='S'  letters[1]='E'  letters[2]='W'  letters[3]='\0' */
printf("%s\n", letters);   /* SEW */
9.29d

6-element float array called consts โ€” 0.005, -0.032, 1e-6, 0.167, -0.3e8, 0.015

Problem
Define a one-dimensional, six-element floating-point array called consts. Assign the following values: 0.005, โˆ’0.032, 1eโˆ’6, 0.167, โˆ’0.3e8, 0.015

Six float values including scientific notation. 1e-6 = 0.000001 and -0.3e8 = -30,000,000. These are valid C float literals.

prob9_29d.c
C
#include <stdio.h>

int main() {
    float consts[6] = {
         0.005,    /* consts[0] */
        -0.032,    /* consts[1] */
         1e-6,     /* consts[2] = 0.000001 */
         0.167,    /* consts[3] */
        -0.3e8,    /* consts[4] = -30000000.0 */
         0.015     /* consts[5] */
    };

    for(int i=0; i<6; i++)
        printf("consts[%d] = %g\n", i, consts[i]);
    return 0;
}
terminal
output
consts[0] = 0.005
consts[1] = -0.032
consts[2] = 1e-06
consts[3] = 0.167
consts[4] = -3e+07
consts[5] = 0.015
%g format: Automatically chooses between fixed and scientific notation โ€” whichever is shorter. So 1e-6 prints as 1e-06 and -0.3e8 prints as -3e+07.
checklist

Checklist

  • I can describe any array declaration โ€” type, dimensions, size, storage class
  • Partial initialisation: missing elements automatically become 0
  • char flag[] = "TRUE" auto-adds '\0' and sets size=5
  • static arrays auto-initialise all elements to 0
  • 2D array partial row braces: missing elements in that row โ†’ 0
  • 3D array: empty braces {} set all elements of that sub-array to 0
  • Problem 9.29a: formula c[i] = 1 + i*3 generates the sequence
  • Problem 9.29d: scientific notation 1e-6 and -0.3e8 are valid float literals