SlideShare a Scribd company logo
1 of 23
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 01: Programming Fundamentals
Lecture 10
Array
1
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
Program to compute distance of 5 points
2
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
3
#include <iostream.h>
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "Distance 1: " << distance[0] << endl;
cout << "Distance 2: " << distance[1] << endl;
cout << "Distance 3: " << distance[2] << endl;
cout << "Distance 4: " << distance[3] << endl;
cout << "Distance 5: " << distance[4] << endl;
Getch();
}
This would produce:
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
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
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();
}
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
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program to find the Maximum number in array
8
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
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
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
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program to search a particular number in
array using Binary search
13
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
GapData Institute
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
Allan Sun
 

What's hot (20)

4. functions
4. functions4. functions
4. functions
 
Abebe1
Abebe1Abebe1
Abebe1
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - Harsh
 
Java interface and inheritance
Java interface and inheritanceJava interface and inheritance
Java interface and inheritance
 
Eta
EtaEta
Eta
 
Maximal slice problem
Maximal slice problemMaximal slice problem
Maximal slice problem
 
Boredom comes to_those_who_wait
Boredom comes to_those_who_waitBoredom comes to_those_who_wait
Boredom comes to_those_who_wait
 
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
FSOFT - Test Java Exam
FSOFT - Test Java ExamFSOFT - Test Java Exam
FSOFT - Test Java Exam
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthrough
 
C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
Sbaw090519
Sbaw090519Sbaw090519
Sbaw090519
 
Time Series Analysis Sample Code
Time Series Analysis Sample CodeTime Series Analysis Sample Code
Time Series Analysis Sample Code
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3
 
Docopt
DocoptDocopt
Docopt
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
 

Similar to Array programs

Programa.eje
Programa.ejePrograma.eje
Programa.eje
guapi387
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
Anderson Dantas
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
LokeshK66
 

Similar to Array programs (20)

Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Ch7
Ch7Ch7
Ch7
 
Ch7
Ch7Ch7
Ch7
 
C++ file
C++ fileC++ file
C++ file
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ report
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 

More from ALI RAZA

Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++
ALI RAZA
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
ALI RAZA
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
ALI RAZA
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shams
ALI RAZA
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-complete
ALI RAZA
 

More from ALI RAZA (20)

Structure
StructureStructure
Structure
 
Recursion
RecursionRecursion
Recursion
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
Algorithm Development
Algorithm DevelopmentAlgorithm Development
Algorithm Development
 
Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Array sorting
Array sortingArray sorting
Array sorting
 
2D-Array
2D-Array 2D-Array
2D-Array
 
Quiz game documentary
Quiz game documentaryQuiz game documentary
Quiz game documentary
 
Function pass by value,function pass by reference
Function pass by value,function pass by reference Function pass by value,function pass by reference
Function pass by value,function pass by reference
 
Drug Addiction 39 Slides
Drug Addiction 39 SlidesDrug Addiction 39 Slides
Drug Addiction 39 Slides
 
Drug Addiction Original 51 Slides
Drug Addiction Original 51 SlidesDrug Addiction Original 51 Slides
Drug Addiction Original 51 Slides
 
Passing stuctures to function
Passing stuctures to functionPassing stuctures to function
Passing stuctures to function
 
Basic general knowledge
Basic general knowledgeBasic general knowledge
Basic general knowledge
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shams
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-complete
 
Husne akhlaq
Husne akhlaqHusne akhlaq
Husne akhlaq
 
Parts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examplesParts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examples
 
Quik tips
Quik tipsQuik tips
Quik tips
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
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
  • 3. University Institute of Information Technology, PMAS-AAUR Lecture 10: Programming Fundamentals 3 #include <iostream.h> int main() { double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28}; cout << "Distance 1: " << distance[0] << endl; cout << "Distance 2: " << distance[1] << endl; cout << "Distance 3: " << distance[2] << endl; cout << "Distance 4: " << distance[3] << endl; cout << "Distance 5: " << distance[4] << endl; Getch(); } This would produce: Distance 1: 44.14 Distance 2: 720.52 Distance 3: 96.08 Distance 4: 468.78 Distance 5: 6.28
  • 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