0% found this document useful (0 votes)
146 views

Exercise 1

The document describes a laboratory exercise on two-dimensional arrays in C. It introduces 2D arrays and how they allow data to be organized in a table using rows and columns indexed by two variables. It provides an example of declaring and initializing a 2D array, as well as defining a function to print a 2D array by passing the array to the function. The exercise asks the student to analyze some 2D array declarations for validity, determine the size of each array, write loops to display the contents, and add another initialized 2D array.

Uploaded by

Diannara Flores
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Exercise 1

The document describes a laboratory exercise on two-dimensional arrays in C. It introduces 2D arrays and how they allow data to be organized in a table using rows and columns indexed by two variables. It provides an example of declaring and initializing a 2D array, as well as defining a function to print a 2D array by passing the array to the function. The exercise asks the student to analyze some 2D array declarations for validity, determine the size of each array, write loops to display the contents, and add another initialized 2D array.

Uploaded by

Diannara Flores
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Laboratory Exercise on Two Dimensional Arrays

Introduction
In C, [one-dimensional] arrays provide a mechanism to access a sequence of data using a single index,
such as item[0], item[1], item[2], .... Similarly, C supports the organization of data in a
table by specifying a row and a column:

As with one-dimensional arrays, two-


dimensional arrays can contain data of any
type, although each entry in a table must have
the same type. Also, the size of a two-
dimensional array must be declared when it is
first defined:

int table [5][10];

When using a two-dimensional array as a


parameter, the declaration of a function header
must know the number of columns in the table
(so the computer knows when one row stops
and the next starts). As with 1-dimensional
arrays, a function header does not need to
specify the number of rows in the table. Thus,
if the above table were passed to
a printArray function, the header
of printArray might be given as follows:

void printArray (int arr[][10])

The sample program 2D-array-with-proc.c shows a 2-dimensional array declared in main and passed
to a printArray function.

Altogether, two dimensional arrays are a wonderful way to store lots of data which would normally
require lots of arrays!
2D-array-with-proc.c
/* C program illustrating how 2-dimensional arrays
are passed in C
*/

#include <stdio.h>

void printArray (int arr[][10])


{
int a, b;
printf ("The 2-dimensional array is:\n");
for (a = 0; a < 5; a++)
{
for (b = 0; b < 10; b++)
printf ("%5d", arr[a][b]);
printf ("\n");
}
}

int main ()
{
/* declare the 2-dimensional array */
int table [5][10];
int i, j; /* loop indices */

/* initialize the array */


for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
table[i][j] = 10*i + j;

printArray (table);

printf ("table element [3][7]: %d\n", table [3][7]);


printf ("table element [3][12]: %d\n", table [3][12]);
printf ("table element [3][-4]: %d\n", table [3][-4]);

return 0;
}
Exercise 1
1. The following program will not compile. Copy the program 2D-array.c and look over the various
array declarations.

2D-array.c 
#include <stdio.h>

int main()
{
int i; // counter variables initialized to 0

int array1[][3] = {{4, 5, 3}, {5, 3, 5}, {5, 2, 8}};


int array2[][5] = {{3, 4, 1}, {6, 3, 2, 4, 1}, {3, 5}};
int array3[2][2] = {{5, 3}, {2, 1}};
int array4[3][] = {{2, 4, 3}, {4, 4, 1}, {1, 2, 3}};
int array5[][] = {{3, 2, 1}, {4, 3, 2}, {2, 4, 6}};

printf ("array1 size = %d\n", sizeof(array1));


for (i = 0; i < 3; i++)
printf ("\tsize of row %d: %d\n", i, sizeof(array1[i]));
printf("\n");

printf ("array2 size = %d\n", sizeof(array2));


for (i = 0; i < 3; i++)
printf ("\tsize of row %d: %d\n", i, sizeof(array2[i]));
printf("\n");

printf ("array3 size = %d\n", sizeof(array3));


for (i = 0; i < 3; i++)
printf ("\tsize of row %d: %d\n", i, sizeof(array3[i]));
printf("\n");

} // main

The program is supposed to print out the sizes of each of the valid arrays and all of their rows
one by one. Note that an integer is 4 bytes, and arrays in the program are arrays of integers.
Thus, to find the size of an array the program multiplies how many integers are in the array by
the size of an integer, which is 4 bytes. This is why you get 4 times the number of things there
are in the array.

a. Which array declarations are invalid and are preventing the program from compiling? Do
you see a pattern of when it is invalid? Comment out the lines that are not working.
b. Write in comments next to each array what the size of the whole array is and what the
size of each row is?
c. How many elements does each array hold?
d. Write loops to display the contents of each array. You should use separate
nested for loops for each array.
e. Add another array, array6[][], which has the numbers (0, 1, 2, 3) in the first row, (2,
4, 6, 8) in the second row, and (3, 6, 9, 12) in the third row. Display the array once you
have declared and initialized it.

You might also like