Sei sulla pagina 1di 14

1

Arrays
C Programming
2
Definition
Array is nothing but a group of similar type of variables.
Array is a collection of homogeneous or similar data type elements.
An array is the same variable name with different values.
Why Array?
Write a program to read and display 5 Numbers.





#include<stdio.h>
#include<conio.h>
int main(void){
int a,b,c,d,e;
printf(Enter 5 Numbers);
scanf(%d%d%d%d%d,&a,&b,&c,&d,&e);
printf(%d %d %d %d %d,a,b,c,d,e);
return 0;
}
3
Array Declaration
Array contains set of homogeneous elements under single name.
Array are stored in consecutive memory locations.
The values in an array can be specified as elements or members of array.
The members of an array can be accessed by using index, which is an integer.
The staring element of array can be stored in 0
th
index. Which is also called
base index of array.
To construct one dimensional array system is expective three specification.
Group Name
No of Variables in the group
Type of variables in the group
We can specify these things by using array variable declaration statement
Syntax
Type Array_Variable[Size/range];
Int mrr[100];

4
Array Declaration
Ex : int Arr[5];
Here group name is : Arr
No of Variable : 5
Type of Variable : int
For 5 variables memory location will be allocated in continuous manner
variables in the group are called elements.


Each element is identifying with unique index.
To name the element of the array { ArrayName[Index]
Name of the First Element
Nos[0]=10;
Nos[4]=50
5
Array Initialization
Arrays can be initialized in two ways.
Static initialization of arrays
Dynamic initialization of arrays.
Static Initialization of arrays
data_type array_name[size]={list_of_values};
Example : int arr[10] = {1,2,3,4,5,6,7,8,9,10};
Dynamic Initialization of arrays:
Example : int arr[10];
for(i=0;i<10;i++)
{ printf(\n Enter the values of array);
Scanf(%d,&a[i]);}


6
Types of Arrays
Mainly arrays are classified into three types based on subscript or
indexed used to access the elements of an array.
One Dimensional array (only one index used to access element)
Two dimensional array (two indices are used)
Multi dimensional array (more than two indices are used)


7
One Dimensional Arrays

One Dimensional array are also called as single-subscripted
variable.
Declaration:
int a[10];
char ch[20];
float mw[5];
It can be used by any data type.
Initialization of arrays
char character[6] = {'a','e','i','o','u','\0'};
char name[7] = ramesh;
Every character array ends with NULL


Array Vowels
a
e
i
o
u
\0
8
Example on ID array
Write a program to display 5 numbers.
#include <stdio.h>
main()
{
int arr[5],i,n; /* arr[5] array stores 5 integer elements */
printf (\n Enter the size of array);
scanf(%d, &n); /* specifies the no. Of elements. Here n = 5 */
printf(Enter the elements in array); /* dynamic initialization */
for (i =0 ; i < n ; i++)
scanf(%d, &arr[i]); /* reading integer and stores in corresponding
locations like arr[0],arr[1]...arr[4] */
printf (\n The elements in array are);
for(i = 0; i < 5; i++)
printf(%d\n,arr[i]); /* it prints the array elements on output screen */
getch();
return ;
}

9
Example on ID array
Write a program to copy one set of values into another set.
#include <stdio.h>
main()
{
int a[10],b[10],i,n; /* a[10] & b[10] arrays stores 10 integer elements */
printf (\n Enter the size of array);
scanf(%d, &n); /* specifies the no. Of elements. Here n <= 10 */
printf(Enter the elements in array); /* dynamic initialization */
for (i =0 ; i < n ; i++)
scanf(%d, &a[i]); /* reading integer and stores in corresponding locations like
arr[0],arr[1]...arr[4] */
for (i = 0; i< n; i++) /* logic to copy the one set of elements into another set */
b[i] = a[i];
printf (\n The elements in array are);
for(i = 0; i < n; i++)
printf(%d\n,b[i]); /* it prints the array elements on output screen */
getch(); return ; }

10
Example on ID array
Write a program to search a element in given set. If not display error.
main()
{
int a[10],x,i,n; /* a[10] arrays stores 10 integer elements */
printf (\n Enter the size of array);
scanf(%d, &n); /* specifies the no. Of elements. Here n <= 10 */
printf(Enter the elements in array); /* dynamic initialization */
for (i =0 ; i < n ; i++)
scanf(%d, &a[i]); /* reading integer and stores in corresponding locations like
arr[0],arr[1]...arr[4] */
printf(\n Enter the search element:);
scanf(%d,&x);
for (i = 0; i< n; i++) /* logic to copy the one set of elements into another set */
{ if (a[i] == x) /* logic to searching an element in array */
{ printf (\n The element is found at positon %d, i);
exit(0); } }
printf(\n the element is not in the given list); }
11
Example on ID array
Write a program to display name using character array

#include <stdio.h>
main()
{
char ch[20]="VIT UNIVERSITY";
int i=0;
printf("\n the name is ");
printf("%s",ch);
gethch();
return ;
}


12
Example on ID array
Write a program to get the sum of elements in array
#include <stdio.h>
main()
{
int a[10],i,n;
int sum = 0;
printf("\n Enter the size ");
scanf("%d",&n);
printf("\n Enter values in array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum = sum + a[i];
printf("\n the total sum is %d",sum); }


13
Two Dimensional Arrays

In 2D arrays, the elements of arrays can be accessed by using
two subscripted values.
2D arrays can be used for applications related to tables.
Used in matrices
Used by any data type.
Declaration
Syntax data_type array_name[row_size][column_size];
Example int a[3][4];
Total elements in this array a is 3*4 = 12

14
Two Dimensional Arrays
In 2D arrays, the elements of arrays can be accessed by using two
subscripted values.
Initialization of 2D arrays
Static Initialization :
int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
int arr[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
Dynamic Initialization is
for(i =0 ;i < 3 ;i ++)
{
for(j = 0; j < 4; j++)
{
scanf(%d, &a[i][j]); } }

Potrebbero piacerti anche