c++ programs, c++ programming, program in c++, program of c++, program for c++, c++ how to program, a program in c++, program on c++, c++ program for , unit 4 part 3 programming with c++ array learn online pdf free download 12 notes, class 12 informatics practices notes, unit 4 part 3 programming with c++ array learn online pdf free download class 12, unit 4 part 3 programming with c++ array learn online pdf free download class 12 notes, class 12 unit 4 part 3 programming with c++ array learn online pdf free download, note informatics practices, informatics practices notes, unit 4 part 3 programming with c++ array learn online pdf free download, class 12 cinformatics practices chapter 6 notes, 12th standard informatics practices notes, 12th std informatics practices notes, class 12
Array is a group of same data types variables which are referred by a common
name.
An array is a series of elements of the same type placed in contiguous memory locations that can
be individually referenced by adding an index to a unique identifier.
Suppose we have to store roll number & marks of 50 students in a program ,
then in all we need 100 variable each having a different name. Now remembering and managing
these 100 variables is not easy task and it will make the program a complex and notunderstandable
program.
But if we declare two array each having fifty elements one for roll numbers and another for marks.
Now, we have only two names to remember. Elements of these array will be referred to as
Arrayname[n], where n is the element number in in the array . Therefore arrays are very much
useful in a case where quit many elements of the same data types need to be stored and processed.
The element number s in parenthesis are called subscripts or index. The subscript, or index of an
element designates its position in the array’s ordering.
Arrays are of two types:
1. One-dimensional Arrays
2. Multi-dimensional Arrays (But we have to discussing only Two-dimensional arrays in our syllabus
The simplest form of an array is one - dimensional. The array itself is given a name and its elements are referred to by their subscripts. In C++ , an array must be defined before it can be used to store information. The general form of an array declaration is:Data_type Array_name[size];
For example the following statement declares an array marks of int data type , which have 50
elements marks[0] to marks[49].
int marks[50] ;
We can reference array elements by using the array’s name& subscript (or index). The first element has a subscript of 0. The last element in an array of length n has a subscript of n-1.
If we declare an array:
int scores[9];
then its 10 element will be referred as:
scores[0], scores[1], ……………………………….., scores[8]
Elements of single dimensional array are stored in contiguous memory location in their index order. For example , an array p of type char with 6 elements declared as: char p[6]; will be represented in memory as shown below:
If we declare an int array age with 5 elements as: int age[5];
Accepting Data in Array from User (Inputting Array elements) and Printing Array elements:- Since we can refer to individual array elements using numbered indexes, it is very common for programmers to use for (or any) loops when processing arrays.
Program : A C++ program to read an array of 10 elements and print the sum of all elements of array:
#include <iostream.h>
void main()
{
int a[10], sum=0;
for(int j=0; j<10; j++) //loop for reading
{
cout<< “Enter Element “<< j+1<<”:”;
cin>> a[j]
sum=sum+a[j]`;
}
cout<<”Sum : “<<sum;
}
Program: Here’s another example of an array this program , invites the user to enter a series of six values representing sales for each day of the week (excluding Sunday), and then calculates the average sales
#include <iostream.h>
void main()
{
const int SIZE = 6; //size of array
double sales[SIZE]; //array of 6 elements
double total = 0;
cout<< “Enter sales for 6 days
”;
for(int j=0; j<SIZE; j++) //put figures in array
cin>> sales[j];
for(j=0; j<SIZE; j++) //read figures from array
total += sales[j]; //to find total
double average = total / SIZE; // find average
cout<< “Average = “ << average << endl;
}
Output:-
Enter sales for 6 days
352.64
867.70
781.32
867.35
746.21
189.45
Average = 634.11
In linear search, each element of the array is compared with the given item to be searched for , one by one either the desired element is found or the end of the array is reached.
#include<iostream.h>
void main( )
{
int size, i, n, c=0;
cout<<”Enter the size of array:”
cin>>size;
int a[size];
cout<<”Enter the elements of Array: “;
58
for(i-0; i<size; i++)
{
cin>>a[i];
}
cout<<” Enter the number to be search : “;
cin>>n ;
for (i=0; i<size; i++)
{ if (a[i]= =n)
{
c=1;
break;
}
}
if (c= =0)
cout<<” The number is not in the array.”;
else
cout<<” The Number is found and location of element is: “<<i+1:
}
When we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }. Syntax for initialization of one-dimensional array is:
Data_type Array_name[sise] = { value list } ;
For example:
int num [5] = { 16, 2, 77, 40, 12071 };
This declaration would have created an array like this:
The amount of values between braces { } must not be larger than the number of elements that we
declare for the array between square brackets [ ].
C++ allows us to skip the size of the array in an array initialization statement ( by leaving the
square brackets empty [ ] ). In this case, the compiler will assume a size for the array equal to the
number of values given between braces { }:
int num [ ] = { 16, 2, 77, 40, 12071 };
After this declaration, the size of array num will be 5, since we have provided 5 initialization
values.
Program : Program to find the maximum and minimum of an array :
#include<iostream.h>
void main( )
{
int arr[ ] = {10, 6 , -9 , 17 , 34 , 20 , 34 ,-2 ,92 ,22 };
int max = min = arr[0];
for(int i = 0 ; i<10 ; i++)
{
if(arr[i] < min)
min= arr[i];
if(arr[i] > max)
max = arr[i];
}
cout<<”The minimum of all is : “ << min<<”and the maximum is : “ <<max;
}
String as an array:- C++ does not have a string data type rather it implements string as single dimensional character array. A string is defined as a character array terminated by a null character ‘ ’. Hence to declare an array strg that holds a 10 character string, you would write : char strg[11]; Program : Program to count total number of vowels present in a string :
#include<iostream.h>
#include<stdio.h>
void main( )
{
char string[35]; int count = 0;
cout<<”Input a string”;
gets(string); // library function in stdio.h to input a string
for(int i = 0 ; string[i] != ’