Skip over navigation

1D Arrays

Problems and Solutions 1

Accessing the Data in a One-Dimensional Array

Looping Constructs for One-Dimensional Arrays

Problem : Declare an array arr of data type spark_t.


spark_t	arr[100];
First we specify the data type, then the name of the array, then the size in square brackets.

Problem : Declare a data type spark_t which consists of an array of 100 integers.


typedef int[100]	spark_t;
Notice the difference between this and the previous problem. In the first problem, we declared a variable and allotted memory for it. In the second one we declared only a data type of which you can later declare variables.

Problem : Prompt the user for an array size and then declare a static array of that size.

This is a trick question. The size of a static array needs to be a constant and cannot be decided at run time; it must be available at compile time. Instead, you would need to allocate dynamic memory.

Follow Us