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