Program in C and C++ to move smallest element in the start position of an array
C PROGRAM
#include<stdio.h> #include<conio.h> #define size 35 void move_sam_start(int A[],int n) { int i,loc=0,min; min=A[0]; for(i=1;i<n;i++) { if(A[i]<min) { min=A[i]; loc=i; } } for(i=loc;i>=0;i--) A[i]=A[i-1]; A[0]=min; } 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_sam_start(a,n); printf("\nElements of array after move smallest elements in the start position 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_sam_start(int A[],int n) { int i,loc=0,min; min=A[0]; for(i=1;i<n;i++) { if(A[i]<min) { min=A[i]; loc=i; } } for(i=loc;i>=0;i--) A[i]=A[i-1]; A[0]=min; } 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_sam_start(a,n); cout<<"\nElements of array after move smallest elements in the start position of an array\n"; for(i=0;i<n;i++) cout<<" "<<a[i]; getch(); }
Output :-
Related programs
0 Comments