SlideShare a Scribd company logo
1 of 19
Download to read offline
Arrays
Arrays
An array is a collection of data elements that are of
the same type (e.g., a collection of integers, collection
of characters, etc).
Array Declaration Syntax:
data_type array_name[size];
Ex. int Ar[10];
The size of the array is indicated by <array_size>,
the number of elements in the array.
<array_size> must be an int constant or a
constant expression
Array Declaration
// array of 10 uninitialized ints
int Ar[10];
-- -- --
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
0 1 2 3 4 5
Memory allocation for arrays
The amount of storage required to hold an array is directly related to its type and
Size
total_bytes = sizeof(array_type) × size_of_array
For example,total bytes allocated for the array declared as float a[10];will be
4 × 10 = 40 bytes . (in Linux)
2 x 10 = 20 bytes . (In Turbo C++, Windows)
Array initialisation
● Array elements can be initialised in their declaration
statements in the same manner
● as in the case of variables ,except that the values must be
included in braces,as shown in the examples:
examples:
● int score[5] = {98, 87, 92, 79, 85};
● char code[6] = {‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’};
● float wgpa[7] = {9.60, 6.43, 8.50, 8.65, 5.89, 7.56, 8.22};
Subscripting
● Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
● To access an individual element we must apply a
subscript to array named Ar.
○ A subscript is a bracketed expression.
■ The expression in the brackets is known as the index.
○ First element of array has index 0.
Ar[0]
○ Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
○ Last element has an index one less than the size of the array.
Ar[9]
● Incorrect indexing is a common error.
Subscripting
// array of 10 uninitialized ints
int Ar[10];
Ar[3] = 1;
int x = Ar[3];
-- -- 1
--
Ar -- -- --
-- -- --
4 5 6
3
0 2 8 9
7
1
Ar[4
]
Ar[5
]
Ar[6
]
Ar[3
]
Ar[0
]
Ar[2
]
Ar[8
]
Ar[9
]
Ar[7
]
Ar[1
]
1
-- -- --
--
--
Program to Input and display elements in
array
// Input and display
elements in array
#include<iostream.h>
void main()
{
int num[5];
cout<<"Enter Five
numbers:";
for(int i=0;i<5;i++)
{
cin>>num[i];
}
cout<<"Inputted Numbers
are:";
for(i=0;i<5;i++)
{
cout<<"n"<<num[i];
}
}
Array Traversal
● Visiting and processing each element in an array is called
traversal.
● The process may be summation, displaying, etc.
for(i=0;i<5;i++)
{
cout<<num[i];
}
Program for sum and average of n elements in
an array
#include<iostream.h>
void main()
{
int a[100],n,i,sum=0,avg;
cout<<“enter number of elements:”;
cin>>n;
cout<<“enter”<< n<< “elements”;
for(i=0; i<n; i++)
{
cin>>a[i];
}
for(i=0; i<n; i++)
{
sum= sum+a[i];
}
avg=sum/n;
cout<<“sum=“<<sum<<“n”<<
“average=“<<avg;
}
Program for Traversal
#include<iostream.h>
void main()
{
int score[6];
int total=0;
float avg;
cout<<"Enter the marks of 6
subjects:";
for(int i=0;i<6;i++)
cin>>score[i];
for(i=0;i<6;i++)
total=total+score[i];
avg=total/6;
cout<<"Total score:"<<total;
cout<<"nAverage score:"<<avg;
}
String as an array
● Strings are group of characters for representing
name, house name, city etc.
● They are represented in double quotes. Eg:
“Geetha”, “Dubai” etc.
● Here a null character terminating character 0 is
used to represent the end of the array.
Declaration syntax
char name[20];
● The array ‘name’ is a character array that
can hold 19 characters and ended with 0.
Initialization of string variable
Example
● char name[7]=”Rohith”;
● char name[6]={‘J’, ‘e’, ‘e’, ‘n’, ‘a’, ‘0’};
Memory allocation for string
char name[5]= “Raju”;
● It is represented in memory as follows.
input a string and display
#include <iostream>
using namespace std;
int main()
{
char my_name[10];
cout << "Enter your name: ";
cin >> my_name;
cout << "Hello " << my_name;
return 0;
}
Out Put
_______________
Sample 1:
Enter your name:
Ajay
Hello Ajay
Sample 2:
Enter your name:
Smitha Mohan
Hello Smitha
(NB: Spaces in Characters
treated as delimiter).
To avoid this problem gets()
function can be used
Reading and displaying string
//String read n display
#include<iostream.h>
#include<stdio.h>
void main()
{
char name[20];
cout<<"ENTER NAME:";
gets(name);
cout<<"THE GIVEN NAME IS:";
puts(name);
}
Find Length of a string
//Length of a string
#include<iostream.h>
#include<stdio.h>
void main()
{
char a[100];
int i,count=0;
cout<<"ENTER A STRING:";
gets(a);
for(i=0;a[i]!='0';++i)
count=count+1;
cout<<"LENGTH OF GIVEN STRING IS:"<<count;
}
input a string and count the vowels in a
string
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[20];
int vow=0;
cout<<"Enter a string: ";
gets(str);
for(int i=0; str[i]!='0'; i++)
switch(str[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
}
cout<<"No. of vowels in the
string "<<str<<" is "<<vow;
}

More Related Content

Similar to 2-Arrays.pdf (20)

Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays
ArraysArrays
Arrays
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Array in-c
Array in-cArray in-c
Array in-c
 
Array in c language
Array in c language Array in c language
Array in c language
 
Array
ArrayArray
Array
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Array in C
Array in CArray in C
Array in C
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
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
 
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&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Arrays
ArraysArrays
Arrays
 
7.basic array
7.basic array7.basic array
7.basic array
 

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
flow of control python.pdf
flow of control python.pdfflow of control python.pdf
flow of control python.pdf
 
Database Management using MS Access.pdf
Database Management using MS Access.pdfDatabase Management using MS Access.pdf
Database Management using MS Access.pdf
 
CA-Web Hosting-Slide.pptx
CA-Web Hosting-Slide.pptxCA-Web Hosting-Slide.pptx
CA-Web Hosting-Slide.pptx
 
loops python.pdf
loops python.pdfloops python.pdf
loops python.pdf
 
flow of control python.pdf
flow of control python.pdfflow of control python.pdf
flow of control python.pdf
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
Java 2 computer science.pptx
Java 2 computer science.pptxJava 2 computer science.pptx
Java 2 computer science.pptx
 
java part 1 computer science.pptx
java part 1 computer science.pptxjava part 1 computer science.pptx
java part 1 computer science.pptx
 
Variable scope in php
Variable scope in phpVariable scope in php
Variable scope in php
 
Vardump and printr in php
Vardump and printr in phpVardump and printr in php
Vardump and printr in php
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Unit1 DBMS Introduction
Unit1 DBMS IntroductionUnit1 DBMS Introduction
Unit1 DBMS Introduction
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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.pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

2-Arrays.pdf

  • 2. Arrays An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, etc). Array Declaration Syntax: data_type array_name[size]; Ex. int Ar[10]; The size of the array is indicated by <array_size>, the number of elements in the array. <array_size> must be an int constant or a constant expression
  • 3. Array Declaration // array of 10 uninitialized ints int Ar[10]; -- -- -- -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1 0 1 2 3 4 5
  • 4. Memory allocation for arrays The amount of storage required to hold an array is directly related to its type and Size total_bytes = sizeof(array_type) × size_of_array For example,total bytes allocated for the array declared as float a[10];will be 4 × 10 = 40 bytes . (in Linux) 2 x 10 = 20 bytes . (In Turbo C++, Windows)
  • 5. Array initialisation ● Array elements can be initialised in their declaration statements in the same manner ● as in the case of variables ,except that the values must be included in braces,as shown in the examples: examples: ● int score[5] = {98, 87, 92, 79, 85}; ● char code[6] = {‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’}; ● float wgpa[7] = {9.60, 6.43, 8.50, 8.65, 5.89, 7.56, 8.22};
  • 6. Subscripting ● Declare an array of 10 integers: int Ar[10]; // array of 10 ints ● To access an individual element we must apply a subscript to array named Ar. ○ A subscript is a bracketed expression. ■ The expression in the brackets is known as the index. ○ First element of array has index 0. Ar[0] ○ Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… ○ Last element has an index one less than the size of the array. Ar[9] ● Incorrect indexing is a common error.
  • 7. Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- -- 1 -- Ar -- -- -- -- -- -- 4 5 6 3 0 2 8 9 7 1 Ar[4 ] Ar[5 ] Ar[6 ] Ar[3 ] Ar[0 ] Ar[2 ] Ar[8 ] Ar[9 ] Ar[7 ] Ar[1 ] 1 -- -- -- -- --
  • 8. Program to Input and display elements in array // Input and display elements in array #include<iostream.h> void main() { int num[5]; cout<<"Enter Five numbers:"; for(int i=0;i<5;i++) { cin>>num[i]; } cout<<"Inputted Numbers are:"; for(i=0;i<5;i++) { cout<<"n"<<num[i]; } }
  • 9. Array Traversal ● Visiting and processing each element in an array is called traversal. ● The process may be summation, displaying, etc. for(i=0;i<5;i++) { cout<<num[i]; }
  • 10. Program for sum and average of n elements in an array #include<iostream.h> void main() { int a[100],n,i,sum=0,avg; cout<<“enter number of elements:”; cin>>n; cout<<“enter”<< n<< “elements”; for(i=0; i<n; i++) { cin>>a[i]; } for(i=0; i<n; i++) { sum= sum+a[i]; } avg=sum/n; cout<<“sum=“<<sum<<“n”<< “average=“<<avg; }
  • 11. Program for Traversal #include<iostream.h> void main() { int score[6]; int total=0; float avg; cout<<"Enter the marks of 6 subjects:"; for(int i=0;i<6;i++) cin>>score[i]; for(i=0;i<6;i++) total=total+score[i]; avg=total/6; cout<<"Total score:"<<total; cout<<"nAverage score:"<<avg; }
  • 12. String as an array ● Strings are group of characters for representing name, house name, city etc. ● They are represented in double quotes. Eg: “Geetha”, “Dubai” etc. ● Here a null character terminating character 0 is used to represent the end of the array.
  • 13. Declaration syntax char name[20]; ● The array ‘name’ is a character array that can hold 19 characters and ended with 0.
  • 14. Initialization of string variable Example ● char name[7]=”Rohith”; ● char name[6]={‘J’, ‘e’, ‘e’, ‘n’, ‘a’, ‘0’};
  • 15. Memory allocation for string char name[5]= “Raju”; ● It is represented in memory as follows.
  • 16. input a string and display #include <iostream> using namespace std; int main() { char my_name[10]; cout << "Enter your name: "; cin >> my_name; cout << "Hello " << my_name; return 0; } Out Put _______________ Sample 1: Enter your name: Ajay Hello Ajay Sample 2: Enter your name: Smitha Mohan Hello Smitha (NB: Spaces in Characters treated as delimiter). To avoid this problem gets() function can be used
  • 17. Reading and displaying string //String read n display #include<iostream.h> #include<stdio.h> void main() { char name[20]; cout<<"ENTER NAME:"; gets(name); cout<<"THE GIVEN NAME IS:"; puts(name); }
  • 18. Find Length of a string //Length of a string #include<iostream.h> #include<stdio.h> void main() { char a[100]; int i,count=0; cout<<"ENTER A STRING:"; gets(a); for(i=0;a[i]!='0';++i) count=count+1; cout<<"LENGTH OF GIVEN STRING IS:"<<count; }
  • 19. input a string and count the vowels in a string #include <iostream> #include <cstdio> using namespace std; int main() { char str[20]; int vow=0; cout<<"Enter a string: "; gets(str); for(int i=0; str[i]!='0'; i++) switch(str[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': vow++; } cout<<"No. of vowels in the string "<<str<<" is "<<vow; }