0% found this document useful (0 votes)
9 views

Name: Arsalna Saif Roll No: 45 Department: Cs &it Class: Bsit (3nd Semester) Assignment Of: Data Structure Lab Assignment To: Ms Rabia

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Name: Arsalna Saif Roll No: 45 Department: Cs &it Class: Bsit (3nd Semester) Assignment Of: Data Structure Lab Assignment To: Ms Rabia

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name:

Arsalna saif
Roll no:
45
Department:
Cs &it
Class:
Bsit (3nd semester )
Assignment of :
Data structure lab
Assignment to :
Ms rabia
Write a program that insert an element at the
beginning and at the end of an array.

//insert an element at the beginning and at the end


#include <iostream>
Using namespace std;
Int main() {
Int a[6],i,n;
//insert at the beginning
Cout≪”enter a number of elements in an array :”≪endl;
Cin≫n;
Cout ≪”enter an elements of an array:”≪endl;
For (i=0;i<n;i++){
Cin≫a[i];
}
Cout≪”enterd elements of an array:”≪endl;
For (i=0;i<n;i++){
Cout≪”a[“≪i≪”]=”≪a[i]≪endl;
}
//insert at the end
Cout≪”enter a number of elements in an array :”≪endl;
Cin≫n;
Cout≪”enter an elements of an array:”≪endl;
For (i=n-1;i<n;i--){
If(i≥0){
Cin≫a[i];
}
}
Cout≪”entered elements of an array:”≪endl;
For (i=n-1;i<n;i--){
If(i≥0){
Cout≪”a[“≪i≪”]=”≪a[i]≪endl;
}
}
Return 0;
}
Out put:-
enter a number of elements in an array :
3
enter an elements of an array:
9
6
8
enterd elements of an array:
a[0]=9
a[1]=6
a[2]=8
enter a number of elements in an array :
5
enter an elements of an array:
8
9
5
3
0
entered elements of an array:
a[4]=8
a[3]=9
a[2]=5
a[1]=3
a[0]=0
Write a program to sort elements of array using
bubble sort.
// short elements of an array using bubble sort
#include <iostream>
using namespace std;
int bubbleSort(int a[],int n) {
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[j]<a[i]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;

}
}
}
}
int main() {
int n,a[6],i;
cout<<"enter a number of element in array:"<<endl;
cin>>n;
cout<<"enter an element of an array:"<<endl;
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"element of an array:"<<endl;
for (i=0;i<n;i++){
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
bubbleSort(a,n);
cout<<"sorted array:";
for(i=0;i<n;i++){
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
return 0;
}
 Output:-
enter a number of element in array:
5
enter an element of an array:
8
5
9
4
3
element of an array:
a[0]=8
a[1]=5
a[2]=9
a[3]=4
a[4]=3
sorted array:
a[0]=3
a[1]=4
a[2]=5
a[3]=8
a[4]=9

You might also like