SlideShare a Scribd company logo
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

PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Array,string structures. Best presentation pptx
PPTX
Chapter-Five.pptx
PPTX
ARRAYS.pptx
PPTX
Arrays_in_c++.pptx
PPTX
Arrays_and_Strings_in_C_Programming.pptx
PPTX
Arrays
PPTX
Arrays
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Array,string structures. Best presentation pptx
Chapter-Five.pptx
ARRAYS.pptx
Arrays_in_c++.pptx
Arrays_and_Strings_in_C_Programming.pptx
Arrays
Arrays

Similar to 2-Arrays.pdf (20)

PPTX
Arrays matrix 2020 ab
PPT
Fp201 unit4
PPT
arrayy.ppt
PPT
Arrays Basics
PDF
Arrays and library functions
PPT
C++ Arrays
PPT
C++ Arrays
PPTX
Data structure.pptx
PPTX
Array and string in C++_093547 analysis.pptx
PPTX
Module_3_Arrays - Updated.pptx............
PDF
Arrays
PPT
Structured Data Type Arrays
PPTX
Array in C
PPTX
Lecture 7
PPT
Mesics lecture 8 arrays in 'c'
DOCX
Array assignment
PPT
2DArrays.ppt
PPT
Lec 25 - arrays-strings
PPT
CHAPTER-5.ppt
PDF
Arrays and library functions
Arrays matrix 2020 ab
Fp201 unit4
arrayy.ppt
Arrays Basics
Arrays and library functions
C++ Arrays
C++ Arrays
Data structure.pptx
Array and string in C++_093547 analysis.pptx
Module_3_Arrays - Updated.pptx............
Arrays
Structured Data Type Arrays
Array in C
Lecture 7
Mesics lecture 8 arrays in 'c'
Array assignment
2DArrays.ppt
Lec 25 - arrays-strings
CHAPTER-5.ppt
Arrays and library functions

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

PPTX
IO and threads Java
PDF
flow of control python.pdf
PDF
Database Management using MS Access.pdf
PPTX
CA-Web Hosting-Slide.pptx
PDF
PDF
flow of control python.pdf
PPTX
java 4 Part 1 computer science.pptx
PPTX
Java 3 Computer Science.pptx
PPTX
Java 2 computer science.pptx
PPTX
java part 1 computer science.pptx
PPTX
Variable scope in php
PPTX
Vardump and printr in php
PPTX
Basic sql Commands
PPTX
Unit1 DBMS Introduction
IO and threads Java
flow of control python.pdf
Database Management using MS Access.pdf
CA-Web Hosting-Slide.pptx
flow of control python.pdf
java 4 Part 1 computer science.pptx
Java 3 Computer Science.pptx
Java 2 computer science.pptx
java part 1 computer science.pptx
Variable scope in php
Vardump and printr in php
Basic sql Commands
Unit1 DBMS Introduction

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Lesson notes of climatology university.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
master seminar digital applications in india
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Lesson notes of climatology university.
Microbial disease of the cardiovascular and lymphatic systems
Yogi Goddess Pres Conference Studio Updates
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
master seminar digital applications in india
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Anesthesia in Laparoscopic Surgery in India
LDMMIA Reiki Yoga Finals Review Spring Summer
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Paper A Mock Exam 9_ Attempt review.pdf.
Microbial diseases, their pathogenesis and prophylaxis
STATICS OF THE RIGID BODIES Hibbelers.pdf
Classroom Observation Tools for Teachers
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Weekly quiz Compilation Jan -July 25.pdf

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; }