Jun 28, 2012 at 8:16am UTC
what am i doing wrong here??
i am supposed write a program to write a random number in the range (0,100) in an array.
getting error C2440: '=' cannot convert from 'void' to 'int *
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
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void RandomArrayFill(int *array,int size)
{
for (int i=0;i<size;i++)
{
array[i]=rand()%101;
}
}
int main()
{
srand(unsigned (time(0)));
int * array=0;
int size=0;
cout<<"enter size of array" ;
cin>>size;
array=RandomArrayFill(array,size);
for (int i=0;i<size;i++)
{
cout<<array[i]<<" " ;
}
}
Last edited on Jun 28, 2012 at 10:43am UTC
Jun 28, 2012 at 8:20am UTC
For code tags you need to highlight your code, *then* hit the code tag button.
As for your problem:
array=RandomArrayFill(array,size);
You are trying to assign the result of RandomArrayFill to array but RandomArrayFill doesn't return anything.
Jun 28, 2012 at 8:25am UTC
As well as ^ you have another problem with your array.
You create an array of size 0 here.
int * array=0;
Then you try fill it up with values later on in your function without actually creating the spaces in the array to put the values into. Add this before you call your function to make sure it has the spaces needed.
array = new int [size];
(Look up dynamically allocating arrays if that didn't make sense)
Last edited on Jun 28, 2012 at 8:25am UTC
Jun 28, 2012 at 8:32am UTC
That actually doesn't create an array at all, it just initializes the pointer to NULL. It's still an issue, though.
Jun 28, 2012 at 10:45am UTC
thank you all for your replies.... but could you be a little more specific. i dont understood what you are trying to say
Jun 28, 2012 at 11:13am UTC
could you be a little more specific
As James/Zhuge said, you're not actually creating an array. You need to add the line James' put there right after you get the size.
1 2
cin >> size;
array = new int [size];
On top of that, you want probably want to add some validation when your user is entering a number. What if they entered -72?
Secondly, as firedraco said, you're trying to assign a return value of a non returning function.
1 2 3
array=RandomArrayFill(array,size);
// Should be..
RandomArrayFill(array,size);
Finally, you're dynamically allocating memory. So don't forget to delete it.
Last edited on Jun 28, 2012 at 11:27am UTC
Jun 28, 2012 at 11:21am UTC
thank you all. It worked like charm
here s the final code
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
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void randomarrayfill(int *array,int size)
{
for (int i=0;i<size;i++)
{
array[i]=rand()%101;
}
}
int main()
{
srand(unsigned (time(0)));
int * array=0;
int size=0;
cout<<"enter size of array" ;
cin>>size;
if (size<=0)
{
cout <<"array size not valid" <<endl;
}
else
{
array = new int [size];
randomarrayfill(array,size);
for (int i=0;i<size;i++)
{
cout<<array[i]<<" " ;
}
delete [] array;
}
}
Last edited on Jun 28, 2012 at 11:24am UTC