SlideShare uma empresa Scribd logo
1 de 40
Declaration, Initialisation , Reading & Writing
of Arrays
• We have used the fundamental data types, char,
int, float, double.
• These types are constrained by the fact that a
variable of these types can store only one value at
any given time
• In many applications, we need to handle large
volumes of data
• To process such large amounts of data, we need a
powerful data types that would facilitate efficient
storing, accessing and manipulation of data items
• C supports a derived data type known as array.
• An array is a fixed-sized sequenced collection of
elements of the same data type thats shares a
common name.
• The common name a is the array name and each
individual data item is known as an element of the
array.
• The elements of the array are stored in the
subsequent memory locations starting from the
memory location given by the array name.
 Array can be classified as two types are,
1. One-Dimensional Array
2. Two-Dimensional or multidimensional
Array
 One-Dimensional Array
◦ A one-dimensional array can be used to represent a
list of data items. It is also known as a vector.
 Two-Dimensional Array
◦ A two dimensional array can be used to represent a
table of data items consisting of rows and columns.
It is also known as a matrix
 Every array must be declared before use like other
variables.
 The declarations of one-dimensional and
multidimensional arrays differ slightly.
 Declaration of One-dimensional array:
where
data_type refers to the data type of elements in the array.
It can be a primitive or derived data type.
name is an identifier which represents the array name.
size is an integer expression representing the total
number of elements in the array.
data_type name1[size1], name2[size2],…, namen[sizen];
 The base address of an array is the address of the
zeroth element (starting element) of that array.
 When an array is declared, the compiler allocates a base
address and reserves enough space in memory for all
the elements of the array.
 In C, the array name represents this base address.
 For example, float x[5]; is the declaration of a one-
dimensional array.
 It defines a float array x of size 5 that represents a
block of 5 consecutive storage regions.
 Each element in the array is referred to by the array
variables x[0], x[1], x[2] ,x[3] and x[4] where 0,1,2,3
and 4 represent subscripts or indices of the array.
 In general, x[i] refers to the ith element of the array.
 The array subscripts always start at zero in C and
they are integer expressions.
 Hence, x[0] refers to the starting element, x[1] refers
to the next element and x[4] refers to the last
element.
 The declaration double list[10], array[15]; declares
two arrays, named list and array having 10 and 15
elements respectively of double precision data type.
 The elements of an array may be assigned with the values using
initialisation instead of reading them by the I/O functions.
 An array can be initialised in its declaration only.
 The list of initialisers (values) are enclosed in braces.
 The initialisers are separated by commas and they must be
constants or constant expressions.
 One-dimensional array can be initialised as given below.
int a[4]={3,5, -8, 10};
 The values within the braces are scanned from left end and
assigned to a[0], a[1] and so on.
 A semicolon should be placed after the closing brace.
“For “ loops used extensively for array processing
Example 1: write a program to read an array and
print.
#include<stdio.h>
void main()
{
int i,a[10];
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for(i=0;i<10;i++)
Printf(“n %d”,a[i]);
}
#include<stdio.h>
void main()
{
int x[2],i;
printf("nEnter the inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("nThe value in x[%d] is %d",i,x[i]);
}
OUTPUT:
Enter the inputs:3
6
The value in x[0] is 3
The value in x[1] is 6
/*Determines the length of a message */
#include<stdio.h>
main()
{
char ch;
int len=0;
printf(“Enter a message : ”);
ch=getchar();
while (ch!=‘n’) {
len++;
ch=getchar();
}
printf(“Your message was %d character(s) long. n”,
len);
return 0;
}
 The c languages treats strings as arrays of characters
 The compiler terminates the character strings with an
additional null (‘0’) character.
 String is a one dimensional array of characters in c.
 The element name[10] holds the null character ‘0’ at
the end
 When declaring character arrays, we must always allow
one extra elements space for the null terminator
#include<stdio.h>
void main()
{
int i=0;
char a[]="abcd";
while(a[i]!='0')
{
printf("t%c",a[i]);
i++;
}
}
OUTPUT:
a b c d
 There are different way to initialize a character array variable.
char name[10]=“StudingTonight”; //valid initialization
char name[10]={‘L’,’E’,’S’,’S’,’O’,’N’,’S’,’0’}; // valid Init
Some example for illegal Initialization of character array are,
char ch[3]=“hello”; // Illegal
char str[4];
str=“hello”; //illegal
Format:
data_type array_name[row_size1][column_size1];
 The first subscript represents the row size and the second subscript
represents the column size.
 The value in the ith row and jth column is referred to by name[i][j].
 The total number of elements in a two dimensional array is calculated
by multiplying the number of rows by the number of columns.
 In two-dimensional arrays, the values are stored row by row.
 int x[3][2];
 For example, the declaration int a[3][2] ,b[7][4]; defines
the two-dimensional integer arrays a and b.
 These arrays are arrays of integer arrays.
 Six (rows * columns) elements of a are stored row by row
in the consecutive memory locations.
 This method of storing the elements in the memory is
known as row major order storage representation
 The zeroth element of array a is denoted by a[0][0] and the
last element in that array is denoted by a[2][1].
 Similarly, for the array b, the zeroth element and the last
element are represented by b[0][0] and b[6][3] respectively.
Starting element a[0][0]
a[0][1]
a[1][0]
a[1][1]
a[2][0]
Last element a[2][1]
Row 0
Row 1Row 1
Row 2
Storage representation of matrix a
Syntax:
data_type array_name[row_size][col_size]={variables};
 A two-dimensional array can be initialised as given below.
int a[3][2] = {20, 25, -3, 8, -5, 7};
 Here, the initialised values are assigned to the array elements
according to the order of the storage in the memory.
 But if the initialisers are enclosed within the inner braces as given
below
int a[3][2] = {{20,25}, {-3, 8}, {-5,7}};
 The initialised values within the inner braces are assigned to each row.
 To enforce the assignment of each row, it is essential to use the inner
braces.
 If the number of values initialised for an array is less than the size
mentioned, the missing elements are assigned zero. In the initialisation
int a[3][4] = { {1,2},{4,8,15} };
 the elements a[0][0] and a[0][1] of the zeroth row and a[1][0], a[1][1] and
a[1][2] of the first row are initialised with the values 1,2,4,8 and 15
respectively.
 All the other elements are initialised to zero.
 If it is given as
int a[3][4] = {1,2,3,8,15};
 the values are assigned from the left end to a[0][0], a[0][1], a[0][2],
a[0][3], and a[[1][0] according to the order of the storage representation.
 If the number of initialisers exceeds the size of the array, it is an error.
 The size of a one-dimensional array need not be mentioned in its
initialisation.
 In this case, the compiler will count the values assigned and take it
as the size of that array.
 In multidimensional arrays the leftmost subscript may be omitted
and all the others must be specified. The initialisation
int x[ ] = {2,4,6,8,10};
 makes the array x having 5 elements
float a[ ][2] = {{3.2,4.8}, {5.3,3.7}};
 makes the array a having 2 rows and 2 columns.
#include<stdio.h>
void main()
{
int i,j;
int x[2][2]={ {1,50},
{2,75}
};
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]);
}
OUTPUT:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
#include<stdio.h>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
for(i=0;i<=2;i++)
for(j=0;j<2;j++)
printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]);
}
OUTPUT:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
The value in x[2][0] is 3
The value in x[2][1] is 65
/* PROGRAM TO FIND THE MINIMUM VALUE AND ITS POSITION */
#include<stdio.h>
#define MAX 5
main( )
{
int a[MAX],i,min;
int pos = 0; /* Fixing position if the first number is
the minimum value */
printf("Enter the array elementsn");
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
min=a[0];
for(i=1;i<MAX;i++)
if(a[i] < min)
{
min=a[i];
pos=i; /* Fixing the minimum value position*/
}
printf(“ MINIMUM VALUE = %dn”,min);
printf(“ POSITION = %dn”,pos);
}
SAMPLE INPUT AND OUTPUT
Enter the array elements
14 29 37 25 45
MINIMUM VALUE = 14
POSITION = 0
#define MAXROW 2
#define MAXCOL 4
main( )
{
int s[MAXROW][MAXCOL],t[MAXCOL][MAXROW];
int i,j;
printf("Enter the values of the matrixn");
for(i=0;i<MAXROW;i++)
for(j=0;j<MAXCOL;j++)
scanf("%d",&s[i][j]);
printf("nGiven Matrixnn");
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOL;j++)
printf("%dt",s[i][j]);
printf("n");
}
for(i=0;i<MAXCOL;i++)
for(j=0;j<MAXROW;j++)
t[i][j] = s[j][i];
printf("nTranspose of the matrixnn");
for(i=0;i<MAXCOL;i++)
{
for(j=0;j<MAXROW;j++)
printf("%dt",t[i][j]); printf("n");
}
}
SAMPLE INPUT AND OUTPUT
Enter the values of the matrix
10 20 30 40 50 60 70 80
Given Matrix
10 20 30 40
50 60 70 80
Transpose of the matrix
10 50
20 60
30 70
40 80
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
//step1:
printf("n Enter the size of matrix
A:");
scanf("%d%d",&r1,&c1);
printf("n Enter the size of matrix
B: ");
scanf("%d%d",&r2,&c2);
if((c1==c2)&&(r1==r2))
goto step2;
else
goto step1;
//step2:
printf("n Enter the elements of matrix
A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix
B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+a[i][j]+b[i][j];
}
}
printf("n The resultant matrix
after addition of A & B isn");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%dt",c[i][j]);
printf("n");
}
getch();
}
OUTPUT:
Enter the size of matrix A: 2
2
Enter the size of matrix B: 2
2
Enter the elements of matrix A
2
2
2
2
Enter the elements of matrix B
3
3
3
3
The resultant matrix after addition
of A&B is
5 5
5 5
/* PROGRAM TO REMOVE THE DUPLICATE ELEMENTS IN AN ARRAY */
#define MAX 5
main( )
{
int a[MAX];
int i,j,k,n=MAX,p;
for(i=0;i<n;i++)
{
printf("Enter the value of a[%d]:n",i);
scanf("%d",&a[i]);
}
printf("Array before deleting duplicate elementsn");
for(i=0;i<n;i++)
printf("%dt",a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)

{
if(a[i] == a[j])
{
p = i ;
for(k=j;k<n;k++)
a[k] =a[++j];
j = p; n--;
}
}
}
putchar('n');
printf("Array after deleting duplicate elementsn");
for(i=0;i<n;i++)
printf("%dt",a[i]);
putchar('n');
}
SAMPLE INPUT AND OUTPUT
Enter the value of a[0]:
5
Enter the value of a[1]:
8
Enter the value of a[2]:
5
Enter the value of a[3]:
12
Enter the value of a[4]:
8
Array before deleting duplicate elements
5 8 5 12
8
Array after deleting duplicate elements
5 8 12
/* PROGRAM TO SORT AN ARRAY */
#define MAX 5
main( )
{
int a[MAX],i,j,temp;
printf("Enter the array elementsn");
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
for(i=0;i<MAX;i++)
for(j=i+1;j<MAX;j++)
{
if(a[i] >a[j])
Example Programs
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf("nAscending ordern");
for(i=0;i<MAX;i++)
printf("%dt",a[i]);
printf("nDescending ordern");
for(i=MAX-1;i>=0;i--)
printf("%dt",a[i]);
}
SAMPLE INPUT AND OUTPUT
Enter the array elements
11 15 20 45 15
Ascending order
11 15 15 20 45
Descending order
45 20 15 15 11
Example Programs
/*PROGRAM TO CONVERT A 2-D ARRAY TO 1-D ARRAY */
#define MAXROW 3
#define MAXCOL 2
main( )
{
int a[MAXROW][MAXCOL],b[MAXROW*MAXCOL];
int i,j,k=0;
printf("Enter the matrix elements in row ordern");
for(i=0;i<MAXROW;i++)
for(j=0;j<MAXCOL;j++)
{
scanf("%d",&a[i][j]);
Example Programs
b[k++] = a[i][j];
}
printf("Given two-dimensional arrayn");
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOL;j++)
printf("%dt",a[i][j]);
printf("n");
}
printf("Equivalent one-dimensional arrayn");
for(i=0;i<MAXROW*MAXCOL;i++)
printf("%dt",b[i]);
}
SAMPLE INPUT AND OUTPUT
Enter the matrix elements in row order
10 15 20 25 30 35
Given two-dimensional array
10 15
20 25
30 35
Equivalent one-dimensional array
10 15 20 25 30 35
Example Programs
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional

Mais conteúdo relacionado

Mais procurados

Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 
structure and union
structure and unionstructure and union
structure and union
student
 

Mais procurados (20)

Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Arrays
ArraysArrays
Arrays
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
String functions in C
String functions in CString functions in C
String functions in C
 
Strings in C
Strings in CStrings in C
Strings in C
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
structure and union
structure and unionstructure and union
structure and union
 
Array
ArrayArray
Array
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
C programming - String
C programming - StringC programming - String
C programming - String
 

Destaque

Destaque (12)

Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015
 
Difference between structure and union
Difference between structure and unionDifference between structure and union
Difference between structure and union
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Types of storage class specifiers in c programming
Types of storage class specifiers in c programmingTypes of storage class specifiers in c programming
Types of storage class specifiers in c programming
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Planers machine
Planers machinePlaners machine
Planers machine
 
Conventional machining vs. non conventional machining
Conventional machining vs. non conventional machiningConventional machining vs. non conventional machining
Conventional machining vs. non conventional machining
 

Semelhante a Arrays 1D and 2D , and multi dimensional

array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Semelhante a Arrays 1D and 2D , and multi dimensional (20)

Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Arrays
ArraysArrays
Arrays
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
About Array
About ArrayAbout Array
About Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Arrays
ArraysArrays
Arrays
 
02 arrays
02 arrays02 arrays
02 arrays
 
Array
ArrayArray
Array
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Último (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 

Arrays 1D and 2D , and multi dimensional

  • 1. Declaration, Initialisation , Reading & Writing of Arrays
  • 2. • We have used the fundamental data types, char, int, float, double. • These types are constrained by the fact that a variable of these types can store only one value at any given time • In many applications, we need to handle large volumes of data • To process such large amounts of data, we need a powerful data types that would facilitate efficient storing, accessing and manipulation of data items
  • 3. • C supports a derived data type known as array. • An array is a fixed-sized sequenced collection of elements of the same data type thats shares a common name. • The common name a is the array name and each individual data item is known as an element of the array. • The elements of the array are stored in the subsequent memory locations starting from the memory location given by the array name.
  • 4.  Array can be classified as two types are, 1. One-Dimensional Array 2. Two-Dimensional or multidimensional Array
  • 5.  One-Dimensional Array ◦ A one-dimensional array can be used to represent a list of data items. It is also known as a vector.  Two-Dimensional Array ◦ A two dimensional array can be used to represent a table of data items consisting of rows and columns. It is also known as a matrix
  • 6.  Every array must be declared before use like other variables.  The declarations of one-dimensional and multidimensional arrays differ slightly.  Declaration of One-dimensional array: where data_type refers to the data type of elements in the array. It can be a primitive or derived data type. name is an identifier which represents the array name. size is an integer expression representing the total number of elements in the array. data_type name1[size1], name2[size2],…, namen[sizen];
  • 7.  The base address of an array is the address of the zeroth element (starting element) of that array.  When an array is declared, the compiler allocates a base address and reserves enough space in memory for all the elements of the array.  In C, the array name represents this base address.  For example, float x[5]; is the declaration of a one- dimensional array.  It defines a float array x of size 5 that represents a block of 5 consecutive storage regions.
  • 8.  Each element in the array is referred to by the array variables x[0], x[1], x[2] ,x[3] and x[4] where 0,1,2,3 and 4 represent subscripts or indices of the array.  In general, x[i] refers to the ith element of the array.  The array subscripts always start at zero in C and they are integer expressions.  Hence, x[0] refers to the starting element, x[1] refers to the next element and x[4] refers to the last element.  The declaration double list[10], array[15]; declares two arrays, named list and array having 10 and 15 elements respectively of double precision data type.
  • 9.  The elements of an array may be assigned with the values using initialisation instead of reading them by the I/O functions.  An array can be initialised in its declaration only.  The list of initialisers (values) are enclosed in braces.  The initialisers are separated by commas and they must be constants or constant expressions.  One-dimensional array can be initialised as given below. int a[4]={3,5, -8, 10};  The values within the braces are scanned from left end and assigned to a[0], a[1] and so on.  A semicolon should be placed after the closing brace.
  • 10. “For “ loops used extensively for array processing Example 1: write a program to read an array and print. #include<stdio.h> void main() { int i,a[10]; for(i=0;i<10;i++) scanf(“%d”,&a[i]); for(i=0;i<10;i++) Printf(“n %d”,a[i]); }
  • 11.
  • 12. #include<stdio.h> void main() { int x[2],i; printf("nEnter the inputs:"); for(i=0;i<2;i++) scanf("%d",&x[i]); for(i=0;i<2;i++) printf("nThe value in x[%d] is %d",i,x[i]); } OUTPUT: Enter the inputs:3 6 The value in x[0] is 3 The value in x[1] is 6
  • 13. /*Determines the length of a message */ #include<stdio.h> main() { char ch; int len=0; printf(“Enter a message : ”); ch=getchar(); while (ch!=‘n’) { len++; ch=getchar(); } printf(“Your message was %d character(s) long. n”, len); return 0; }
  • 14.  The c languages treats strings as arrays of characters  The compiler terminates the character strings with an additional null (‘0’) character.  String is a one dimensional array of characters in c.  The element name[10] holds the null character ‘0’ at the end  When declaring character arrays, we must always allow one extra elements space for the null terminator
  • 15. #include<stdio.h> void main() { int i=0; char a[]="abcd"; while(a[i]!='0') { printf("t%c",a[i]); i++; } } OUTPUT: a b c d
  • 16.  There are different way to initialize a character array variable. char name[10]=“StudingTonight”; //valid initialization char name[10]={‘L’,’E’,’S’,’S’,’O’,’N’,’S’,’0’}; // valid Init Some example for illegal Initialization of character array are, char ch[3]=“hello”; // Illegal char str[4]; str=“hello”; //illegal
  • 17. Format: data_type array_name[row_size1][column_size1];  The first subscript represents the row size and the second subscript represents the column size.  The value in the ith row and jth column is referred to by name[i][j].  The total number of elements in a two dimensional array is calculated by multiplying the number of rows by the number of columns.  In two-dimensional arrays, the values are stored row by row.
  • 19.  For example, the declaration int a[3][2] ,b[7][4]; defines the two-dimensional integer arrays a and b.  These arrays are arrays of integer arrays.  Six (rows * columns) elements of a are stored row by row in the consecutive memory locations.  This method of storing the elements in the memory is known as row major order storage representation  The zeroth element of array a is denoted by a[0][0] and the last element in that array is denoted by a[2][1].  Similarly, for the array b, the zeroth element and the last element are represented by b[0][0] and b[6][3] respectively.
  • 20. Starting element a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] Last element a[2][1] Row 0 Row 1Row 1 Row 2 Storage representation of matrix a
  • 21. Syntax: data_type array_name[row_size][col_size]={variables};  A two-dimensional array can be initialised as given below. int a[3][2] = {20, 25, -3, 8, -5, 7};  Here, the initialised values are assigned to the array elements according to the order of the storage in the memory.  But if the initialisers are enclosed within the inner braces as given below int a[3][2] = {{20,25}, {-3, 8}, {-5,7}};  The initialised values within the inner braces are assigned to each row.  To enforce the assignment of each row, it is essential to use the inner braces.
  • 22.  If the number of values initialised for an array is less than the size mentioned, the missing elements are assigned zero. In the initialisation int a[3][4] = { {1,2},{4,8,15} };  the elements a[0][0] and a[0][1] of the zeroth row and a[1][0], a[1][1] and a[1][2] of the first row are initialised with the values 1,2,4,8 and 15 respectively.  All the other elements are initialised to zero.  If it is given as int a[3][4] = {1,2,3,8,15};  the values are assigned from the left end to a[0][0], a[0][1], a[0][2], a[0][3], and a[[1][0] according to the order of the storage representation.  If the number of initialisers exceeds the size of the array, it is an error.
  • 23.  The size of a one-dimensional array need not be mentioned in its initialisation.  In this case, the compiler will count the values assigned and take it as the size of that array.  In multidimensional arrays the leftmost subscript may be omitted and all the others must be specified. The initialisation int x[ ] = {2,4,6,8,10};  makes the array x having 5 elements float a[ ][2] = {{3.2,4.8}, {5.3,3.7}};  makes the array a having 2 rows and 2 columns.
  • 24. #include<stdio.h> void main() { int i,j; int x[2][2]={ {1,50}, {2,75} }; for(i=0;i<2;i++) for(j=0;j<2;j++) printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]); } OUTPUT: The value in x[0][0] is 1 The value in x[0][1] is 50 The value in x[1][0] is 2 The value in x[1][1] is 75
  • 25. #include<stdio.h> void main() { int i,j; int x[][2]={ {1,50},{2,75},{3,65}}; for(i=0;i<=2;i++) for(j=0;j<2;j++) printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]); } OUTPUT: The value in x[0][0] is 1 The value in x[0][1] is 50 The value in x[1][0] is 2 The value in x[1][1] is 75 The value in x[2][0] is 3 The value in x[2][1] is 65
  • 26. /* PROGRAM TO FIND THE MINIMUM VALUE AND ITS POSITION */ #include<stdio.h> #define MAX 5 main( ) { int a[MAX],i,min; int pos = 0; /* Fixing position if the first number is the minimum value */ printf("Enter the array elementsn"); for(i=0;i<MAX;i++) scanf("%d",&a[i]); min=a[0]; for(i=1;i<MAX;i++) if(a[i] < min) { min=a[i]; pos=i; /* Fixing the minimum value position*/ } printf(“ MINIMUM VALUE = %dn”,min); printf(“ POSITION = %dn”,pos); } SAMPLE INPUT AND OUTPUT Enter the array elements 14 29 37 25 45 MINIMUM VALUE = 14 POSITION = 0
  • 27. #define MAXROW 2 #define MAXCOL 4 main( ) { int s[MAXROW][MAXCOL],t[MAXCOL][MAXROW]; int i,j; printf("Enter the values of the matrixn"); for(i=0;i<MAXROW;i++) for(j=0;j<MAXCOL;j++) scanf("%d",&s[i][j]); printf("nGiven Matrixnn"); for(i=0;i<MAXROW;i++) { for(j=0;j<MAXCOL;j++) printf("%dt",s[i][j]); printf("n"); }
  • 28. for(i=0;i<MAXCOL;i++) for(j=0;j<MAXROW;j++) t[i][j] = s[j][i]; printf("nTranspose of the matrixnn"); for(i=0;i<MAXCOL;i++) { for(j=0;j<MAXROW;j++) printf("%dt",t[i][j]); printf("n"); } } SAMPLE INPUT AND OUTPUT Enter the values of the matrix 10 20 30 40 50 60 70 80 Given Matrix 10 20 30 40 50 60 70 80 Transpose of the matrix 10 50 20 60 30 70 40 80
  • 29. #include<stdio.h> #include<conio.h> void main() { int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); //step1: printf("n Enter the size of matrix A:"); scanf("%d%d",&r1,&c1); printf("n Enter the size of matrix B: "); scanf("%d%d",&r2,&c2); if((c1==c2)&&(r1==r2)) goto step2; else goto step1; //step2: printf("n Enter the elements of matrix A n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } } printf("n Enter the elements of matrix B n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("t%d",&b[i][j]); } }
  • 30. for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=0; c[i][j]=c[i][j]+a[i][j]+b[i][j]; } } printf("n The resultant matrix after addition of A & B isn"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%dt",c[i][j]); printf("n"); } getch(); } OUTPUT: Enter the size of matrix A: 2 2 Enter the size of matrix B: 2 2 Enter the elements of matrix A 2 2 2 2 Enter the elements of matrix B 3 3 3 3 The resultant matrix after addition of A&B is 5 5 5 5
  • 31. /* PROGRAM TO REMOVE THE DUPLICATE ELEMENTS IN AN ARRAY */ #define MAX 5 main( ) { int a[MAX]; int i,j,k,n=MAX,p; for(i=0;i<n;i++) { printf("Enter the value of a[%d]:n",i); scanf("%d",&a[i]); } printf("Array before deleting duplicate elementsn"); for(i=0;i<n;i++) printf("%dt",a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++)
  • 32.  { if(a[i] == a[j]) { p = i ; for(k=j;k<n;k++) a[k] =a[++j]; j = p; n--; } } } putchar('n'); printf("Array after deleting duplicate elementsn"); for(i=0;i<n;i++) printf("%dt",a[i]); putchar('n'); } SAMPLE INPUT AND OUTPUT Enter the value of a[0]: 5 Enter the value of a[1]: 8 Enter the value of a[2]: 5 Enter the value of a[3]: 12 Enter the value of a[4]: 8 Array before deleting duplicate elements 5 8 5 12 8 Array after deleting duplicate elements 5 8 12
  • 33. /* PROGRAM TO SORT AN ARRAY */ #define MAX 5 main( ) { int a[MAX],i,j,temp; printf("Enter the array elementsn"); for(i=0;i<MAX;i++) scanf("%d",&a[i]); for(i=0;i<MAX;i++) for(j=i+1;j<MAX;j++) { if(a[i] >a[j]) Example Programs
  • 34. { temp = a[i]; a[i] = a[j]; a[j] = temp; } } printf("nAscending ordern"); for(i=0;i<MAX;i++) printf("%dt",a[i]); printf("nDescending ordern"); for(i=MAX-1;i>=0;i--) printf("%dt",a[i]); } SAMPLE INPUT AND OUTPUT Enter the array elements 11 15 20 45 15 Ascending order 11 15 15 20 45 Descending order 45 20 15 15 11 Example Programs
  • 35. /*PROGRAM TO CONVERT A 2-D ARRAY TO 1-D ARRAY */ #define MAXROW 3 #define MAXCOL 2 main( ) { int a[MAXROW][MAXCOL],b[MAXROW*MAXCOL]; int i,j,k=0; printf("Enter the matrix elements in row ordern"); for(i=0;i<MAXROW;i++) for(j=0;j<MAXCOL;j++) { scanf("%d",&a[i][j]); Example Programs
  • 36. b[k++] = a[i][j]; } printf("Given two-dimensional arrayn"); for(i=0;i<MAXROW;i++) { for(j=0;j<MAXCOL;j++) printf("%dt",a[i][j]); printf("n"); } printf("Equivalent one-dimensional arrayn"); for(i=0;i<MAXROW*MAXCOL;i++) printf("%dt",b[i]); } SAMPLE INPUT AND OUTPUT Enter the matrix elements in row order 10 15 20 25 30 35 Given two-dimensional array 10 15 20 25 30 35 Equivalent one-dimensional array 10 15 20 25 30 35 Example Programs