SlideShare a Scribd company logo
1 
ARRAY: 
An array is a “contiguous memory location*” 
Contiguous memory location means “ memory blocks having consecutive addresses”. 
An array is a group of consecutive memory location with same name and type. Simple 
variable is a single memory location with unique name and a type. 
The memory locations in the array are known as elements of array. The total number of 
elements in the array is called its length. Each element in the array is accessed with 
reference to its position of the location in the array. This position is called index. Each 
element in the array has a unique index. The index of first element is 0 and the index of 
last element is length. The value of the index is written in brackets along with the name 
of the array. 
An array is a collective name given to a group of similar quantities. These similar 
quantities could be percentage marks of 100 students, number of chairs in home, or 
salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar 
elements. These similar elements could be all integers or all floats or all characters etc. 
ARRAY DECLARATION: 
The process of specifying array name, length, and data type is called array declaration. 
Syntax = Data type _name of array[length];
2 
ARRAY INITIALIZATION: 
The process of assigning values to array elements at the time of array declaration is called 
array initialization. The initialization process provides a list of initial values for array 
elements. The values are separated with commas and enclosed within braces. 
Syntax = Data type _name of array[length] = {list of values}; 
ACCESSING ARRAY ELEMENTS USING LOOPS: 
An easier and faster way of accessing array elements in using loops. The following 
examples shows how array elements can be accessed using for loop. 
int marks[5]; 
for(inti=0; i<5; i++) 
marks[i]=i; 
The above example uses for loop to store different values in the array .It uses the counter 
variable ’i’ as an index. In each iteration, the value of ’i’ is changed. The statement 
marks[i] refers to different array element in each iteration. 
INPUT AND OUTPUT VALUES OF AN ARRAY: 
The process of input and output with arrays is similar to the Input and output with simple 
variables. The cin object is used to input values in the arrays. The cout object is used to 
display values of arrays. 
USES OF ARRAYS: 
Some uses of arrays are as follows: 
 Arrays can store a large number of values with single name. 
 Arrays are used to process many values easily and quickly. 
 The values stored in arrays can be sorted easily. 
 A search process can be applied on arrays easily. 
 Array can perform matrix operations 
ARRAY DATA TYPES: 
Data type indicates the value to be stored in the array. 
Int It stores integer data and its capacity is 4 bytes (32 bits). 
Float It stores integeras well as fractional data its capcity is 4 bytes (32 bits). 
Char It stores characters (alphabets) its capacity is 1 byte (8 bits). 
Double It also stores fractional data like float its capacity is 4 bytes.
3 
TYPES OF ARRAY: 
Arrays can of following types: 
I. Single-Dimensional Array. (1D array ) 
II. Multi-Dimensional Array. (2D array ) 
SINGLE-DIMENSIONAL ARRAY: 
A type of array in which all elements are arranged in the form of a list is known as one 
dimensional array. It is also called one-dimensional array. It consists of one column or 
one row. The elements are stored in consecutive memory locations. E.g. A [1], A [2]... A 
[N]. 
SYNTAX: 
The syntax of declaring one-dimensional array is as follows: 
Data type _identifier[length]; 
Data _Type: It indicates the data types of the values to be stored in the array. 
Identifier: It indicates the name of the array. 
Length: It indicates total number of elements in the array. It must be a literal 
constant or symbolic constant. 
EXAMPLE: 
int x[7]; 
It allocates seven consecutive locations in memory. The index of first element is 0 and 
index of last element is 6. 
MULTI-DIMENSIONAL ARRAY: 
In multi-dimensional array there is two-dimensional array which can be considered as a 
table that consists of rows and column. Each element in 2-D array is referred with the 
help of two indexes. One index is used to indicate the row and the second index indicates 
the column of the element.. It is also called matrix array because in it the elements form a 
matrix. E.g. A [2] [3] has 2 rows and 3 columns and 2*3 = 6 elements.
4 
SYNTAX: 
The syntax of declaring two-dimensional array is as follows: 
Data type _identifier[Rows][column]; 
Data _Type: It indicates the data types of the values to be stored in the array. 
Identifier: It indicates the name of the array. 
Rows: It indicates number of rows in the array. It must be a literal constant or 
symbolic constant. 
column: It indicates number of column in the array. It must be a literal constant. 
EXAMPLE: 
intArr[3][4]; 
The statement declares a two-dimensional array. The first index indicates array contain 
three rows. The index of first row is 0 and the index of last row is 2. The second index 
indicates that each row in the array contains three columns. The index of first column is 0 
and the index of last column is 3. The total number of elements can be determined by 
multiplying rows and columns. It means that array contains twelve element
5 
PROGRAMS OF ARRAY 
1. Write a program to find the highest and second highest number? Using array. 
DESCRIPTION: 
In this program we find highest and second highest number. In the program we 
will the input from user and use FOR loop statement to completing the program. 
INPUT: 
Enter 0 Number 
Enter 1 Number 
Enter 2 Number 
Enter 3 Number 
Enter 4 Number 
METHOD: 
This program will take an integer array as input and by using FOR loop it will give highest and 
second highest as output 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a[5],highest=0,secondhighest=0; 
for (int i=0; i<5; i++) 
{ 
cout<<" Enter "<<i<<" Number ="; 
cin>>a[i]; 
} 
for (int j=0; j<5; j++) 
{ 
if (a[j] > highest) 
{ 
highest = a[j]; 
} 
} 
cout<<"Highest Number is ="<<highest<<"n"; 
for (int k=0; k<5; k++) 
{ 
if (a[k] > secondhighest && a[k] < highest) 
{ 
secondhighest = a[k]; 
} 
} 
cout<<"Second Highest Number is =" 
cout<<secondhighest<<"n"; 
getch(); 
}
6 
OUTPUT: 
Enter 0 Number =90 
Enter 1 Number =7 
Enter 2 Number =60 
Enter 3 Number =55 
Enter 4 Number =40 
Highest Number is =90 
Second Highest Number is =70 
2. Write a program to find highest and lowest number? Using array. 
DESCRIPTION: 
In this program we will find the highest and lowest numbers. In that program the input from 
user and use FOR loop statement to completing the program. 
INPUT: 
Enter 0 Number, Enter 1 Number, Enter 2 Number, Enter 3 Number, Enter 4 Number 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
int a[5]; 
for (int i=0; i<5; i++) 
{ 
cout<<" Enter "<<i<<" Number = "; 
cin>>a[i]; 
cout<<"n"; 
} 
int highest=a[0],lowest=a[0]; 
for (int j=0; j<5; j++) 
{ 
if (a[j] > highest) 
{ 
highest = a[j]; 
} 
} 
cout<<"Highest Number is ="; 
cout<<highest<<”nn”; 
for (int k=0; k<5; k++) 
{ 
if (a[k] < lowest) 
{ 
lowest = a[k]; 
} 
} 
cout<<"Lowest Number is ="<<lowest<<”nn”; 
getch(); 
}
7 
OUTPUT: 
Enter 0 Number =1000 
Enter 1 Number =899 
Enter 2 Number =678 
Enter 3 Number =45 
Enter 4 Number =55 
Highest Number is =1000 
Lowest Number is =45 
3. Write a program to find the average? Using array 
DESCRIPTION: 
In this program we will find average (means that sum of total numbers divided by 
total numbers) of the number with the help of FOR loop statement. 
INPUT: 
Enter Number (This message will be displayed 5 times) 
METHOD: 
This program will take integer array i-e.; m [5] as input and by using FOR loop it will 
give average by finding first the sum of the numbers given by user and then dividing by total 
number of values as output. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main () 
{ 
clrscr(); 
int m[5]; 
float average; 
for (int s=0; s<5; s++) 
{ 
cout<<"Enter Number "<<s<<" = "; 
cin>>m[s]; 
cout<<"n"; 
} 
float z=0; 
for (int i=0; i<5; i++) 
{ 
z=z+m[i]; 
} 
average=z/5; 
cout<<"Average is ="<<average<<"n"; 
getch(); 
}
8 
OUTPUT: 
Enter Number = 50 
Enter Number= 50 
Enter Number= 50 
Enter Number=50 
Enter Number=50 
Average is = 50 
4. Write a program to find the marks of math and declare that how many students pass 
and fail in math subject? Using array. 
DESCRIPTION: 
In this question we type the program in c++ for the marks of math student and show 
the result that how many student are passed in math and how many student are failed in math 
with the help of FOR loop statement. 
INPUT: 
Enter Marks of Student No 0 of Math 
Enter Marks of Student No 1 of Math 
Enter Marks of Student No 2 of Math 
Enter Marks of Student No 3 of Math 
Enter Marks of Student No 4 of Math 
METHOD: 
This program will take integer array as input. user enter the numbers of mathematics of five 
students in that array then by using FOR loop and IF ELSE statement it will give output of total 
number of PASSED and FAILED students. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int s[5]; 
for (int j=0; j<5; j++) 
{ 
if (s[j] >=50) 
{ 
pass=pass+1; 
}
9 
for (int i=0; i<5; i++) 
{ 
cout<<"Enter Marks of Student No " 
cout<<i<<" of math"<<" = "; 
cin>>s[i]; 
cout<<"n"; 
} 
int pass=0,fail=0; 
else 
{ 
fail=fail+1; 
} 
} 
cout<<"passed student are ="<<pass<<”nn”; 
cout<<"failed student are ="<<fail<<”nn”; 
getch(); 
} 
OUTPUT: 
Enter Marks of Student No 0 of Math = 78 
Enter Marks of Student No 1 of Math = 35 
Enter Marks of Student No 2 of Math = 88 
Enter Marks of Student No 3 of Math = 67 
Enter Marks of Student No 4 of Math = 23 
passed student are = 3 
failed student are = 2 
5. Write a program to find the multiple of matrix? Using Array. 
DESCRIPTION: 
In this program we multiply the matrix (2*2) in the array by using FOR loop statement. 
INPUT: 
Multiplication of Matrix is (This message will be displayed only one time) 
PROGRAM:
10 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a[3][3],b[3][3],i,j,k,sum; 
cout<<"First Matrix"<<“n”; 
for(i=0;i<3;i++) 
{ 
for(j=0;j<3;j++) 
{ 
cout<<"Enter number :"; 
cin>>a[i][j]; 
} 
} 
cout<<"Second Matrix"<<“n”; 
for(i=0;i<3;i++) 
{ 
for(j=0;j<3;j++) 
{ 
cout<<"Enter number :"; 
METHOD: 
cin>>b[i][j]; 
} 
} 
cout<<"Multiplication is"<<“n”; 
for(i=0;i<3;i++) 
{ 
for(j=0;j<3;j++) 
{ 
sum=0; 
for(k=0;k<3;k++) 
{ 
sum=sum,+a[i][k]*b[k][j]; 
} 
cout<<sum<<"t"; 
} 
cout<<“n”; 
} 
getch(); 
} 
In this program we first enter the elements 9 times for each matrix. There variable “i” is 
used for the rows and variable “j” is used for the columns and the sum variable is used to 
find the individual product of each element as the multiplication formula is “ 1st rows 
elements of 1st matrix * 1st column elements of 2nd matric and add them together. After 
calculation product is displayed at the end ./t is used to give 8 spaces.
11 
OUTPUT: 
First Matrix 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Second Matrix 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Enter the number1 
Multiplication is 
3 3 3 
3 3 3 
3 3 3 
6. Write a program for the addition of matrix? Using Array. 
DESCRIPTION:
In this program we will add the matric (2x2) in the array by using FOR loop statement. 
12 
INPUT: 
First Matrix 
Enter Element (This message will be displayed 4 times) 
Second Matrix 
Enter Element (This message will also be displayed 4 times) 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
clrscr(); 
int a[2][2],b[2][2],c[2][2], i, j; 
cout<<" First Matrix "<<"n"; 
for(i=0;i<2;i++) 
{ 
for(j=0;j<2;j++) 
{ 
cout<<"Enter Element = "; 
cin>>a[i][j]; 
} 
} 
cout<<" Second Matrix "<<"n"; 
for(i=0;i<2;i++) 
{ 
for(j=0;j<2;j++) 
{ 
cout<<"Enter Element = "; 
cin>>b[i][j]; 
} 
} 
cout<<" Addition of Matrix "<<"n"; 
for(i=0;i<2;i++) 
{ 
for(j=0;j<2;j++) 
{ 
c[i][j] = a[i][j] + b[i][j]; 
cout<<c[i][j]<<" "; 
} 
cout<<"nn"; 
} 
getch(); 
} 
METHOD: 
In this program we first enter the elements 4 times for each matrix in two arrays i-e.; 
a[2][2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is added with 
1st element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . Program will 
give one matrix after addition of two as output. 
OUTPUT:
13 
First Matrix 
Enter Element = 1 
Enter Element = 2 
Enter Element = 1 
Enter Element = 1 
Second Matrix 
Enter Element = 1 
Enter Element = 1 
Enter Element = 1 
Enter Element = 1 
Addition of matrix 
2 3 
2 2 
7. Write a program for the subtraction of matrix? Using Array. 
DESCRIPTION: 
In this program we subtract the matrix in the array by using FOR loop statement. 
INPUT: 
First Matrix 
Enter Element (This message will be displayed 4 times) 
Second Matrix 
Enter Element (This message will also be displayed 4 times) 
METHOD: 
In this program we first enter the elements 4 times for each matrix in two arrays i-e.; 
a[2][2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is subtracted 
with 1st element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . 
Program will give one matrix c[2][2] after subtraction of two as output. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
clrscr(); 
int a[2][2],b[2][2],c[2][2], i, j; 
cout<<" First Matrix "<<"n"; 
cout<<"Enter Element = "; 
cin>>b[i][j]; 
} 
} 
cout<<" Subtraction of Matrix "<<"n"; 
for(i=0;i<2;i++) 
{
14 
for(i=0;i<2;i++) 
{ 
for(j=0;j<2;j++) 
{ 
cout<<"Enter Element = "; 
cin>>a[i][j]; 
} 
} 
cout<<" Second Matrix "<<"n"; 
for(i=0;i<2;i++) 
{ 
for(j=0;j<2;j++) 
{ 
for(j=0;j<2;j++) 
{ 
c[i][j] = a[i][j] + b[i][j]; 
cout<<c[i][j]<<" "; 
} 
cout<<"nn"; 
} 
getch(); 
} 
OUTPUT: 
First Matrix 
Enter Element = 1 
Enter Element = 2 
Enter Element = 1 
Enter Element = 1 
Second Matrix 
Enter Element = 1 
Enter Element = 1 
Enter Element = 1 
Enter Element = 1 
Subtraction of matrix 
0 1 
0 0 
8. Write a program that inputs values from the user, stores them in an array and displays 
the sum. 
DESCRIPTION: 
This program will take the input of 5 numbers from the user and by using for loop in 
array it will calculate the sum of these numbers and then find the average by dividing the 
sum of values by the number of values.
15 
INPUT: 
Enter value: (This message will be displayed 5 times) 
METHOD: 
The above example initializes the variable sum to 0. The first loop gets five inputs. The 
second loop again visits all elements of the array. In first iteration, the value of counter 
variable is 0. So it adds the value of first elements and the value of Sum and stores the 
result in sum. In second iteration, it adds the value of second element and sum and then 
stores the result back to sum and so on. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
int arr[5], i, sum=0; 
for(i=0;i<5;i++) 
{ 
cout<<”Enter value=”; 
OUTPUT: 
cin>>arr[i]; 
} 
for(i=0;i<5;i++) 
{ 
sum=sum+arr[i]; 
} 
cout<<”sum is=”<<sum<<“n”; 
getch(); 
} 
Enter value=3 
Enter value=7 
Enter value=5 
Enter value=9 
Enter value=6 
Sum is=30
9. Write a program that inputs the age of different users and counts the number of 
16 
persons in the age group of 50 to 60. 
DESCRIPTION: 
This program will take the input from the user and the number of users will also be taken 
at run time. After this it will asks for the ages of people. By using for loop and IF 
Statement in array it will display the number of persons in the age group 50 and 60 if the 
condition satisfies. 
INPUT: 
Enter the number of person required= (This message will be displayed as much time as 
user required) 
Enter ages of persons = (This will take the ages of persons) 
METHOD: 
In this program the user first enters the number of person and their ages with the help of 
one-dimensional array in the loop and by applying if condition user easily determine the 
ages in between 50 and 60. Taking “sum” as a variable equals to zero as condition 
satisfied in the if statement the value of sum increment and at the end it stores a value 
which shows the ages of person which is in between 50 and 60. 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
int age[10], i, n, sum=0; 
cout<<”Enter the number of person 
required=”; 
cin>>n; 
cout<<”Enter ages 
of”<<n<<”persons=”<<“n”; 
for(i=0; i<n; i++) 
{ 
cin>>age[i]; 
} 
for(int z=0; z<n; z++) 
{ 
if(age[z]>=50 && age[z]<=60) 
{ 
Sum=sum+1; 
} 
cout<<sum<<”persons are between 50 and 
60.”; 
getch(); 
}
17 
OUTPUT: 
10. Write a program that input values from the user and stores them in an array. It 
displays the location of those values. 
DESCRIPTION: 
This program will take the input from the user and it will displays the location of that 
element stored in that array by using For Loop. 
INPUT: 
Enter values to find their location= (This message will take the input from user 5 times) 
METHOD: 
In this program the user enters number in the array and it stores in array with their 
specific locations. The loop searches the array stores the value of their index. The 
program checks the value that is stored over it and by taking a new variable which is 
equal to the loop in which the value is stored we can easily determine their locations. 
PROGRAM: 
Enter the number of person required=3 
Enter ages of 3 persons= 
51 
55 
34 
2 persons are between 50 and 60.
18 
OUTPUT: 
#include<iostream.h> 
#include<conio.h> 
void main() 
11. Write a program that inputs the marks of 3 students in three different subjects 
.If student is passed in two subjects then he will be considered pass and displays 
the number of pass students and also displays the number of students who fail. 
{ 
Int a[5], i, locat, h; 
Cout<<”Enter values to find their 
location=”; 
for(i= 0; i<5; i++) 
{ 
cin>>a[i]; 
} 
for(int d=0; d<5; d++) 
{ 
h=a[d]; 
locat=d; 
cout<<”The value “<<h<<” is in“<<locat 
cout<<” location”; 
} 
getch(); 
} 
Enter values to find their location= 
23 
45 
12 
56 
78 
The value 23 is in 0 location. 
The value 45 is in 1 location. 
The value 12 is in 2 location. 
The value 56 is in 3 location. 
The value 78 is in 4 location.
19 
DESCRIPTION: 
This program will take the input the marks of 3 students of their any 3 subjects .Then it 
will check that the student is passed or not by using IF Condition .If student is passed in 
two subjects then he will be considered pass .It will also use a counter to calculate the 
number of students who are passed or fail.by using Nested For Loop and IF Condition. 
INPUT: 
Enter 1 student marks in 1 subject = 
Enter 1 student marks in 2 subject = 
Enter 1 student marks in 3 subject = 
(This message will displayed 3 times) 
METHOD: 
In the above program user inputs the marks of students in three subjects with the help of 
2-D array and by using the loops. At start we first apply if condition in the subject loop 
that if marks of student is equal to 50 or greater than 50 then a variable “s” is increment 
similarly if less than 50 or equal to 50 than the variable ”f”is decrement same procedure 
is apply in the student loop that if the variable ”s” is greater than 2 or equals to 2 then a 
new variable “u” is increment which shows the number of pass students and remaining 
goes to the else condition in which the variable “f” is increment which shows the number 
of fail students. 
PROGRAM:
20 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
int a[15][3]; 
for(int i=1; i<=3; i++) 
{ 
for (int z=1; z<=3; z++) 
{ 
cout<<”Enter”<<i<<” student marks in 
“<<z<<” subject =”; 
cin>>a[i][z]; 
} 
cout<<”n”; 
} 
int u=0,h=0; 
for(int y=1; y<=3; y++) 
{ 
int s=0,f=0; 
for(int r=1; r<=3; r++) 
{ 
If( a[y][r]>=50) 
{ 
s=s+1; 
} 
else 
{ 
f=f+1; 
} 
} 
if(s>=2) 
{ 
u=u+1; 
} 
else 
{ 
h=h+1; 
} 
} 
cout<<”pass student =”<<u; 
cout<<“n”; 
cout<<”fail student =”<<h; 
getch(); 
}
21 
OUTPUT: 
Enter 1 student marks in 1 subject =98 
Enter 1 student marks in 2 subject =56 
Enter 1 student marks in 3 subject =23 
Enter 2student marks in 1 subject =23 
Enter 2 student marks in 2 subject =34 
Enter 2 student marks in 3 subject =67 
Enter 3 student marks in 1 subject =85 
Enter 3 student marks in 2 subject =56 
Enter 3 student marks in 3 subject =67 
pass student =2 
fail student =1 
12. Write a program for enter the marks of math, physics, chemistry and show the 
result that how many student are passed and failed in these subject? Using 
Array. 
DESCRIPTION: 
In the program we use FOR loop and IF Else statement to enter the marks of 
student of these subject and declare that these student are passed and these student are 
failed. 
INPUT: 
Enter Math Numbers (This message will displayed 5 times) 
Enter Physics Numbers (This message will also displayed 5 times) 
Enter Chemistry Numbers (This message will also displayed 5 times)
METHOD: 
In this program user will enter the marks of three subjects of 4 students. By used 
FOR loop and IF ELSE statement program will give number of failed and passed 
student of each subject as output. 
22 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int math[4],physics[4],chemistry[4]; 
for (int i=0; i<4; i++) 
{ 
cout<<"Enter Math Numbers ="; 
cin>>math[i]; 
cout<<"Enter Physics Numbers ="; 
cin>>physics[i]; 
cout<<"Enter Chemistry Numers ="; 
cin>>chemistry[i]; 
} 
cout<<"n"; 
int mp=0,mf=0,pp=0,pf=0,cp=0,cf=0; 
for (int j=0; j<4; j++) 
{ 
if (math[j] >=50) 
{ 
mp=mp+1; 
} 
else 
{ 
mf=mf+1; 
} 
if (physics[j] >=50) 
{ 
pp=pp+1; 
} 
OUTPUT: 
else 
{ 
pf=pf+1; 
} 
if (chemistry[j] >=50) 
{ 
cp=cp+1; 
} 
else 
{ 
cf=cf+1; 
} 
} 
cout<<"passed student for math 
="<<mp<<"n"; 
cout<<"failed student for math=" 
cout <<mf<<"n"; 
cout<<"passed student for physics =" 
cout<<pp<<"n"; 
cout<<"failed student for physics =" 
cout<<pf<<"n"; 
cout<<"passed student for chemistry =" 
cout<<cp<<"n"; 
cout<<"failed student for chemistry =" 
cout<<cf<<"n"; 
getch(); 
}
23 
Enter Math Numbers =78 
Enter Physics Numbers =12 
Enter Chemistry Numbers =34 
Enter Math Numbers =90 
Enter Physics Numbers =33 
Enter Chemistry Numbers =23 
Enter Math Numbers =99 
Enter Physics Numbers =78 
Enter Chemistry Numbers =77 
Enter Math Numbers =45 
Enter Physics Numbers =66 
Enter Chemistry Numbers =20 
Passed student for math =3 
failed student for math =1 
Passed student for physics =2 
failed student for physics =2 
Passed student for chemistry =1 
failed student for chemistry =3 
13. Write a program to swap the numbers? Using Array. 
DESCRIPTION: 
In this program we will change the location of number with the help of 
FOR and IF Else statement. 
INPUT: 
Enter the Number A
24 
Enter the Number B 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a[2],c; 
for (int i=0; i<2; i++) 
{ 
METHOD: 
else 
{ 
cout<<"Enter the Number B ="; 
cin>>a[i]; 
} 
c=a[0]; 
a[0]=a[1]; 
a[1]=c; 
This program will get an integer array as input of two values and BY using FOR loop and IF ELSE 
statement it will swap the numbers with each other and give output as shown below. 
PROGRAM:
25 
if (i==0) 
{ 
cout<<"Enter the Number A = "; 
} 
Output: 
cout<<"n"<<"After Swapping"<<"n"; 
cout<<"A is = "<<a[0]<<"n"; 
cout<<"B is = "<<a[1]<<"n"; 
getch(); 
} 
Enter the Number A = 20 
Enter the Number B = 10 
After the swapping 
A is = 10 
B is = 20 
B is = 20 
Enter the Number A = 20 
Enter the Number B = 10 
After the swapping 
A is = 10 B is = 20 
B is = 20
Ad

More Related Content

What's hot (20)

STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointer arithmetic in c
Pointer arithmetic in c Pointer arithmetic in c
Pointer arithmetic in c
sangrampatil81
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
Character set of c
Character set of cCharacter set of c
Character set of c
Chandrapriya Rediex
 
Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Array in C
Array in CArray in C
Array in C
Kamal Acharya
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
priya Nithya
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
Ankur Pandey
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointer arithmetic in c
Pointer arithmetic in c Pointer arithmetic in c
Pointer arithmetic in c
sangrampatil81
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
Modes of transfer - Computer Organization & Architecture - Nithiyapriya Pasav...
priya Nithya
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
Ankur Pandey
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 

Similar to Array assignment (20)

Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
Arrays
ArraysArrays
Arrays
Neeru Mittal
 
Arrays
ArraysArrays
Arrays
Chukka Nikhil Chakravarthy
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
rohassanie
 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
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
Appili Vamsi Krishna
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Arrays
ArraysArrays
Arrays
Notre Dame of Midsayap College
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Arrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.pptArrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Array
ArrayArray
Array
hjasjhd
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
Ad

Recently uploaded (20)

Setup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODCSetup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODC
outsystemspuneusergr
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Rajdeep Chakraborty
 
The Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdfThe Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdf
RDinuRao
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
Bidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdfBidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdf
ISGF - International Scout and Guide Fellowship
 
Approach to diabetes Mellitus, diagnosis
Approach to diabetes Mellitus,  diagnosisApproach to diabetes Mellitus,  diagnosis
Approach to diabetes Mellitus, diagnosis
Mohammed Ahmed Bamashmos
 
Bloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptxBloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptx
FamilyWorshipCenterD
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
A Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity SequencesA Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity Sequences
natarajan8993
 
Updated treatment of hypothyroidism, causes and symptoms
Updated treatment of hypothyroidism,  causes and symptomsUpdated treatment of hypothyroidism,  causes and symptoms
Updated treatment of hypothyroidism, causes and symptoms
Mohammed Ahmed Bamashmos
 
Bidding World Conference 2027-NSGF Senegal.pdf
Bidding World Conference 2027-NSGF Senegal.pdfBidding World Conference 2027-NSGF Senegal.pdf
Bidding World Conference 2027-NSGF Senegal.pdf
ISGF - International Scout and Guide Fellowship
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Profit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdfProfit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdf
TheodoreHawkins
 
kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
Bidding World Conference 2027 - Ghana.pptx
Bidding World Conference 2027 - Ghana.pptxBidding World Conference 2027 - Ghana.pptx
Bidding World Conference 2027 - Ghana.pptx
ISGF - International Scout and Guide Fellowship
 
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptxLec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
TayyabaSiddiqui12
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx
Dale Wells
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
Setup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODCSetup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODC
outsystemspuneusergr
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Prelims.pptx
Rajdeep Chakraborty
 
The Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdfThe Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdf
RDinuRao
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
Bloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptxBloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptx
FamilyWorshipCenterD
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
A Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity SequencesA Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity Sequences
natarajan8993
 
Updated treatment of hypothyroidism, causes and symptoms
Updated treatment of hypothyroidism,  causes and symptomsUpdated treatment of hypothyroidism,  causes and symptoms
Updated treatment of hypothyroidism, causes and symptoms
Mohammed Ahmed Bamashmos
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Profit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdfProfit Growth Drivers for Small Business.pdf
Profit Growth Drivers for Small Business.pdf
TheodoreHawkins
 
kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptxLec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
Lec 3 - Chapter 2 Carl Jung’s Theory of Personality.pptx
TayyabaSiddiqui12
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx
Dale Wells
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
Ad

Array assignment

  • 1. 1 ARRAY: An array is a “contiguous memory location*” Contiguous memory location means “ memory blocks having consecutive addresses”. An array is a group of consecutive memory location with same name and type. Simple variable is a single memory location with unique name and a type. The memory locations in the array are known as elements of array. The total number of elements in the array is called its length. Each element in the array is accessed with reference to its position of the location in the array. This position is called index. Each element in the array has a unique index. The index of first element is 0 and the index of last element is length. The value of the index is written in brackets along with the name of the array. An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. ARRAY DECLARATION: The process of specifying array name, length, and data type is called array declaration. Syntax = Data type _name of array[length];
  • 2. 2 ARRAY INITIALIZATION: The process of assigning values to array elements at the time of array declaration is called array initialization. The initialization process provides a list of initial values for array elements. The values are separated with commas and enclosed within braces. Syntax = Data type _name of array[length] = {list of values}; ACCESSING ARRAY ELEMENTS USING LOOPS: An easier and faster way of accessing array elements in using loops. The following examples shows how array elements can be accessed using for loop. int marks[5]; for(inti=0; i<5; i++) marks[i]=i; The above example uses for loop to store different values in the array .It uses the counter variable ’i’ as an index. In each iteration, the value of ’i’ is changed. The statement marks[i] refers to different array element in each iteration. INPUT AND OUTPUT VALUES OF AN ARRAY: The process of input and output with arrays is similar to the Input and output with simple variables. The cin object is used to input values in the arrays. The cout object is used to display values of arrays. USES OF ARRAYS: Some uses of arrays are as follows:  Arrays can store a large number of values with single name.  Arrays are used to process many values easily and quickly.  The values stored in arrays can be sorted easily.  A search process can be applied on arrays easily.  Array can perform matrix operations ARRAY DATA TYPES: Data type indicates the value to be stored in the array. Int It stores integer data and its capacity is 4 bytes (32 bits). Float It stores integeras well as fractional data its capcity is 4 bytes (32 bits). Char It stores characters (alphabets) its capacity is 1 byte (8 bits). Double It also stores fractional data like float its capacity is 4 bytes.
  • 3. 3 TYPES OF ARRAY: Arrays can of following types: I. Single-Dimensional Array. (1D array ) II. Multi-Dimensional Array. (2D array ) SINGLE-DIMENSIONAL ARRAY: A type of array in which all elements are arranged in the form of a list is known as one dimensional array. It is also called one-dimensional array. It consists of one column or one row. The elements are stored in consecutive memory locations. E.g. A [1], A [2]... A [N]. SYNTAX: The syntax of declaring one-dimensional array is as follows: Data type _identifier[length]; Data _Type: It indicates the data types of the values to be stored in the array. Identifier: It indicates the name of the array. Length: It indicates total number of elements in the array. It must be a literal constant or symbolic constant. EXAMPLE: int x[7]; It allocates seven consecutive locations in memory. The index of first element is 0 and index of last element is 6. MULTI-DIMENSIONAL ARRAY: In multi-dimensional array there is two-dimensional array which can be considered as a table that consists of rows and column. Each element in 2-D array is referred with the help of two indexes. One index is used to indicate the row and the second index indicates the column of the element.. It is also called matrix array because in it the elements form a matrix. E.g. A [2] [3] has 2 rows and 3 columns and 2*3 = 6 elements.
  • 4. 4 SYNTAX: The syntax of declaring two-dimensional array is as follows: Data type _identifier[Rows][column]; Data _Type: It indicates the data types of the values to be stored in the array. Identifier: It indicates the name of the array. Rows: It indicates number of rows in the array. It must be a literal constant or symbolic constant. column: It indicates number of column in the array. It must be a literal constant. EXAMPLE: intArr[3][4]; The statement declares a two-dimensional array. The first index indicates array contain three rows. The index of first row is 0 and the index of last row is 2. The second index indicates that each row in the array contains three columns. The index of first column is 0 and the index of last column is 3. The total number of elements can be determined by multiplying rows and columns. It means that array contains twelve element
  • 5. 5 PROGRAMS OF ARRAY 1. Write a program to find the highest and second highest number? Using array. DESCRIPTION: In this program we find highest and second highest number. In the program we will the input from user and use FOR loop statement to completing the program. INPUT: Enter 0 Number Enter 1 Number Enter 2 Number Enter 3 Number Enter 4 Number METHOD: This program will take an integer array as input and by using FOR loop it will give highest and second highest as output PROGRAM: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5],highest=0,secondhighest=0; for (int i=0; i<5; i++) { cout<<" Enter "<<i<<" Number ="; cin>>a[i]; } for (int j=0; j<5; j++) { if (a[j] > highest) { highest = a[j]; } } cout<<"Highest Number is ="<<highest<<"n"; for (int k=0; k<5; k++) { if (a[k] > secondhighest && a[k] < highest) { secondhighest = a[k]; } } cout<<"Second Highest Number is =" cout<<secondhighest<<"n"; getch(); }
  • 6. 6 OUTPUT: Enter 0 Number =90 Enter 1 Number =7 Enter 2 Number =60 Enter 3 Number =55 Enter 4 Number =40 Highest Number is =90 Second Highest Number is =70 2. Write a program to find highest and lowest number? Using array. DESCRIPTION: In this program we will find the highest and lowest numbers. In that program the input from user and use FOR loop statement to completing the program. INPUT: Enter 0 Number, Enter 1 Number, Enter 2 Number, Enter 3 Number, Enter 4 Number PROGRAM: #include<iostream.h> #include<conio.h> void main() { int a[5]; for (int i=0; i<5; i++) { cout<<" Enter "<<i<<" Number = "; cin>>a[i]; cout<<"n"; } int highest=a[0],lowest=a[0]; for (int j=0; j<5; j++) { if (a[j] > highest) { highest = a[j]; } } cout<<"Highest Number is ="; cout<<highest<<”nn”; for (int k=0; k<5; k++) { if (a[k] < lowest) { lowest = a[k]; } } cout<<"Lowest Number is ="<<lowest<<”nn”; getch(); }
  • 7. 7 OUTPUT: Enter 0 Number =1000 Enter 1 Number =899 Enter 2 Number =678 Enter 3 Number =45 Enter 4 Number =55 Highest Number is =1000 Lowest Number is =45 3. Write a program to find the average? Using array DESCRIPTION: In this program we will find average (means that sum of total numbers divided by total numbers) of the number with the help of FOR loop statement. INPUT: Enter Number (This message will be displayed 5 times) METHOD: This program will take integer array i-e.; m [5] as input and by using FOR loop it will give average by finding first the sum of the numbers given by user and then dividing by total number of values as output. PROGRAM: #include<iostream.h> #include<conio.h> void main () { clrscr(); int m[5]; float average; for (int s=0; s<5; s++) { cout<<"Enter Number "<<s<<" = "; cin>>m[s]; cout<<"n"; } float z=0; for (int i=0; i<5; i++) { z=z+m[i]; } average=z/5; cout<<"Average is ="<<average<<"n"; getch(); }
  • 8. 8 OUTPUT: Enter Number = 50 Enter Number= 50 Enter Number= 50 Enter Number=50 Enter Number=50 Average is = 50 4. Write a program to find the marks of math and declare that how many students pass and fail in math subject? Using array. DESCRIPTION: In this question we type the program in c++ for the marks of math student and show the result that how many student are passed in math and how many student are failed in math with the help of FOR loop statement. INPUT: Enter Marks of Student No 0 of Math Enter Marks of Student No 1 of Math Enter Marks of Student No 2 of Math Enter Marks of Student No 3 of Math Enter Marks of Student No 4 of Math METHOD: This program will take integer array as input. user enter the numbers of mathematics of five students in that array then by using FOR loop and IF ELSE statement it will give output of total number of PASSED and FAILED students. PROGRAM: #include<iostream.h> #include<conio.h> void main() { clrscr(); int s[5]; for (int j=0; j<5; j++) { if (s[j] >=50) { pass=pass+1; }
  • 9. 9 for (int i=0; i<5; i++) { cout<<"Enter Marks of Student No " cout<<i<<" of math"<<" = "; cin>>s[i]; cout<<"n"; } int pass=0,fail=0; else { fail=fail+1; } } cout<<"passed student are ="<<pass<<”nn”; cout<<"failed student are ="<<fail<<”nn”; getch(); } OUTPUT: Enter Marks of Student No 0 of Math = 78 Enter Marks of Student No 1 of Math = 35 Enter Marks of Student No 2 of Math = 88 Enter Marks of Student No 3 of Math = 67 Enter Marks of Student No 4 of Math = 23 passed student are = 3 failed student are = 2 5. Write a program to find the multiple of matrix? Using Array. DESCRIPTION: In this program we multiply the matrix (2*2) in the array by using FOR loop statement. INPUT: Multiplication of Matrix is (This message will be displayed only one time) PROGRAM:
  • 10. 10 #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[3][3],b[3][3],i,j,k,sum; cout<<"First Matrix"<<“n”; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<"Enter number :"; cin>>a[i][j]; } } cout<<"Second Matrix"<<“n”; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<"Enter number :"; METHOD: cin>>b[i][j]; } } cout<<"Multiplication is"<<“n”; for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=0; for(k=0;k<3;k++) { sum=sum,+a[i][k]*b[k][j]; } cout<<sum<<"t"; } cout<<“n”; } getch(); } In this program we first enter the elements 9 times for each matrix. There variable “i” is used for the rows and variable “j” is used for the columns and the sum variable is used to find the individual product of each element as the multiplication formula is “ 1st rows elements of 1st matrix * 1st column elements of 2nd matric and add them together. After calculation product is displayed at the end ./t is used to give 8 spaces.
  • 11. 11 OUTPUT: First Matrix Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Second Matrix Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Multiplication is 3 3 3 3 3 3 3 3 3 6. Write a program for the addition of matrix? Using Array. DESCRIPTION:
  • 12. In this program we will add the matric (2x2) in the array by using FOR loop statement. 12 INPUT: First Matrix Enter Element (This message will be displayed 4 times) Second Matrix Enter Element (This message will also be displayed 4 times) PROGRAM: #include<iostream.h> #include<conio.h> int main() { clrscr(); int a[2][2],b[2][2],c[2][2], i, j; cout<<" First Matrix "<<"n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"Enter Element = "; cin>>a[i][j]; } } cout<<" Second Matrix "<<"n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"Enter Element = "; cin>>b[i][j]; } } cout<<" Addition of Matrix "<<"n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j] = a[i][j] + b[i][j]; cout<<c[i][j]<<" "; } cout<<"nn"; } getch(); } METHOD: In this program we first enter the elements 4 times for each matrix in two arrays i-e.; a[2][2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is added with 1st element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . Program will give one matrix after addition of two as output. OUTPUT:
  • 13. 13 First Matrix Enter Element = 1 Enter Element = 2 Enter Element = 1 Enter Element = 1 Second Matrix Enter Element = 1 Enter Element = 1 Enter Element = 1 Enter Element = 1 Addition of matrix 2 3 2 2 7. Write a program for the subtraction of matrix? Using Array. DESCRIPTION: In this program we subtract the matrix in the array by using FOR loop statement. INPUT: First Matrix Enter Element (This message will be displayed 4 times) Second Matrix Enter Element (This message will also be displayed 4 times) METHOD: In this program we first enter the elements 4 times for each matrix in two arrays i-e.; a[2][2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is subtracted with 1st element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . Program will give one matrix c[2][2] after subtraction of two as output. PROGRAM: #include<iostream.h> #include<conio.h> int main() { clrscr(); int a[2][2],b[2][2],c[2][2], i, j; cout<<" First Matrix "<<"n"; cout<<"Enter Element = "; cin>>b[i][j]; } } cout<<" Subtraction of Matrix "<<"n"; for(i=0;i<2;i++) {
  • 14. 14 for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"Enter Element = "; cin>>a[i][j]; } } cout<<" Second Matrix "<<"n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(j=0;j<2;j++) { c[i][j] = a[i][j] + b[i][j]; cout<<c[i][j]<<" "; } cout<<"nn"; } getch(); } OUTPUT: First Matrix Enter Element = 1 Enter Element = 2 Enter Element = 1 Enter Element = 1 Second Matrix Enter Element = 1 Enter Element = 1 Enter Element = 1 Enter Element = 1 Subtraction of matrix 0 1 0 0 8. Write a program that inputs values from the user, stores them in an array and displays the sum. DESCRIPTION: This program will take the input of 5 numbers from the user and by using for loop in array it will calculate the sum of these numbers and then find the average by dividing the sum of values by the number of values.
  • 15. 15 INPUT: Enter value: (This message will be displayed 5 times) METHOD: The above example initializes the variable sum to 0. The first loop gets five inputs. The second loop again visits all elements of the array. In first iteration, the value of counter variable is 0. So it adds the value of first elements and the value of Sum and stores the result in sum. In second iteration, it adds the value of second element and sum and then stores the result back to sum and so on. PROGRAM: #include<iostream.h> #include<conio.h> void main() { int arr[5], i, sum=0; for(i=0;i<5;i++) { cout<<”Enter value=”; OUTPUT: cin>>arr[i]; } for(i=0;i<5;i++) { sum=sum+arr[i]; } cout<<”sum is=”<<sum<<“n”; getch(); } Enter value=3 Enter value=7 Enter value=5 Enter value=9 Enter value=6 Sum is=30
  • 16. 9. Write a program that inputs the age of different users and counts the number of 16 persons in the age group of 50 to 60. DESCRIPTION: This program will take the input from the user and the number of users will also be taken at run time. After this it will asks for the ages of people. By using for loop and IF Statement in array it will display the number of persons in the age group 50 and 60 if the condition satisfies. INPUT: Enter the number of person required= (This message will be displayed as much time as user required) Enter ages of persons = (This will take the ages of persons) METHOD: In this program the user first enters the number of person and their ages with the help of one-dimensional array in the loop and by applying if condition user easily determine the ages in between 50 and 60. Taking “sum” as a variable equals to zero as condition satisfied in the if statement the value of sum increment and at the end it stores a value which shows the ages of person which is in between 50 and 60. PROGRAM: #include<iostream.h> #include<conio.h> void main() { int age[10], i, n, sum=0; cout<<”Enter the number of person required=”; cin>>n; cout<<”Enter ages of”<<n<<”persons=”<<“n”; for(i=0; i<n; i++) { cin>>age[i]; } for(int z=0; z<n; z++) { if(age[z]>=50 && age[z]<=60) { Sum=sum+1; } cout<<sum<<”persons are between 50 and 60.”; getch(); }
  • 17. 17 OUTPUT: 10. Write a program that input values from the user and stores them in an array. It displays the location of those values. DESCRIPTION: This program will take the input from the user and it will displays the location of that element stored in that array by using For Loop. INPUT: Enter values to find their location= (This message will take the input from user 5 times) METHOD: In this program the user enters number in the array and it stores in array with their specific locations. The loop searches the array stores the value of their index. The program checks the value that is stored over it and by taking a new variable which is equal to the loop in which the value is stored we can easily determine their locations. PROGRAM: Enter the number of person required=3 Enter ages of 3 persons= 51 55 34 2 persons are between 50 and 60.
  • 18. 18 OUTPUT: #include<iostream.h> #include<conio.h> void main() 11. Write a program that inputs the marks of 3 students in three different subjects .If student is passed in two subjects then he will be considered pass and displays the number of pass students and also displays the number of students who fail. { Int a[5], i, locat, h; Cout<<”Enter values to find their location=”; for(i= 0; i<5; i++) { cin>>a[i]; } for(int d=0; d<5; d++) { h=a[d]; locat=d; cout<<”The value “<<h<<” is in“<<locat cout<<” location”; } getch(); } Enter values to find their location= 23 45 12 56 78 The value 23 is in 0 location. The value 45 is in 1 location. The value 12 is in 2 location. The value 56 is in 3 location. The value 78 is in 4 location.
  • 19. 19 DESCRIPTION: This program will take the input the marks of 3 students of their any 3 subjects .Then it will check that the student is passed or not by using IF Condition .If student is passed in two subjects then he will be considered pass .It will also use a counter to calculate the number of students who are passed or fail.by using Nested For Loop and IF Condition. INPUT: Enter 1 student marks in 1 subject = Enter 1 student marks in 2 subject = Enter 1 student marks in 3 subject = (This message will displayed 3 times) METHOD: In the above program user inputs the marks of students in three subjects with the help of 2-D array and by using the loops. At start we first apply if condition in the subject loop that if marks of student is equal to 50 or greater than 50 then a variable “s” is increment similarly if less than 50 or equal to 50 than the variable ”f”is decrement same procedure is apply in the student loop that if the variable ”s” is greater than 2 or equals to 2 then a new variable “u” is increment which shows the number of pass students and remaining goes to the else condition in which the variable “f” is increment which shows the number of fail students. PROGRAM:
  • 20. 20 #include<iostream.h> #include<conio.h> void main() { int a[15][3]; for(int i=1; i<=3; i++) { for (int z=1; z<=3; z++) { cout<<”Enter”<<i<<” student marks in “<<z<<” subject =”; cin>>a[i][z]; } cout<<”n”; } int u=0,h=0; for(int y=1; y<=3; y++) { int s=0,f=0; for(int r=1; r<=3; r++) { If( a[y][r]>=50) { s=s+1; } else { f=f+1; } } if(s>=2) { u=u+1; } else { h=h+1; } } cout<<”pass student =”<<u; cout<<“n”; cout<<”fail student =”<<h; getch(); }
  • 21. 21 OUTPUT: Enter 1 student marks in 1 subject =98 Enter 1 student marks in 2 subject =56 Enter 1 student marks in 3 subject =23 Enter 2student marks in 1 subject =23 Enter 2 student marks in 2 subject =34 Enter 2 student marks in 3 subject =67 Enter 3 student marks in 1 subject =85 Enter 3 student marks in 2 subject =56 Enter 3 student marks in 3 subject =67 pass student =2 fail student =1 12. Write a program for enter the marks of math, physics, chemistry and show the result that how many student are passed and failed in these subject? Using Array. DESCRIPTION: In the program we use FOR loop and IF Else statement to enter the marks of student of these subject and declare that these student are passed and these student are failed. INPUT: Enter Math Numbers (This message will displayed 5 times) Enter Physics Numbers (This message will also displayed 5 times) Enter Chemistry Numbers (This message will also displayed 5 times)
  • 22. METHOD: In this program user will enter the marks of three subjects of 4 students. By used FOR loop and IF ELSE statement program will give number of failed and passed student of each subject as output. 22 PROGRAM: #include<iostream.h> #include<conio.h> void main() { clrscr(); int math[4],physics[4],chemistry[4]; for (int i=0; i<4; i++) { cout<<"Enter Math Numbers ="; cin>>math[i]; cout<<"Enter Physics Numbers ="; cin>>physics[i]; cout<<"Enter Chemistry Numers ="; cin>>chemistry[i]; } cout<<"n"; int mp=0,mf=0,pp=0,pf=0,cp=0,cf=0; for (int j=0; j<4; j++) { if (math[j] >=50) { mp=mp+1; } else { mf=mf+1; } if (physics[j] >=50) { pp=pp+1; } OUTPUT: else { pf=pf+1; } if (chemistry[j] >=50) { cp=cp+1; } else { cf=cf+1; } } cout<<"passed student for math ="<<mp<<"n"; cout<<"failed student for math=" cout <<mf<<"n"; cout<<"passed student for physics =" cout<<pp<<"n"; cout<<"failed student for physics =" cout<<pf<<"n"; cout<<"passed student for chemistry =" cout<<cp<<"n"; cout<<"failed student for chemistry =" cout<<cf<<"n"; getch(); }
  • 23. 23 Enter Math Numbers =78 Enter Physics Numbers =12 Enter Chemistry Numbers =34 Enter Math Numbers =90 Enter Physics Numbers =33 Enter Chemistry Numbers =23 Enter Math Numbers =99 Enter Physics Numbers =78 Enter Chemistry Numbers =77 Enter Math Numbers =45 Enter Physics Numbers =66 Enter Chemistry Numbers =20 Passed student for math =3 failed student for math =1 Passed student for physics =2 failed student for physics =2 Passed student for chemistry =1 failed student for chemistry =3 13. Write a program to swap the numbers? Using Array. DESCRIPTION: In this program we will change the location of number with the help of FOR and IF Else statement. INPUT: Enter the Number A
  • 24. 24 Enter the Number B #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[2],c; for (int i=0; i<2; i++) { METHOD: else { cout<<"Enter the Number B ="; cin>>a[i]; } c=a[0]; a[0]=a[1]; a[1]=c; This program will get an integer array as input of two values and BY using FOR loop and IF ELSE statement it will swap the numbers with each other and give output as shown below. PROGRAM:
  • 25. 25 if (i==0) { cout<<"Enter the Number A = "; } Output: cout<<"n"<<"After Swapping"<<"n"; cout<<"A is = "<<a[0]<<"n"; cout<<"B is = "<<a[1]<<"n"; getch(); } Enter the Number A = 20 Enter the Number B = 10 After the swapping A is = 10 B is = 20 B is = 20 Enter the Number A = 20 Enter the Number B = 10 After the swapping A is = 10 B is = 20 B is = 20