• No results found

Arrays in C

N/A
N/A
Protected

Academic year: 2020

Share "Arrays in C"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

ARRAYS

Introduction

In C programming, one of the frequently arising problems is to handle similar types of data. For example: If the user wants to store marks of 100 students. This can be done by creating 100 variables individually but, this process is rather tedious and impracticable. This type of problem can be handled in C programming using arrays.

Definition of an Array An Array

 Is a derived data type,

 Which is a collection of similar data items,

 That are to be stored in continuous memory locations  and all the elements are referred by single name known as array name.

We can declare arrays in two ways. They are  Static array

 Dynamic array

Static Array

Static arrays or fixed length array, the size of the array is known when the programmer is written.

Dynamic Array

Dynamic arrays or variable-length array, the size of the array is not known until the program run.

Advantages of Arrays:

1) Array can store multiple values of same data type.

2) All the elements in array are accessible by using single name (array name). It overcomes the name conflict problem.

3) It is capable of storing many elements at a time.

4) The memory locations of elements in the array are sequential. 5) Sorting and Searching becomes easy.

Disadvantages of arrays:

1) Memory wastage will be there.

2) To delete an element in an array we need to traverse throughout the array. 3) The size of the array is fixed while declaration itself.

4) The size can’t be increased/decreased dynamically.

(2)

An array must be declared and defined before it can be used. Array declaration and definition tells to the compiler about the name of the array, the type of each element, and the size or number of elements in the array.

In fixed-length array, the size of the array is constant and must have a value at compile time.

The declaration format is shown in the following

Syntax for creating an array:

<data-type> <array-name>[size];

Example:

int x[20] ;

In the above declaration, the compiler can know the following.

1) Name of the array is ‘x’.

2) Size i.e., no of elements is 20.

3) Data type of all elements is int.

4) No. of bytes occupied by x is 40 bytes.

Direct Initialization of an array: Syntax:

<data type> <array name> [size] = {no.of elements};

Example:

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

The following shows the memory representation for the above initialized array.

All the elements in an array are individually accessed by using index numbers. The index number of array always starts with ‘0’ and ends with ‘size-1’.

Elements in the array a:

(3)

Types of Arrays:

There are three types of arrays are there. They are

 Single –dimensional array (or) one dimensional array.

 Two-dimensional array.

 Multi dimensional array.

Single Dimensional Array:

An Array which is used to represent and store data in a linear form is called single dimensional array or one dimensional array.

Syntax:

<data type> <array_name> [size]; Example:

int a [3] = {3, 4, 8};

char ch [20] =”c language”;

float ele[3]={45.08, 256.56, 3450.50}; Reading values into an array

To read the elements into a Single Dimensional array we required a ‘for’ loop that repeats until the total number of elements are inserted into the array.

The following coding illustrates this.

For example, consider an integer array with size 5, i.e., int a[5];

for(i=0;i<=4;i++) {

(4)

To print the elements of an array on the console we require a for loop as follows.

for(i=0;i<=4;i++) {

printf(“%3d”,a[i]); }

The following program illustrate the Single Dimensional Arrays in C. Aim: C-Program to read and print an array of elements.

Program:

#include<stdio.h> #include<conio.h> void main() {

int a[10],n,i; clrscr();

printf("\n Enter the size of the array:"); scanf("%d",&n);

printf("\n Enter the elements into the array\n"); for(i=0;i<=n-1;i++)

{

scanf("%d",&a[i]); }

printf("\n The array elements are....\n"); for(i=0;i<=n-1;i++)

{

printf("%3d",a[i]); }

getch(); }

(5)

Two dimensional Array:

An Array which is used to represent and store data in a tabular form is called Two Dimensional Array. This type of arrays is used to represent data in a matrix form.

Syntax:

<data-type> <array_name>[row size][column size];

Example:

int a[3][3];

Memory Allocation:

The following program illustrates the above concept.

Aim: C-Program to read and print a Two-Dimensional Array Program:

#include<stdio.h> #include<conio.h> void main() {

int a[5][5],i,j,r,c; clrscr();

printf("\n Enter the order of the matrix:"); scanf("%d%d",&r,&c);

printf("\n Enter the elements into the array\n\n"); for(i=0;i<=r-1;i++)

{

for(j=0;j<=c-1;j++) {

scanf("%d",&a[i][j]); }

}

printf("\n The elements of the matrix are as follows...\n\n"); for(i=0;i<=r-1;i++)

(6)

{

printf("%3d",a[i][j]); }

printf("\n\n"); }

getch(); }

Output:

Multi Dimensional Arrays:

C programming language allows to create Array of Arrays is known as multi dimensional arrays. (Or) collections of two dimensional arrays are called as multi dimensional array.

For example the declaration form of Three-dimensional array is

data_type Array_name [size1][size2][size3];

Example Programs

1. Write a C program to calculate the sum and Average of given Array Elements.

#include<stdio.h> #include<conio.h> void main() {

int i, n, sum=0, a[20], avg; clrscr();

(7)

{

scanf(“%d”,&a[i]); }

for(i=0;i<n;i++) {

sum=sum+a[i]; }

avg=sum/n;

printf(“the sum & average of given elements is %d \t %d”, sum,avg); getch();

}

Output:

Enter the size of an array 3 Enter Array elements 1 2 3

The sum & average of given elements are 6 2

2. Write a C program to find minimum element in the Array.

#include<stdio.h> #include<conio.h> void main() {

int a[20], size, i ,min; clrscr();

printf(“\n enter the size of an array”); scanf(“%d”, &size);

printf(“\n enter elements into array”); for(i=0;i<size;i++)

scanf(‘%d”,&a[i]); min=a[0];

for(i=0;i<size;i++) {

if(a[i]<min) min=a[i]; }

printf(“\n min element=%d”, min); }

Output:

References

Related documents

Studies I &amp; II provide an empirical test of the theory of collectivity of drinking cultures based on temporal changes in the adult population and in the youth population.

[r]

modest change detected in our sample is similar to Scott, Minichiello, and Browning (1998) who found that younger adults participating in an older adult education program

To accomplish these objectives, ammonia sensitivity and reduction rates for pathogens and model organisms were studied at different ammonia concentrations and treatment

All of the participants were faculty members, currently working in a higher education setting, teaching adapted physical activity / education courses and, finally, were

Inverse modeling of soil water content to estimate the hydraulic properties of a shallow soil and the associated weathered bedrock.. Ayral

Mackey brings the center a laparoscopic approach to liver and pancreas surgery not available at most area hospitals.. JOSHUA FORMAN, MD

All records for LASIK procedures from one single cen- ter without patient identifiers were extracted from the Optical Express electronic medical record system using the