Beyond the EU: DORA and NIS 2 Directive's Global Impact
Array programs
1. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 01: Programming Fundamentals
Lecture 10
Array
1
2. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
Program to compute distance of 5 points
2
4. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example 2
Array program that input 5 integers and store
them in array. It then display all the values in
array.
4
5. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
5
#include <iostream.h>
#include <conio.h>
main()
{
int array[5];
cout<< "Enter five integers" << endl;
cin>>array[0];
cin>>array[1];
cin>>array[2];
cin>>array[3];
cin>>array[4];
cout<< "the values in array are" << endl;
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<array[2]<<endl;
getch();
}
6. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program that inputs five values from user,
store them in array and displays the sum and
average of these values.
6
7. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include <iostream.h>
#include <conio.h>
main()
{
int array[5], i, sum = 0;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
sum = sum + array[i];
}
float avg = sum/5.0;
cout<< "The sum is" << sum<<endl;
cout<< "The average is" << avg<<endl;
getch();
}
7
8. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program to find the Maximum number in array
8
9. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include <iostream.h>
#include <conio.h>
main()
{
int array[5], i, max;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
max = array[0];
for(i=0; i<5; i++)
{
if(max < array[i])
max = array[i];
}
cout<< "The maximum Number is " << max<<endl;
getch();
}
9
10. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that input 5 integers and
display them in actual and in reverse order.
10
11. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include <iostream.h>
#include <conio.h>
main()
{
int array[5], i;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
cout<<"The array in Reverse Ordern ";
for(i=4; i>=0; i--)
{
cout<<array[i]<<" ";
}
getch();
}
11
12. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include <iostream.h>
#include<conio.h>
main()
{
int array[5]={10,20,30,40,50};
int i, n, loc = -1;
cout<< "Enter value to find" << endl;
cin>>n;
for(i=0; i<5; i++)
{
if(array[i] == n)
{
loc = i;
}
}
if(loc==-1)
cout<<"Number is not found in array"<<endl;
else
cout<<"the numbe is found at position "<<loc<< " and is "<<n;
getch();
}
12
13. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program to search a particular number in
array using Binary search
13
14. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
int array[5]={10,20,30,40,50};
int i, n, mid, loc = -1;
int start = 0;
int end = 4;
cout<< "Enter value to find" << endl;
cin>>n;
while(start<=end)
{
mid = (start + end)/2;
if(array[mid] == n)
{
loc = mid;
break;
}
else if(n<array[mid])
end = mid - 1;
else
start = mid + 1;
}
if(loc==-1)
cout<<"Number is not found in array"<<endl;
else
cout<<"the numbe is found at position "<<loc<< " and is "<<n;
getch();
}
14
15. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Selection Sort
Program that take 5 random values from user
in an array and sort it in ascending order.
15
16. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=i+1; j<5; j++)
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
}
16
17. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program that take 5 random values from user
in an array and sort it in ascending order,
using bubble sort.
17
18. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
main()
{
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=0; j<4; j++)
if(array[j]>array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
} 18
19. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that initializes 2D array
having two rows and three columns and then
displays its values.
19
20. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include<iostream.h>
#include<conio.h>
main()
{
int i, j;
int array[2][4]={{10,21,9,84},{33,72,48,17}};
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<"Position "<<i<<" "<<j<<" = "<<array[i][j]<<endl;
}
getch();
}
20
21. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that initialize a 2D array of 4
rows and 2 columns and then displays the
maximum and minimum number in array.
21
22. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
main()
{
int i, j, max, min;
int array[2][4]={{10,21,9,84},{33,72,48,17}};
max=min=array[0][0];
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
if(array[i][j]>max)
max=array[i][j];
if(array[i][j]<min)
min=array[i][j];
}
cout<<"Maximum valu in array is "<<max<<endl;
cout<<"Minimum valu in array is "<<min;
getch();
}
22
23. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
Program to stores values in an array of 2 Rows and four Columns.
int i,j;
int array[2][4];
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<"Enter an Integer ";
cin>>array[i][j];
}
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<array[i][j]<<"t";
}
getch();
}
23