I am using this code for the function overloading using C++ to sort a double and an integer array.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void sort(int arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted integer array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
void sort(double arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted double array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int n;
cout<<"Enter the size of the array \n";
cin>>n;
cout<<"Enter the values for the integer function \n";
int a[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
double b[n];
cout<<"Enter the values for the double array \n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(a,n);
sort(b,n);
}
In the answer it rounds off the values of the double values except the first entry into the double array. Is there any type casting step which I am missing or any other flaw in the code?