1D Arrays IV2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Informatika

17.03.2014.
IV - 2
Arrays
Array is a data structure, which provides the facility to store a collection of data of same type
under single variable name. Just like the ordinary variable, the array should also be declared
properly. The declaration of array includes the type of array that is the type of value we are going
to store in it, the array name and maximum number of elements.
The type may be any valid type supported by C. Array names, like other variable
names, can contain only letter, digit and underscore characters. Array names cannot begin with a
digit character. I.e., The rule for giving the array name is same as the ordinary variable. The size
should be an individual constant. To refer to a particular location or element in the array, we
specify the name of the array and the index of the particular element in the array. The index
specifies the location of the element in the array. The array index starts from zero. The maximum
index value will be equal to the size of the array minus one. The array index can be an integer
variable for an integer constant.
One dimensional array in c programming
language : ID array

An array can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on. In this topic, we will
discuss 1-Dimensional arrays in C Programming Language. So lets start with 1-Dimensional
array.
The declaration form of one-dimensional array is Data_type array_name [size];
The following declares an array called numbers to hold 5 integers and sets the first and last
elements. C arrays are always indexed from 0. So the first integer in numbers array is
numbers[0] and the last is numbers[4].
int numbers [5];
numbers [0] = 1; // set first element
numbers [4] = 5; // set last element
This array contains 5 elements. Any one of these elements may be referred to by giving the
name of the array followed by the position number of the particular element in square brackets
([]). The first element in every array is the zeroth element.Thus, the first element of array
numbersis referred to as numbers[ 0 ], the second element of array numbersis referred to as
numbers[ 1 ], the fifth element of array numbersis referred to as numbers[ 4 ], and, in general,
the n-th element of array numbersis referred to as numbers[ n - 1 ].
Example:

It's a very common error to try to refer to non-existent numbers[ 5], element. C does not do
much hand holding. It is invariably up to the programmer to make sure that programs are free
from errors. This is especially true with arrays. C does not complain if you try to write to
elements of an array which do not exist!
For example: If you wrote:
numbers[ 5 ], = 6;

C would happily try to write 6 at the location which would have corresponded to the sixth
element, had it been declared that way. Unfortunately this would probably be memory taken up
by some other variable or perhaps even by the operating system. The result would be either:
The value in the incorrect memory location would be corrupted with unpredictable
consequences.
The value would corrupt the memory and crash the program completely!
The second of these tends to be the result on operating systems with proper memory
protection. Writing over the bounds of an array is a common source of error. Remember that the
array limits run from zero to the size of the array minus one.
Out of bounds access
I would be doing well, that really shows the range of valid indices, please check often. 10 When
the number of elements, all the indices of 10 or more is illegal. Also, whether any size of
negative indices is also illegal.
These errors are detected at compile time is not. It is an error at run time. However, there is no
guarantee that the error reliably. Sometimes works correctly (seems to be moving.) This is the
"luck ran" instead of "bad luck worked" so, please, as no doubt.
The best way to see these principles is by use of an example, so load the program and display
it on your monitor.
#include <stdio.h>
#include <conio.h>
int main( )
{
char name[7]; /* define a string of characters */
name[0] = 'A';
name[1] = 's';
name[2] = 'h';
name[3] = 'r';
name[4] = 'a';
name[5] = 'f';
name[6] = '\0'; /* Null character - end of text */
name[7] = X;
clrscr();
printf("My name is %s\n",name);
printf("First letter is %c\n",name[0]);
printf("Fifth letter is %c\n",name[4]);
printf("Sixth letter is %c\n",name[5]);
printf("Seventh letter is %c\n",name[6]);
printf("Eight letter is %c\n",name[7]);
getch();
return 0;
}
The elements of an array can also be initialized in the array declaration by following the
declaration with an equal sign and a comma-separated list (enclosed in braces) of initializers.
The following program initializes an integer array with five values and prints the array.
#include <stdio.h>
#include <conio.h>
int main()
{
int numbers[]={1,2,3,4,5};
int i;
clrscr();
printf("Array elements are\n");
for(i=0; i<= 4; i++)
printf("%d\n",numbers[i]);
getch();
return 0;
}
If there are fewer initializers than elements in the array, the remaining elements are initialized
to zero. For example, the elements of the array n could have been initialized to zero with the
declaration
int n[ 10 ] = { 0 };
which explicitly initializes the first element to zero and initializes the remaining nine elements to
zero because there are fewer initializers than there are elements in the array. It is important to
remember that arrays are not automatically initialized to zero. The programmer must at least
initialize the first element to zero for the remaining elements to be automatically zeroed. This
method of initializing the array elements to 0 is performed at compile time for static arrays and at
run time for automatic arrays.
There are more complex uses of arrays, which I will address later along with pointers.

EXAMPLES:
Let us understand this with the help of an example.
void main(){
int arr[3],i;
printf(Enter 3 values\n);
for(i=0;i<3;i++)


void main(){
int arr[3], i;
printf(Enter 3 values\n);
for(i=0; i < 3; i++)
{
scanf(%d,&arr[i])
}
printf(The entered values are:\n);
for(i=0;i<10;i++)
{
printf(%d\t,arr[i])
}
}
Explanation:
int arr[3] statement declares an array capable of holding 3 integer values. The first value can be
accessed using arr[0]. Similarly second value can be accessed using arr[1]. Note that the number
enclosed in [] is called index value. The index value of array in C always starts from 0.
So,
First element > arr[0]
Second element > arr[1]
Third element > arr[2]
nth element > arr[n-1]
When an array is declared, a garbage value is stored in it. Thus, accessing the value of array
elements without defining it will give garbage value.
for(i=0;i<3;i++)
{
scanf(%d,&arr[i])
}


for(i=0;i<3;i++)
{
scanf(%d,&arr[i])
}
The above for loop accepts the value from the user and stores it in an array. The array that was
declared before is getting defined here.
Thus,
First value corresponding to 1st iteration of for loop is stored in the element arr[0]
Second value corresponding to 2nd iteration of for loop is stored in the element arr[1]
Third value corresponding to 3rd iteration of for loop is stored in the element arr[2]
for(i=0;i<3;i++)
{
printf(%d\t,arr[i])
}


for(i=0;i<3;i++)
{
printf(%d\t,arr[i])
}
The above for loop displays the value stored in the array.
Thus,
1st iteration displays value stored in arr[0]
2nd iteration displays value stored in arr[1]
3rd iteration displays value stored in arr[2]
In the above program, we have used data type int for the array. Hence it is called integer array.
We can also declare array as float. But then it will contain float values. An array element can
only hold one type of data i.e. either integer or float or character.
Consider this program,
void main(){
int arr[3];
arr[0] = 1;
arr[1] = 2;

1
2
3
4
5
6
void main(){
int arr[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
}
Let us suppose that base address of this array is 6000. So value 1 is stored at memory location
6000. Since it is integer array, 16 bit C compiler will assign 2 bytes of memory for its storage.
Hence next value i.e. 2 will be stored at location 6002. Similarly, 3 will be stored at location
6004.
An array can be defined as follows:
int arr[3]={1,2,3};
i.e., arr[0] > 1
arr[1] > 2
arr[2] > 3
An array can also be defined as:
int arr[]={1,2,3}

You might also like