Chapter 7: One-Dimensional Arrays

• The simple built-in data types of the C
language such as int, float, - are not sufficient
to represent complex data such as lists, tables,
vectors, and matrices.
• Sorting and searching require an entire list of
data to be stored in memory.
• The most convenient way to accomplish this is
to store the data in a one-dimensional array.
One-Dimensional Arrays
• An array is a named sequence of memory locations that is
used to store different values of the same data type.
• Each of the named memory locations is called an element of
the array. The elements of an array are identified by a
subscript or an index (0,1,2,3,4).
• Above, x is the name of the array, containing the elements
x[0], x[1], x[2], x[3], and x[4].
Array Declaration and Initialization
• In C, there are two ways to represent a subscripted variable such as:
i = 0 to 9
The first way it can be represented is as regular variables, as we have been doing so far:
a0, a1, a2, a3, a4, a5, a6, a7, a8, a9
The above are ten different, explicitly named variables.
• However, if you have a large number of named variables, it is not practical to name them as
shown above. Instead, we use subscripts (or array) as shown below:
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]
• The above is a declaration of an array a which holds 10 different items such as int, or float.
• The declaration, initialization of arrays is as follows:
//declare the array
int i; //declare an integer named i
int a[5]; //declare array a: a[0], a[1], a[2], a[3],
for(i = 0; i < 5; i++)//loop to go through array
a[i] = 2; // initialize array
a equal to 2
Arrays Declaration and Initialization
• One-dimensional arrays may be declared individually as follows:
float pressure[20];
float temperature[40];
float velocity[40];
• Or all of the float data types can be declared in one declaration statement as follows:
float pressure[20], temperature[40], velocity[40];
•One-dimensional arrays may be initialized in a declaration statement as follows:
int a[5] = {5, 9, 8, 4, 7}; //where a[0] is 5, a[1] is 9, a[2] is 8, a[3] is 4, and a[4] is 7
• The following example shows the initialization of fewer values than the size of the array:
int a[5] = {5, 9, 8, 4}; //where a[0] is 5, a[1] is 9, a[2] is 8, a[3] is 4, and a[4] is 0
•Notice that the unspecified element a[4] is 0. If a value is not specified, then a zero is assigned.
Arrays Declaration/Initialization
One-dimensional arrays may be initialized using assignment statements as follows:
int a[5];
a[0] = 5;
a[1] = 9;
a[2] = 8;
a[3] = 4;
a[4] = 7;
Initialization is usually quickly done with a for loop as follows:
int i, count [5];
float velocity[10];
for(i = 0; i < 5; i++){
count[i] = 2;
for(i = 0; i < 10; i++){
velocity[i]= 500.75;
Array Input/Output
Sorting algorithms:
• Bubble sort is a simple sorting algorithm that works by
repeatedly stepping through the list to be sorted
• It compares each pair of adjacent items and swaps
them, if they are in the wrong order.
• Every pass, the largest value “bubbles up” to the top.
for(int x=0; x<n; x++) {
for(int y=0; y<n-1; y++)
if( array[y] > array[y+1])
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
H.W. Arrays
1. Compile and run the program on slide 6
- Save it as sampleArrays.c
2. Write a program to:
• Ask the user for 7 temperature and 7 pressure values
for a scientific experiment.
• Store the values in two one-dimensional arrays.
• Identify the highest temperature and pressure.
• Show the highest temperature and pressure to the
• Save your program as tempPressArrays.c

similar documents