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

More Related Content

What's hot (20)

PPTX
Flex box
Harish Karthick
 
PPTX
Break and continue in C
vishnupriyapm4
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Java Foundations: Data Types and Type Conversion
Svetlin Nakov
 
PPTX
Control flow statements in java
yugandhar vadlamudi
 
PDF
Basic JavaScript Tutorial
DHTMLExtreme
 
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Array and string
prashant chelani
 
PPTX
Javascript functions
Alaref Abushaala
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPTX
Control statements in java
Madishetty Prathibha
 
PDF
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
PPTX
Control Statement programming
University of Potsdam
 
PPT
Array in c
Ravi Gelani
 
PPTX
C# String
Raghuveer Guthikonda
 
PPTX
Static keyword ppt
Vinod Kumar
 
PDF
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
Flex box
Harish Karthick
 
Break and continue in C
vishnupriyapm4
 
Pointers C programming
Appili Vamsi Krishna
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java Foundations: Data Types and Type Conversion
Svetlin Nakov
 
Control flow statements in java
yugandhar vadlamudi
 
Basic JavaScript Tutorial
DHTMLExtreme
 
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
String and string buffer
kamal kotecha
 
Array and string
prashant chelani
 
Javascript functions
Alaref Abushaala
 
Type casting in java
Farooq Baloch
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Control statements in java
Madishetty Prathibha
 
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
Control Statement programming
University of Potsdam
 
Array in c
Ravi Gelani
 
Static keyword ppt
Vinod Kumar
 
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 

Similar to Array assignment (20)

PPTX
Arrays
Neeru Mittal
 
PPTX
Arrays in C++
Kashif Nawab
 
PPT
Fp201 unit4
rohassanie
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Arrays_in_c++.pptx
MrMaster11
 
PPTX
Array 2 hina
heena94
 
PPT
Chapter 10.ppt
MithuBose3
 
PDF
arraysfor engineering students sdf ppt on arrays
ruvirgandhi123
 
PPT
2DArrays.ppt
Nooryaseen9
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
amanpathak160605
 
PPT
C++ Arrays
أحمد محمد
 
PPT
C++ Arrays
أحمد محمد
 
PPTX
Structured data type
Omkar Majukar
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
PDF
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
info309708
 
PPT
Arrays
SARITHA REDDY
 
PPTX
Array 1D.................................pptx
Hassan Kamal
 
Arrays
Neeru Mittal
 
Arrays in C++
Kashif Nawab
 
Fp201 unit4
rohassanie
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
Data structure array
MajidHamidAli
 
Arrays_in_c++.pptx
MrMaster11
 
Array 2 hina
heena94
 
Chapter 10.ppt
MithuBose3
 
arraysfor engineering students sdf ppt on arrays
ruvirgandhi123
 
2DArrays.ppt
Nooryaseen9
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
amanpathak160605
 
C++ Arrays
أحمد محمد
 
C++ Arrays
أحمد محمد
 
Structured data type
Omkar Majukar
 
ARRAYS.pptx
MamataAnilgod
 
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
info309708
 
Array 1D.................................pptx
Hassan Kamal
 
Ad

Recently uploaded (19)

PPTX
AI presentation for everyone in every fields
dodinhkhai1
 
PPTX
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
PPTX
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
PDF
The Family Secret (essence of loveliness)
Favour Biodun
 
PDF
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
PPTX
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
PDF
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
PDF
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
PPTX
some leadership theories MBA management.pptx
rkseo19
 
DOCX
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
PPTX
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
PDF
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PPTX
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
PDF
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
PPTX
presentation on legal and regulatory action
raoharsh4122001
 
PDF
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
PPTX
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
PDF
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
AI presentation for everyone in every fields
dodinhkhai1
 
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
The Family Secret (essence of loveliness)
Favour Biodun
 
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
some leadership theories MBA management.pptx
rkseo19
 
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
presentation on legal and regulatory action
raoharsh4122001
 
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
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