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

2-Arrays.pdf

  • 1.
  • 2.
    Arrays An array isa 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 // arrayof 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 forarrays 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 ● Arrayelements 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 anarray 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 of10 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 Inputand 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 ● Visitingand 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 sumand 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> voidmain() { 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 anarray ● 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 stringvariable Example ● char name[7]=”Rohith”; ● char name[6]={‘J’, ‘e’, ‘e’, ‘n’, ‘a’, ‘0’};
  • 15.
    Memory allocation forstring char name[5]= “Raju”; ● It is represented in memory as follows.
  • 16.
    input a stringand 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 displayingstring //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 ofa 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 stringand 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; }