0% found this document useful (0 votes)
15 views6 pages

Arrayes

Arrays

Uploaded by

sencermalik144
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)
15 views6 pages

Arrayes

Arrays

Uploaded by

sencermalik144
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/ 6

1

ARRAYS
INTRODUCTION
Suppose we need to find out the average of 100 integer numbers entered by user.
In C,for this we have two ways to do this.
Ø Define 100 variables with int data type and then perform 100 scanf()
operations to store the entered values in the variables and then at last
calculate the average of them.
Ø Have a single integer array to store all the values, loop the array to store all
the entered values in array and later calculate the average.
Which solution is better according to you? Obviously the second solution, it is
convenient to store same data types in one single variable and later access them
using array index.
Definition
An array is defined as finite ordered collection of homogenous data, stored in
contiguous memory locations
Here the words,

• finite means data range must be defined.


• ordered means data must be stored in continuous memory addresses.
• homogenous means data must be of similar data type.
2

Declaration:
int num[35]; /* An integer array of 35 elements */ Similarly an array can be of any
char ch[10]; /* An array of characters for 10 elements */ data type such
as double, float, short etc.

Initialization of an array

We can also initialize the array during declaration like this

int arr[5] = {1, 2, 3, 4 ,5};


int arr[] = {1, 2, 3, 4, 5};
Un-initialized array always contain garbage values.

Input data into the array

for (x=0; x<6;x++) Here we are iterating the array from 0 to 5 .


{ Inside the loop we are displaying a message
printf("Enter number %d \n", (x+1)); to the user to enter the values. All the input
scanf("%d", &num[x]); values are stored in the corresponding array
} elements using scanf function.

Reading out data from an array:

for (x=0; x<4;x++) Suppose, if we want to display the elements


{ of the array then we can use the for loop in C
printf("num[%d]\n", num[x]); like this.
}
3

Accessing element of an array in C

int mydata[20]; In general arr[n-1] can be used to


mydata[0] /* first element of array mydata*/ access nth element of an array. where
mydata[19] /* last (20th) element of array mydata*/
n is any integer number.

Program:01
#include <stdio.h> Output:
int main() Enter number 1
{ 10
int avg = 0; Enter number 2
int sum =0; 10
int x=0; Enter number 3
int num[4]; /* Array- declaration – length 4*/ 20
for (x=0; x<4;x++) Enter number 4
{ 40
printf("Enter number %d \n", (x+1)); Average of entered
scanf("%d", &num[x]); number is: 20
}
for (x=0; x<4;x++)
{
sum = sum+num[x];
}

avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}

Two dimensional (2D) arrays:

An array of arrays is known as 2D array. The two dimensional (2D) array in C


programming is also known as matrix.
A matrix can be represented as a table of rows and columns..
4

DECLARATION:
int abc[2][2] = {1, 2, 3 ,4 } /* Valid declaration*/ Explanation
Things that you must con
int abc[][2] = {1, 2, 3 ,4 } /* Valid declaration*/ We already know, when we initialize a
normal array (or you can say one dimensional
int abc[][] = {1, 2, 3 ,4 } /* Invalid because you must specify array) during declaration, we need not to
second dimension*/
specify the size of it. However that’s not the
case with 2D array, you must always specify
int abc[2][] = {1, 2, 3 ,4 }/* Invalid declaration – because you
the second dimension even if you are
must specify second dimension*/
specifying elements during the declaration.
Let’s understand this with the help of few
examples –

Initialization of 2D Array

There are two ways to initialize a two Dimensional arrays during declaration

1.)int disp[2][4] = { Although both the above declarations are


{10, 11, 12, 13}, valid, I recommend you to use the first
{14, 15, 16, 17} method as it is more readable, because you
}; can visualize the rows and columns of 2d
OR array in this method.

2.)int disp[2][4] = { 10, 11, 12, 13, 14,


15, 16, 17};

How to store user input data into 2D array:

#include<stdio.h> We can calculate how many elements a two


int main(){ dimensional array can have by using this formula:
int abc[5][4]; The array arr[n1][n2] can have n1*n2 elements. The
int i, j; array that we have in the example below is having
for(i=0; i<5; i++) { the dimensions 5 and 4. These dimensions are
for(j=0;j<4;j++) { known as subscripts. So this array has first subscript
printf("Enter value for abc[%d][%d]:", i, j); value as 5 and second subscript value as 4.
scanf("%d", &abc[i][j]); So the array abc[5][4] can have 5*4 = 20 elements.
5

}
} To store the elements entered by user we are using
return 0; two for loops, one of them is a nested loop. The
} outer loop runs from 0 to the (first subscript -1) and
the inner for loops runs from 0 to the (second
subscript -1). This way the the order in which user
enters the elements would be abc[0][0], abc[0][1],
abc[0][2]…so on.

Conceptually we can visualize the above array as in below pic

However the actual representation of this array in memory would be something


like this:
6

You might also like