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

Arrays

The document discusses arrays in C programming. It defines what an array is, how to declare and initialize arrays, how to access array elements using indexes, and how to perform operations on entire arrays using loops. It also covers multidimensional arrays and provides examples of declaring and using arrays.

Uploaded by

Lahari Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Arrays

The document discusses arrays in C programming. It defines what an array is, how to declare and initialize arrays, how to access array elements using indexes, and how to perform operations on entire arrays using loops. It also covers multidimensional arrays and provides examples of declaring and using arrays.

Uploaded by

Lahari Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT-3

Presilla R
Assistant Professor
ARRAYS
An array is a collection of elements of the same type that are referenced by a
common name.

Compared to the basic data type (int, float) it is an


aggregate or derived data type.

All the elements of an array occupy a set of contiguous memory locations.

Why need to use array type?

Consider the following issue:

"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 studMark0, studMark1, ...studMark999


Can you imagine how long we have to write the
declaration part by using normal variable declaration?

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.

We can use index or subscript to identify each element


or location in the memory.

Hence, if we have an index of j, studMark[j] would refer


to the jth element in the array of studMark.

For example, studMark[0] will refer to the first element


of the array.
Thus by changing the value of j, we could refer to any
element in the array.
A single or one dimensional array declaration
has the following form,

array_element_data_type array_name[array_
size];

Here, array_element_data_type defines the base type of


the array, which is the type of each element in the array.
array_name is any valid C identifier name that obeys the
same rule for the identifier naming.
array_size defines how many elements the array
will hold.
For example, to declare an array of 30 characters, that construct a people
name, we could declare,
char cName[30];
Which can be depicted as follows,

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].

Note that the index runs from 0 to 29.


In C, an index always starts from 0 and ends
with array's (size-1).

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.

Initialization of an array may take the following form,


Type array_name[size] = {a_list_of_value};

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.

The second line assigns the values 5.6 to


fFloatNum[0], 5.7 to fFloatNum[1], and so on.

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 ";

For example, the array chVowel in the previous example


could have been written more compactly as follows,
charchVowel[6] = "aeiou";

When the value assigned to a character array is a string (which must be


enclosed in double quotes), the compiler automatically supplies the NULL
character but we still have to reserve one extra place for the NULL.
For unsized array (variable sized), we can declare as
follow,
char chName[ ] = "Mr. Dravid ";

C compiler automatically creates an array which is big enough to hold all the
initializer.
RETRIEVING ARRAY ELEMENTS

If you want to retrieve specific element then then


you have to specify not only the array or variable
name but also the index number of interest.

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];

• printf("Enter %d integers:\n", SIZE);


• for (int i = 0; i < SIZE; i++) { Enter 5 integers:
• scanf("%d", &numbers[i]); 1
2
• }
3
4
• printf("Array elements: "); 5
• for (int i = 0; i < SIZE; i++) { Array elements: 1 2 3 4 5
• printf("%d ", numbers[i]);
• }
• printf("\n");

• 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] ;

• where storage -class refers to the storage class


of the array, data- type is its data type, array is
the array name, and expression 7, expression
2, . . ., expression n are positive-valued integer
expressions that indicate the number of array
elements associated with each subscript.
float table[50][50];
char page[24][80];
static double records[100][66][255];
static double records[L][M][N];
Write a c program to Add Two Tables
of Numbers

Write a c program to perform matrix


multiplication

You might also like