Program in C and C++ to move largest element in the end of an array
C PROGRAM
#include<stdio.h> #include<conio.h> #define size 35 void move_lar_end(int A[],int n) { int i,loc=0,max; max=A[0]; for(i=1;i<n;i++) { if(A[i]>max) { max=A[i]; loc=i; } } for(i=loc;i<n;i++) A[i]=A[i+1]; A[n-1]=max; } int main() { int a[size],n,i; printf("How many number you want to enter:-"); scanf("%d",&n); printf("Enter %d numbers :-",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Elements of array\n"); for(i=0;i<n;i++) printf("%4d",a[i]); move_lar_end(a,n); printf("\nElements of array after move largest elements in end of an array\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); }
C++ PROGRAM
#include<iostream> #include<conio.h> using namespace std; const int size=35; void move_lar_end(int A[],int n) { int i,loc=0,max; max=A[0]; for(i=1;i<n;i++) { if(A[i]>max) { max=A[i]; loc=i; } } for(i=loc;i<n;i++) A[i]=A[i+1]; A[n-1]=max; } int main() { int a[size],n,i; cout<<"How many number you want to enter:-"; cin>>n; cout<<"Enter "<<n<<" numbers :-"; for(i=0;i<n;i++) cin>>a[i]; cout<<"Elements of array\n"; for(i=0;i<n;i++) cout<<" "<<a[i]; move_lar_end(a,n); cout<<"\nElements of array after move largest elements in end of an array\n"; for(i=0;i<n;i++) cout<<" "<<a[i]; getch(); }
Output:-
- Program in C and C++ to move smallest element in the start position of an array
0 Comments