0% found this document useful (0 votes)
34 views7 pages

Arrays 1D

Arrays allow storing of multiple values of the same type sequentially in memory. They have several key properties: 1) Elements are of the same data type. 2) Elements have indices from 0 to size-1. 3) Elements are stored in contiguous memory locations. One-dimensional arrays have a single subscript to identify elements. Arrays can be initialized during compilation with an initializer list or at runtime by iterating through elements.

Uploaded by

Rishik Puneet m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Arrays 1D

Arrays allow storing of multiple values of the same type sequentially in memory. They have several key properties: 1) Elements are of the same data type. 2) Elements have indices from 0 to size-1. 3) Elements are stored in contiguous memory locations. One-dimensional arrays have a single subscript to identify elements. Arrays can be initialized during compilation with an initializer list or at runtime by iterating through elements.

Uploaded by

Rishik Puneet m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to C programming Arrays

ARRAYS

Introduction:

C has rich set of data types. i.e..,


 Fundamental Data types
 Derived Data types
 User-defined Data types

C data types

Derived Data types Primary or User – defined Data


Fundamental data types
types
-Arrays -Integer types - Structures
-Functions -Floating Types - Unions
-Pointers -Character types - Enumerations

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.

 An array is a fixed-size sequenced collection of elements of the same data type.

 An Array can be used to represent a list of numbers or a list of names.

 The elements of an array are called the data items.

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

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

5. Array elements are always stored in sequential memory locations.

6. The number of subscripts determines the dimension of an array.


Based on the subscripts, arrays are classified into: -
- One - Dimensional,
- Two - Dimensional,
- Multi - Dimensional, (3 and more).
One - Dimensional Array: -

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

The above values can be assigned as follows: -

Marks[0] = 40;

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

Marks[1] = 45;
Marks[2] = 50;
Marks[3] = 41;
Marks[4] = 38;

Declaration of One-Dimensional Array : -

General form:
type var_name[size];

Where, type is the data type.


var_name is the array-name, which follows the rules of an identifier.
size, is the size of the array.

Eg: - int a[10];


Here, an array name is ‘a’. It reserves 10 contiguous memory locations which is of type integer.
 The size of the array should be always an integer.

 Consider the following example: -


int max_stud=50;
int marks[max_stud];
This declaration is not allowed, since size of an array is not integer constant.
Hence, this can be rewritten using the macro definition, which is as follows: -

#define MAX_STUD 50
int marks[MAX_STUD];

Initialization of Arrays: -

Arrays can be initialized in 2 stages.

 During Compilation Time.


 During Run-Time.

1. Compilation time: -

General Format: -
type array_name[size] = { list of values };

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

Where, type is the datatype


 array_name is the name of the array.
 “size” is the size of the array.
 “List of values “ is the values that should be initialized for the array. (Data elements)

Eg: - int marks[5]={40, 20, 30, 35, 50};

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]

i.e.., Marks[0] = 40;


Marks[1] = 20;
Marks[0] = 30;
Marks[0] = 35;
Marks[0] = 50;

The same thing can be rewritten as follows: -


int Marks[ ]={40, 20, 30, 35, 50};

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

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

if the data given is(say) WELCOME,

‘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: -

Suppose we have an example: -

int marks[5]={10, 20, 30, 40, 50, 60};

Say, 6 data items, this is invalid. You will get abnormal terminations sometimes, because, the size
declared is smaller than the elements initialized.

2. Runtime Initialization of an Array : -

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

Also, we can initialize or input values through keyboard.


i.e.., scanf(“%d%d%d”, &a*0+, &a*1+, &a*2+);
The values for the locations, a[0], a[1], a[2] can be given during runtime.

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

Examples: -

1. Program to write the data into array and print them.

#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();
}

2. Program to find sum, average and smallest of ‘n’ numbers.

3. Program to add two 1-D arrays.

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

Dr. Bharathi P. T Department of CS&E


Introduction to C programming Arrays

printf("\n\nEnter the elements of array B : \n\n");


for(i=0; i<n; i++)
scanf("%d", &b[i]);
clrscr();
for(i=0; i<n; i++)
c[i] = a[i] + b[i];
printf("The resultant array is : \n\n");
for(i=0; i<n; i++)
printf("%d\n", c[i]);
getch();
}

4. Program to generate first N Fibonacci numbers using arrays.

#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();
}

Dr. Bharathi P. T Department of CS&E

You might also like