Sorting numbers while overloading the function.
Dec 9, 2014 at 9:18pm UTC
So I've been trying to practice function overloading and I've come across a problem in displaying numbers that are not integers. The program compiles but I get an error saying narrowing conversion from double to int. How do I overcome this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include <iostream>
#include<iomanip>
using namespace std;
void sorting(int vallist[])
{
int i,j;
int temp;
for (i=0; i<5; i++)
{
for (j=0; j<5; j++)
{
if (vallist[j]>vallist[j+1])
{
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
}
}
for (i=0; i<5; i++)
{
cout<<vallist[i]<<"\n" ;
}
}
void sorting(float vallist[])
{
int i,j;
float temp;
for (i=0; i<5; i++)
{
for (j=0; j<5; j++)
{
if (vallist[j]>vallist[j+1])
{
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
}
}
for (i=0; i<5; i++)
{
cout<<vallist[i]<<"\n" ;
}
}
void sorting(double vallist[])
{
int i,j;
double temp;
for (i=0; i<5; i++)
{
for (j=0; j<5; j++)
{
if (vallist[j]>vallist[j+1])
{
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
}
}
for (i=0; i<5; i++)
{
cout<<vallist[i]<<"\n" ;
}
}
int main()
{
int vallist[]= {5.65, 10.50 ,25.90 ,50.31 ,100.99};
sorting(vallist);
return 0;
}
Dec 9, 2014 at 10:25pm UTC
It sounds like it just warning you that it will convert to a type that might result in the loss of data.
Dec 9, 2014 at 10:27pm UTC
The program compiles but I get an error saying narrowing conversion from double to int
If you get an error that means it is not compiles.
Do not try to stuck doubles in integer array.
Change it to either
1 2
double vallist[]= {5.65, 10.50 ,25.90 ,50.31 ,100.99}; //Double array with double values
int vallist[]= {5, 10 ,25 ,50 ,100}; //int array with int values
Dec 9, 2014 at 10:33pm UTC
Yes, you're right I should've said that it was just a warning. Anyways, thanks.
Topic archived. No new replies allowed.