SlideShare a Scribd company logo
BY
OMKAR MAJUKAR
XI-A
ARRAY
 An array is collection of variable of
same datatype that are referenced
by a common name.
 Types of arrays-
1) SINGLE DIMENSIONAL ARRAYS.
2) TWO DIMENSIONAL ARRAYS.
SINGLE
DIMENSIONAL
ARRAYS The simplest form of an array is a
single dimentional array.
 The array is given a name and its
elements are referred to by their
subscripts or indices.
 C++ array’s index numbering
starts with 0.
BASE DATATYPE
 The data type of array elements is
known as the base type of the
array.
type array-name[size];
 The above statement declared
array marks has 50 elements,
marks[0] to marks[49].
 EXAMPLE-
#include<iostream.h>
void main()
{
const int size = 50;
float marks[size];
for(int i=0; i<size; i++)
{
cout<<“Enter marks of students”<<i+1<<“n”;
cin>>marks[i];
}
cout<<n”;
for(i=0; i<size; i++)
cout<<“marks[“<<i<<“]=“<<marks[i]<<“n”;
return 0;
}
 OUTPUT-
Enter marks of student1
89
Enter marks of student2
98
Enter marks of student3
88
Marks[0] = 89
Marks[1] = 98
Marks[2] = 88
MEMORY
REPRESENTATION OF
SINGLE DIMENSIONAL
ARRAYS Single dimension arrays are essentially lists of
same information of the same type and their
elements are stored in contiguous memory
location in their index order.
 For example, an array grade of type char with 8
declared as
char grade[8];
will have element grade[0] at 1st allocated
memory location, grade[1] at the next, and so
forth.
Since grade is a char type array, each
element size of 1 byte(a character size is 1 byte)
and it will be represented in the memory as shown
below-
GRADE
ADDRESS 2000 2001 2002 2003 2004 2005 2006 2007
*An 8 element character array beginning at
location 2000 .
Total bytes = size of base datatype * no. of elements
Grade
[0]
Grade
[1]
Grade
[2]
Grade[
3]
Grade
[4]
Grade
[5]
Grade
[6]
Grade
[7]
ARRAY
TRAVERSAL Traversal- Accessing each element of array.
 Example-
#include<iostream.h>
Void main()
{
float PER[10];
int c;
for(c=0; c<10; c++)
{
cout<<“Enter percentage of student”<<c+1;
cin>>PER[c];
}
cout<<endl<<“Contents of array”<<endl;
for(c=0; c<10; ++c)
{
cout<<“Percentage of student”<<c+1<<“=“<<PER[c]<<endl;
}
}
SEARCHING FOR AN
ELEMENT IN A 1-D
ARRAY Sometimes we need to search an
element in an array.
 This can be only done by comparing the
search-item with each and every
element of the array.
 This process for searching for an
element in an array is called linear
search.
 EXAMPLE-
#include<iostream.h>
void main()
{
const int maxsize = 10;
int ARR[maxsize],srch,ndx,found=0;
cout<<endl<<“Enter array values”<<endl;
for(int c=0; c<maxsize; ++c)
cin>>ARR[c];
cout<<“Enter the value to be searched”;
cin>>srch;
for(c=0; c<maxsize; ++c)
{
if(ARR[c]==srch)
{
found=1;
ndx=c;
break;
}
}
if(found==1)
cout<<“First occurance is at index”<<ndx;
else
Cout<<“Value not found in the array”;
}
CHARACTER
ARRAY-STRINGS
 C++ stores sequence of characters I,e
string into character arrays.
 In character arrays arrays C++ puts a
special character after the last character
within the string to mark the end of string.
 The special character is known as null
character or 0.
For reading spaces within a string
gets() or getline() methods should be used
and for reading one word only cin can be
used.
 EXAMPLE-
#include<iostream.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
char st[50];
int c;
cout<<“Enter a string of characters”;
gets(st);
for(c=0; st[c]!=‘0’; c++)
{
if(c%2==0)
st[c]=toupper(st[c]);
else
st[c]=tolower(st[c]);
}
cout<<“Converted string = “<<st;
}
2 DIMENSIONAL
ARRAYS
 A 2-D array is an array in which each element is
itself an array.
 For instance, an array A[m][n] is an M by N table
with M rows and N columns containing M*N
elements.
No. of elements in 2-D array = No. of rows * No. of
columns
 The simplest form of a multi dimensioned array is 2-
D arrays, is an array having single dimensioned
array as its elements.
 The general form of the 2-D array declaration is
type array-name[rows][columns]
PROCESSING 2-D
ARRAYS
 To read or process a 2-D array, you
need to use nested loops.
 One loop processes the rows and other
the columns.
 If outer loop is for rows and inner loop
is for columns, then for each row index,
all columns are processed and then the
same process is repeated for next row
index.
 EXAMPLE-
#include<iostream.h>
void main()
{
float marks[4][3],sum,avg;
char grade[4];
int i,j;
for(i=0; i<4; i++)
{
sum = avg = 0;
cout<<“Enter 3 scores of student”<<i+1<<“:”;
for(j=0; j<3; j++)
{
cin>>marks[i][j];
sum+=marks[i][j];
}
avg = sum3;
if(avg<45.0)
grade[i] = ‘D’;
else if(avg<60.0)
grade[i] = ‘C’;
else if(avg<75.0)
grade[i] = ‘B’;
else
grade[i] = ‘A’;
}
for(i=0; i<4; i++)
{ cout<<“student”<<i+1<<“tTotal Marks = “;
cout<<marks[i][0] + marks[i][2]
cout<<“tGrade=“<<grade[i]<<“n”;
}
return 0;
}
 OUTPUT-
Enter 3 scores of student 1 : 78 65 46
Enter 3 scores of student 2 : 56 65 66
Enter 3 scores of student 3: 90 89 92
Enter 3 scores of student 4: 45 56 43
Student 1 Total Marks = 189 Grade = B
Student 2 Total Marks = 187 Grade = B
Student 3 Total Marks = 271 Grade = A
Student 4 Total Marks = 144 Grade = C
MEMORY
REPRESENTATION OF
2 DIMENSIONAL
ARRAYS Two dimensional arrays are stored in a row-
column matrix, where the first index
indicates the row and second indicates the
column.
 This means the second index changes
faster than the first index when accessing
the elements in the array in the order in
which they are actually stored in memory.
0 1 2 3 4 5
0
1
2
3
4
*A two dimensional array pay[5][7] in
memory.
 Total bytes=no. of rows*no. of
columns*size of base data type.
Pay[2][3]
Pay[3][1]
MATRICES AS 2-D
ARRAYS
 Matrix is a useful concept of
mathematics.
 A matrix is a set of mn numbers
arranged in the form of a rectangular
array of m rows and n columns.
 Such a matrix is called m*n(m by n)
matrix.
 Matrices can be represented through 2-
D arrays.
 EXAMPLE-
#include<iostream.h>
void main()
{
int A[3][3],B[3][3],r,c; //Read values in matrices
cout<<“Enter first matrix row wisen”;
for(r=0;r<3;r++)
{ for(c=0;c<3;c++)
{ cin>>B[r][c];
}
}
int flag=0; //loop to check equality
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{ if(A[r][c]!=B[r][c])
{ flag=1; break;
}
if(flag==1) break;
}
if(flag!=0)
cout<<“Matrices are unequaln”;
else
cout<<“Matrices are equaln”;
return 0;
}
 OUTPUT-
Enter first matrix row wise
1 2 3
4 5 6
7 8 9
Enter second matrix row wise
1 2 3
4 5 6
7 8 9
Matrices are equal
ARRAY
INITIALIZATION
 C++ provides the facility of array initialization at
the time of declaration.
 The arrays are initialized in the same way as other
variables are.
 The general form of array initialization is
Type array-name[size1]……[size N]={value-list};
 Following code initializes an integer array with 12
elements:
int days of month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
CALLING
FUNCTIONS WITH
ARRAYS When an arrays argument is passed to a
function, C++, in most contexts treats the
name of an array as if it were a pointer i,e.,
memory address of some element.
 That, if age is an int array to hold 10
integers then age stores the address of
age[0], the first element of an array i,e., the
array name age is a pointer to an integer
which is the first element of an array
age[10].
 EXAMPLE-
#include<iostream.h>
void main()
{
float large(float arr[],intn); //Function prototype
char ch;
int i;
float amount[50],big;
for(i=0;i<50;i++)
{
cout<<“nEnter element no”<<i+1<<“n”;
cin>>amount[i];
cout<<“nWant to enter more?(y/n)n”;
cin>>ch;
if(ch!=‘y’)
break;
}
if(i<50)
i++; //In case of incomplete loop
big=large(amount,i); //Call function large() by passing the array and its number of elements
cout<<“nThe largest element of the array is :”<<big<<“n”;
return 0;
}
float large(float arr[],int n)
{
if(arr[j]>max)
max=arr[j];
}
return(max);
}
 EXAMPLE-
Enter element no. 2
5
Want to enter more?(y/n)
y
Enter element no. 3
33
Want to enter more?(y/n)
y
Enter element no. 4
13
Want to enter more?(y/n)
n
The largest element of the array is :33
THANK
YOU

More Related Content

What's hot (16)

Arrays
ArraysArrays
Arrays
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
C arrays
C arraysC arrays
C arrays
 
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
 
Array
ArrayArray
Array
 
Model-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error CheckingModel-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error Checking
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Array
ArrayArray
Array
 
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
Qno 2 (c)
Qno 2 (c)Qno 2 (c)
Qno 2 (c)
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 

Similar to Structured data type

Similar to Structured data type (20)

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
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
 
Array assignment
Array assignmentArray assignment
Array assignment
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Arrays
ArraysArrays
Arrays
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 

Recently uploaded

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya - UEM Kolkata Quiz Club
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Abhinav Gaur Kaptaan
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfjoachimlavalley1
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 

Recently uploaded (20)

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 

Structured data type

  • 2. ARRAY  An array is collection of variable of same datatype that are referenced by a common name.  Types of arrays- 1) SINGLE DIMENSIONAL ARRAYS. 2) TWO DIMENSIONAL ARRAYS.
  • 3. SINGLE DIMENSIONAL ARRAYS The simplest form of an array is a single dimentional array.  The array is given a name and its elements are referred to by their subscripts or indices.  C++ array’s index numbering starts with 0.
  • 4. BASE DATATYPE  The data type of array elements is known as the base type of the array. type array-name[size];  The above statement declared array marks has 50 elements, marks[0] to marks[49].
  • 5.  EXAMPLE- #include<iostream.h> void main() { const int size = 50; float marks[size]; for(int i=0; i<size; i++) { cout<<“Enter marks of students”<<i+1<<“n”; cin>>marks[i]; } cout<<n”; for(i=0; i<size; i++) cout<<“marks[“<<i<<“]=“<<marks[i]<<“n”; return 0; }
  • 6.  OUTPUT- Enter marks of student1 89 Enter marks of student2 98 Enter marks of student3 88 Marks[0] = 89 Marks[1] = 98 Marks[2] = 88
  • 7. MEMORY REPRESENTATION OF SINGLE DIMENSIONAL ARRAYS Single dimension arrays are essentially lists of same information of the same type and their elements are stored in contiguous memory location in their index order.  For example, an array grade of type char with 8 declared as char grade[8]; will have element grade[0] at 1st allocated memory location, grade[1] at the next, and so forth.
  • 8. Since grade is a char type array, each element size of 1 byte(a character size is 1 byte) and it will be represented in the memory as shown below- GRADE ADDRESS 2000 2001 2002 2003 2004 2005 2006 2007 *An 8 element character array beginning at location 2000 . Total bytes = size of base datatype * no. of elements Grade [0] Grade [1] Grade [2] Grade[ 3] Grade [4] Grade [5] Grade [6] Grade [7]
  • 9. ARRAY TRAVERSAL Traversal- Accessing each element of array.  Example- #include<iostream.h> Void main() { float PER[10]; int c; for(c=0; c<10; c++) { cout<<“Enter percentage of student”<<c+1; cin>>PER[c]; } cout<<endl<<“Contents of array”<<endl; for(c=0; c<10; ++c) { cout<<“Percentage of student”<<c+1<<“=“<<PER[c]<<endl; } }
  • 10. SEARCHING FOR AN ELEMENT IN A 1-D ARRAY Sometimes we need to search an element in an array.  This can be only done by comparing the search-item with each and every element of the array.  This process for searching for an element in an array is called linear search.
  • 11.  EXAMPLE- #include<iostream.h> void main() { const int maxsize = 10; int ARR[maxsize],srch,ndx,found=0; cout<<endl<<“Enter array values”<<endl; for(int c=0; c<maxsize; ++c) cin>>ARR[c]; cout<<“Enter the value to be searched”; cin>>srch; for(c=0; c<maxsize; ++c) { if(ARR[c]==srch) { found=1; ndx=c; break; } } if(found==1) cout<<“First occurance is at index”<<ndx; else Cout<<“Value not found in the array”; }
  • 12. CHARACTER ARRAY-STRINGS  C++ stores sequence of characters I,e string into character arrays.  In character arrays arrays C++ puts a special character after the last character within the string to mark the end of string.  The special character is known as null character or 0. For reading spaces within a string gets() or getline() methods should be used and for reading one word only cin can be used.
  • 13.  EXAMPLE- #include<iostream.h> #include<stdio.h> #include<ctype.h> void main() { char st[50]; int c; cout<<“Enter a string of characters”; gets(st); for(c=0; st[c]!=‘0’; c++) { if(c%2==0) st[c]=toupper(st[c]); else st[c]=tolower(st[c]); } cout<<“Converted string = “<<st; }
  • 14. 2 DIMENSIONAL ARRAYS  A 2-D array is an array in which each element is itself an array.  For instance, an array A[m][n] is an M by N table with M rows and N columns containing M*N elements. No. of elements in 2-D array = No. of rows * No. of columns  The simplest form of a multi dimensioned array is 2- D arrays, is an array having single dimensioned array as its elements.  The general form of the 2-D array declaration is type array-name[rows][columns]
  • 15. PROCESSING 2-D ARRAYS  To read or process a 2-D array, you need to use nested loops.  One loop processes the rows and other the columns.  If outer loop is for rows and inner loop is for columns, then for each row index, all columns are processed and then the same process is repeated for next row index.
  • 16.  EXAMPLE- #include<iostream.h> void main() { float marks[4][3],sum,avg; char grade[4]; int i,j; for(i=0; i<4; i++) { sum = avg = 0; cout<<“Enter 3 scores of student”<<i+1<<“:”; for(j=0; j<3; j++) { cin>>marks[i][j]; sum+=marks[i][j]; } avg = sum3; if(avg<45.0) grade[i] = ‘D’; else if(avg<60.0) grade[i] = ‘C’; else if(avg<75.0) grade[i] = ‘B’; else grade[i] = ‘A’; } for(i=0; i<4; i++) { cout<<“student”<<i+1<<“tTotal Marks = “; cout<<marks[i][0] + marks[i][2] cout<<“tGrade=“<<grade[i]<<“n”; } return 0; }
  • 17.  OUTPUT- Enter 3 scores of student 1 : 78 65 46 Enter 3 scores of student 2 : 56 65 66 Enter 3 scores of student 3: 90 89 92 Enter 3 scores of student 4: 45 56 43 Student 1 Total Marks = 189 Grade = B Student 2 Total Marks = 187 Grade = B Student 3 Total Marks = 271 Grade = A Student 4 Total Marks = 144 Grade = C
  • 18. MEMORY REPRESENTATION OF 2 DIMENSIONAL ARRAYS Two dimensional arrays are stored in a row- column matrix, where the first index indicates the row and second indicates the column.  This means the second index changes faster than the first index when accessing the elements in the array in the order in which they are actually stored in memory.
  • 19. 0 1 2 3 4 5 0 1 2 3 4 *A two dimensional array pay[5][7] in memory.  Total bytes=no. of rows*no. of columns*size of base data type. Pay[2][3] Pay[3][1]
  • 20. MATRICES AS 2-D ARRAYS  Matrix is a useful concept of mathematics.  A matrix is a set of mn numbers arranged in the form of a rectangular array of m rows and n columns.  Such a matrix is called m*n(m by n) matrix.  Matrices can be represented through 2- D arrays.
  • 21.  EXAMPLE- #include<iostream.h> void main() { int A[3][3],B[3][3],r,c; //Read values in matrices cout<<“Enter first matrix row wisen”; for(r=0;r<3;r++) { for(c=0;c<3;c++) { cin>>B[r][c]; } } int flag=0; //loop to check equality for(r=0;r<3;r++) { for(c=0;c<3;c++) { if(A[r][c]!=B[r][c]) { flag=1; break; } if(flag==1) break; } if(flag!=0) cout<<“Matrices are unequaln”; else cout<<“Matrices are equaln”; return 0; }
  • 22.  OUTPUT- Enter first matrix row wise 1 2 3 4 5 6 7 8 9 Enter second matrix row wise 1 2 3 4 5 6 7 8 9 Matrices are equal
  • 23. ARRAY INITIALIZATION  C++ provides the facility of array initialization at the time of declaration.  The arrays are initialized in the same way as other variables are.  The general form of array initialization is Type array-name[size1]……[size N]={value-list};  Following code initializes an integer array with 12 elements: int days of month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  • 24. CALLING FUNCTIONS WITH ARRAYS When an arrays argument is passed to a function, C++, in most contexts treats the name of an array as if it were a pointer i,e., memory address of some element.  That, if age is an int array to hold 10 integers then age stores the address of age[0], the first element of an array i,e., the array name age is a pointer to an integer which is the first element of an array age[10].
  • 25.  EXAMPLE- #include<iostream.h> void main() { float large(float arr[],intn); //Function prototype char ch; int i; float amount[50],big; for(i=0;i<50;i++) { cout<<“nEnter element no”<<i+1<<“n”; cin>>amount[i]; cout<<“nWant to enter more?(y/n)n”; cin>>ch; if(ch!=‘y’) break; } if(i<50) i++; //In case of incomplete loop big=large(amount,i); //Call function large() by passing the array and its number of elements cout<<“nThe largest element of the array is :”<<big<<“n”; return 0; } float large(float arr[],int n) { if(arr[j]>max) max=arr[j]; } return(max); }
  • 26.  EXAMPLE- Enter element no. 2 5 Want to enter more?(y/n) y Enter element no. 3 33 Want to enter more?(y/n) y Enter element no. 4 13 Want to enter more?(y/n) n The largest element of the array is :33