Hi
I want to write a function which takes as input an array of INTEGERS and return another array which contains squares of the integers in input array.Following code works perfect.
#include <iostream>
using namespace std;
const int SIZE=5;
int* SquareArray(int c[],int n);
int main()
{
int b[SIZE];
cout<<"Enter a sequence of "<<SIZE<<" numbers"<<endl;
for(int i=0;i<SIZE;i++){
cin>>b[i];
}
int* a=SquareArray(b,SIZE);
for ( int i = 0; i < SIZE; i++ )
{
cout << a[i] << endl;
}
return 0;
}
int* SquareArray(int c[],int n)
{
int *array = new int[n];
for ( int i = 0; i < n; i++ )
{
array[i]=c[i]*c[i];
}
return(array);
}
but i want to do the same thing when the input array contains real variable of type DOUBLE.A simple modification as below is not working:
#include <iostream>
using namespace std;
const int SIZE=5;
double* SquareArray(double c[],int n);
int main()
{
double b[SIZE];
cout<<"Enter a sequence of "<<SIZE<<" real numbers"<<endl;
for(int i=0;i<SIZE;i++){
cin>>b[i];
}
double* a=SquareArray(b,SIZE);
for ( int i = 0; i < SIZE; i++ )
{
cout << *(a+i) << endl;
}
return 0;
}
double* SquareArray(double c[],int n)
{
double *array = new int[n];
for ( int i = 0; i < n; i++ )
{
array[i]=c[i]*c[i];
}
return(array);
}
Error:cannot convert ‘int*’ to ‘double*’ in return
where as defining
int* SquareArray(double c[],int n); gives only integer part of the square(which is understandable)
can any one explain what is happening and how to resolve this issue.