Arrays
Arrays
Presilla R
Assistant Professor
ARRAYS
An array is a collection of elements of the same type that are referenced by a
common name.
"We have a list of 1000 students' marks of an integer type. If using the basic
data type (int), we will declare something like the following…"
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …,
studMark998, stuMark999, studMark1000;
…
…
return 0;
}
By using an array, we just declare like this,
Int studMark[1000];
This will reserve 1000 contiguous memory locations for
storing the students’ marks.
Graphically, this can be depicted as in the following figure.
This absolutely has simplified our declaration of the
variables.
array_element_data_type array_name[array_
size];
In this statement, the array character can store up to 30 characters with the
first character occupying location cName[0] and the last character
occupying cName[29].
So, take note the difference between the array size and subscript/index
terms.
• Examples of the one-dimensional array declarations,
• int xNum[20], yNum[50];
• float fPrice[10], fYield;
• char chLetter[70];
• The first example declares two arrays named xNum and yNum of type int.
Array xNum can store up to 20 integer numbers while yNum can store up
to 50 numbers.
•
• The second line declares the array fPrice of type float. It
• can store up to 10 floating-point values.
•
• fYield is basic variable which shows array type can be declared together
with basic type provided the type is similar.
•
• The third line declares the array chLetter of type char. It can store a
string up to 69 characters.
•
• Why 69 instead of 70? Remember, a string has a null terminating
character (\0) at the end, so we must reserve for it.
ARRAY INITIALIZATION
An array may be initialized at the time of declaration.
For example:
Int idNum[7] = {1, 2, 3, 4, 5, 6, 7};
float fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
Char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
The first line declares an integer array idNum and it immediately assigns the values 1,
2, 3, ..., 7 to idNum[0], idNum[1], idNum[2],..., idNum[6] respectively.
Similarly the third line assigns the characters 'a' to chVowel[0], 'e' to chVowel[1], and
so on. Note again, for characters we must use the single apostrophe/quote (') to
enclose them.
Also, the last character in chVowel is NULL character ('\0').
Initialization of an array of type char for holding strings
may take the following form,
chararray_name[size] = "string_lateral_constant ";
C compiler automatically creates an array which is big enough to hold all the
initializer.
RETRIEVING ARRAY ELEMENTS
For example:
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[0],Arr[2]);
Output: 1 5
ARRAY EXAMPLE
• Strings (i.e., character arrays) are handled
somewhat differently,. In particular, when a string
constant is assigned to an external or a static
character array as a part of the array definition,
the array size specification is usually omitted.
• The proper array size will be assigned
automatically. This will include a provision for the
null character \O, which is automatically added at
the end of every string
• Write a c program that reads in a one
dimensional character array, converts all of
the elements to uppercase, and then displays
the converted array.
PROCESSING AN ARRAY
• Single operations which involve entire arrays are
not permitted in C. Thus, if a and b are similar
arrays (i.e., same data type, same dimensionality
and same size), assignment operations,
comparison operations, etc. must be carried out
on an element-by-element basis.
• This is usually accomplished within a loop, where
each pass through the loop is used to process one
array element. The number of passes through the
loop will therefore equal the number of array
elements to be processed.
Program to enter and display the array elements
• #include <stdio.h>
• #define SIZE 5
• int main() {
• int numbers[SIZE];
• return 0;
• }
• Write a program to calculate the average of n
numbers, then compute the deviation of each
number about the average
MULTIDIMENSIONAL ARRAYS
• Multidimensional arrays are defined in much
the same manner as one-dimensional arrays,
except that a separate pair of square brackets
is required for each subscript.
• Thus, a two-dimensional array will require two
pairs of square brackets, a three-dimensional
array will require three pairs of square
brackets, and so on.
• storage-class data- type array[ expression I] [
expression 2] . . . [ expression n] ;