Arrays 1D
Arrays 1D
ARRAYS
Introduction:
C data types
Arrays are referred to as Structured Data types because; they can be used to represent data values that
have a structure of some sort.
Array Definitions: -
An Array can be defined as a structured Data type consisting of a group of elements of the same
type.
Properties of Array: -
1. The individual data items can be characters, integers, floating point numbers.
2. All the elements of an array will be of the same data type and will have the same storage class.
3. Each element of an array is referred to by specifying an array name followed by one or more
subscripts.
a. Each subscript is enclosed within square brackets.
b. The subscript gives the position of an array element.
c. Each subscript must be a positive integer.
d. The value of each subscript can be expressed as an integer constant, integer variable or
a complex integer expression.
4. An Array index begins at 0(zero).
Eg: -int marks [10]; is the declaration, it reserves 10 consecutive storage locations to hold 10
array elements of type int.
i.e.., marks[0], marks[1],......marks[9].
Arrays, which have elements with the single subscript, are known as 1-Dimensional arrays.
Example: -
Consider, int marks [5];
The array name “marks” can be used to store the marks of 5 students as follows: -
40 45 50 41 38
marks[0] marks[1] marks[2] marks[3] marks[4]
Marks[0] = 40;
Marks[1] = 45;
Marks[2] = 50;
Marks[3] = 41;
Marks[4] = 38;
General form:
type var_name[size];
#define MAX_STUD 50
int marks[MAX_STUD];
Initialization of Arrays: -
1. Compilation time: -
General Format: -
type array_name[size] = { list of values };
Meaning : - An array ‘marks’ reserves 5 contiguous memory locations to store the list of values given
within the curly braces. It is represented as shown below:
40 Marks[0]
20 Marks[1]
30 Marks[2]
35 Marks[3]
50 Marks[4]
Eg: -
Suppose, we have,
int marks[5]={10,11,12};
Here, an array will reserve 5 contiguous memory locations and the values 10, 11, 12 will be initialized as
shown in the diagram.
Marks[0] 10
Marks[1] 11
Marks[2] 12
Marks[3] 0
Marks[4] 0
The leftover locations will be filled with zeroes. Since the data type is integer.
If the data type is character, then the leftover spaces will be filled or initialized to null characters.
Eg 3: -
char abc[10];
‘W’
‘E’
‘L’
‘C’
‘O’
‘M’
‘E’
‘\0’
If it’s character type, then always the last character will be the null character.
Eg 4: -
Say, 6 data items, this is invalid. You will get abnormal terminations sometimes, because, the size
declared is smaller than the elements initialized.
Whenever we want to initialize large number of values, we can follow the Runtime initializing.
Eg: - if we have 100 elements, and for 1st 50 values we want to initialize value 0 and for the next 50
values, the value 1 to be initialized. This is shown as follows: -
for(i=0;i<100;i++)
{
if(i<50)
sum[i]=0.0;
else
sum[i]=1.0;
}
Examples: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a[ ], i, n;
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("\n\nEnter the array elements : \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\n\nThe array elements are : \n");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, n, b[10], c[10];
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("Enter the elements of array A : \n\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
#include<stdio.h>
#include<conio.h>
void main()
{
int fibo[50], i, n;
clrscr();
printf("Enter the number of terms : ");
scanf("%d", &n);
fibo[0] = 0;
fibo[1] = 1;
for(i=2; i<n; i++)
fibo[i] = fibo[i-1] + fibo[i-2];
printf("Fibonacci Numbers are : \n\n");
for(i=0; i<n; i++)
printf("%d\n", fibo[i]);
getch();
}