ARRAYS IN C LANGUAGE
Aditya Silver Oak Institute of Technology
• Name : Surbhi R Yadav
• Branch : Computer Engineering
• Enroll. No.: 141200107065
• Subject Name : Computer Programming and
Utilization
• Date : 03/03/2015
Contents
• Need of an Arrays
• Introduction
• Types of Arrays
• Single dimensional Arrays
• Two dimensional Arrays
• Multi-dimensional Arrays
Need of an Array
• Till now, we have been storing data in simple
variables.
• Although storing data of a large number of
people is quite difficult with the process we use
still.
• To store this large data, the developers
developed the concept of arrays in C language
Introduction
• An array is a collection of similar data elements
with same data type
• The elements of the array are stored in
consecutive memory locations and are
referenced by an index.
• Index indicates an ordinal number of the
elements counted from beginning of the array.
Some examples where the concept of the array can be
used are :
• List of temperatures recorded on every day of month
• List of employees in a company
• List of students in a class
• List of products sold
• List of customers
Arrays are of three types :
• Single dimensional Array
• Two-dimensional Array
• Multi-dimensional Array
Single dimensional Array
• The one dimensional array or single dimensional
array in C language is the simplest type of array
that contains only one row for storing data.
• One dimensional array is like a list of some
items, which can be stored by using only one
dimension.
Declaration of Array
• An array must be declared before being used
• Declaring an array means specifying three things
o Data type-what kind of values it can store, like
int, char, float, double
oName-to identify the array
oSize-the maximum number of values that array
can hold
oSyntax : datatype variablename[size] ;
oEx : int marks[10];
Initialization of Arrays
• Elements of the array can also be initialized at
the time of declaration as in the case of every
variable.
• Arrays are initialized as :
datatype array_name[size]={list of values};
Ex : int a[5]={90,82,78,95,88};
a[0] a[1] a[2] a[3] a[4]
90 82 78 95 88
Operations that can be performed on
Arrays
There are number of operations that can be
performed on arrays. These operations include :
• Transversal
• Insertion
• Search
• Deletion
• Merging
• Sorting
Example of One Dimensional Array
• /*Write a program to get n numbers and find out sum and average of numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]; //array of size 10
int i,n;
float avg,sum=0;
clrscr();
printf(“Give the values of n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Give numbern”);
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
avg=sum/n;
printf(“Array elements are :n);
for(i=0;i<n;i++)
printf(“nSum=%f Average= %6.2fn”,sum, avg);
}
Two Dimensional Array
• If one dimensional array is like a list, two
dimensional array is like a table.
• A two dimensional array is specified using two
index where one denotes row and another one
denotes column.
• C considers the two dimensional array as an
array of a one dimensional array.
Declaration of Two Dimensional Array
• Similar to one dimensional arrays, two
dimensional arrays must be declared before
being used.
• A two dimensional array is declared as :
data_type array_name[rowsize][columnsize];
Ex :
int marks[3][5];
Initialization of Two-Dimensional Array
• A two-dimensional array is initialized in the same
way as a one-dimensional array.
• Ex : int a[2][3] = {90,87,78,68,62,71};
or
int a[2][3] = {{90,87,78},{68,62,71}};
a[0][0] a[0][1] a[0][2]
90 87 78
68 62 71
a[1][0] a[1][1] a[1][2]
• /*Write a program to read 3*3 matrix and find maximum number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,max;
clrscr();
printf(“How matrix row wisen”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“a[%d][%d]= “,i,j);
scanf(“%d”,&[i][j]);
}
printf(“n”);
}
max=a[0][0];
for(i=0;i<3;i++)
{
for(j=o;j<3;j++)
{
if(max<a[i][j])
max=a[i][j];
}
}
printf(“Max number is = %d”,max);
getch();
}
Multi-dimensional Array
• In general, we can have n-dimensional array.
• If n>=2, then we call that array as multi-
dimensional array. For example, suppose we
want to store 5 students marks information for
two different exams carried out in the term for 4
different subjects, this data can be stored by
using 3-dimensional array like,
int marks[5][2][4];
Arrays In C Language

Arrays In C Language

  • 1.
    ARRAYS IN CLANGUAGE
  • 2.
    Aditya Silver OakInstitute of Technology • Name : Surbhi R Yadav • Branch : Computer Engineering • Enroll. No.: 141200107065 • Subject Name : Computer Programming and Utilization • Date : 03/03/2015
  • 3.
    Contents • Need ofan Arrays • Introduction • Types of Arrays • Single dimensional Arrays • Two dimensional Arrays • Multi-dimensional Arrays
  • 4.
    Need of anArray • Till now, we have been storing data in simple variables. • Although storing data of a large number of people is quite difficult with the process we use still. • To store this large data, the developers developed the concept of arrays in C language
  • 5.
    Introduction • An arrayis a collection of similar data elements with same data type • The elements of the array are stored in consecutive memory locations and are referenced by an index. • Index indicates an ordinal number of the elements counted from beginning of the array.
  • 6.
    Some examples wherethe concept of the array can be used are : • List of temperatures recorded on every day of month • List of employees in a company • List of students in a class • List of products sold • List of customers Arrays are of three types : • Single dimensional Array • Two-dimensional Array • Multi-dimensional Array
  • 7.
    Single dimensional Array •The one dimensional array or single dimensional array in C language is the simplest type of array that contains only one row for storing data. • One dimensional array is like a list of some items, which can be stored by using only one dimension.
  • 8.
    Declaration of Array •An array must be declared before being used • Declaring an array means specifying three things o Data type-what kind of values it can store, like int, char, float, double oName-to identify the array oSize-the maximum number of values that array can hold oSyntax : datatype variablename[size] ; oEx : int marks[10];
  • 9.
    Initialization of Arrays •Elements of the array can also be initialized at the time of declaration as in the case of every variable. • Arrays are initialized as : datatype array_name[size]={list of values}; Ex : int a[5]={90,82,78,95,88}; a[0] a[1] a[2] a[3] a[4] 90 82 78 95 88
  • 10.
    Operations that canbe performed on Arrays There are number of operations that can be performed on arrays. These operations include : • Transversal • Insertion • Search • Deletion • Merging • Sorting
  • 11.
    Example of OneDimensional Array • /*Write a program to get n numbers and find out sum and average of numbers*/ #include<stdio.h> #include<conio.h> void main() { int a[10]; //array of size 10 int i,n; float avg,sum=0; clrscr(); printf(“Give the values of n”); scanf(“%d”,&n); for(i=0;i<n;i++) { printf(“Give numbern”); scanf(“%d”,&a[i]); sum=sum+a[i]; } avg=sum/n; printf(“Array elements are :n); for(i=0;i<n;i++) printf(“nSum=%f Average= %6.2fn”,sum, avg); }
  • 12.
    Two Dimensional Array •If one dimensional array is like a list, two dimensional array is like a table. • A two dimensional array is specified using two index where one denotes row and another one denotes column. • C considers the two dimensional array as an array of a one dimensional array.
  • 13.
    Declaration of TwoDimensional Array • Similar to one dimensional arrays, two dimensional arrays must be declared before being used. • A two dimensional array is declared as : data_type array_name[rowsize][columnsize]; Ex : int marks[3][5];
  • 14.
    Initialization of Two-DimensionalArray • A two-dimensional array is initialized in the same way as a one-dimensional array. • Ex : int a[2][3] = {90,87,78,68,62,71}; or int a[2][3] = {{90,87,78},{68,62,71}}; a[0][0] a[0][1] a[0][2] 90 87 78 68 62 71 a[1][0] a[1][1] a[1][2]
  • 15.
    • /*Write aprogram to read 3*3 matrix and find maximum number*/ #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j,max; clrscr(); printf(“How matrix row wisen”); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(“a[%d][%d]= “,i,j); scanf(“%d”,&[i][j]); } printf(“n”); } max=a[0][0]; for(i=0;i<3;i++) { for(j=o;j<3;j++) { if(max<a[i][j]) max=a[i][j]; } } printf(“Max number is = %d”,max); getch(); }
  • 16.
    Multi-dimensional Array • Ingeneral, we can have n-dimensional array. • If n>=2, then we call that array as multi- dimensional array. For example, suppose we want to store 5 students marks information for two different exams carried out in the term for 4 different subjects, this data can be stored by using 3-dimensional array like, int marks[5][2][4];