hello, trying to change array of ints to dynamically allocated array of floats

Hello,
trying to change array of ints of the given size into dynamically-allocated array of float of the same size with same value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;
float* int_to_float(int* data, int size) {
	float* ydata = new float[*size];
	for (int x = 0; x < size; x++) {
		ydata[x] = data[x];
	}
	return ydata;

}
int main() {
	int data, size;
	cout << "Enter Data: ";
	cin >> data;
	cout << "Enter Size: ";
	cin >> size;

	
	cout << int_to_float(&data, size); /* here when I do (data, size) it makes error so I put & before data but is  this why it prints address for size? */
delete ydata;
	return 0;
}


thanks.
Last edited on
float* ydata = new float[*data];

Shouldn't that newly created array be of length size?
oh ill change thx.
Given that data is a single int - just one int - what's intended by data[x]? data isn't an array
than without array [] just data?
ydata[x] = data;
Is that what you're trying to do? Make an array of the same numbers? So if the user enters 5 for data and 3 for size, you end up with an array: {5 5 5} ?
yea but change to float.
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
#include <iostream>
using namespace std;

float* int_to_float(int data, int size) 
{
	float* ydata = new float[size];
	for (int x = 0; x < size; x++) 
       {
		ydata[x] = data;
	}
	return ydata;
}

int main() {
	int data, size;
	cout << "Enter Data: ";
	cin >> data;
	cout << "Enter Size: ";
	cin >> size;

	float* returned_array = int_to_float(data, size);

        for (int x = 0; x < size; x++) 
        {
	    cout << returned_array[x] << " ";
	}
	return 0;
}
wow thanks
float* returned_array = int_to_float(data, size);
// this returned means data that has been returned from function return ydata; this right?

oh just float type name for it. :p

Last edited on
What is a new without delete?
Oh yea forgot to put
delete ydata;
at the end!
ill edit it.
Topic archived. No new replies allowed.