- Declare a pointer, ‘dp’
- Use this pointer to create a dynamic array of values. You will need to use the ‘number’ to determine the size of the new array.
I thought I did those steps correctly |
You did:
1 2 3
|
double *dp; // <- dp is a pointer. CORRECT
dp = new double[number]; // <- using 'number' to create a dynamic array, and have dp point to it. CORRECT
|
but "dp" seems to be the problem. |
dp is fine. The problem is you have mismatching types.
Since my previous post did not clarify it... let me try to explain by backing up and going over some basics:
Every variable has to have a "type". This type indicates what kind of information the variable can contain.
For example:
-
int
can contain an integer
-
char
can contain a single character
-
double
can contain a floating-point numerical value
-
string
can contain a string of text
etc
etc
Pointers are exactly the same. They are also variables -- but instead of containing a character or an integer, they contain an
address. This address typically refers, or "points to" another variable.
For example:
-
int*
is a pointer (because it has the * on it). So it will contain an address. And whatever variable that is located at that address must be an
int
, so it would contain an integer.
C++ is a [mostly] strict typed language, so understanding different types is very important. You cannot treat a string as if it were an int, because they are two different types. Likewise, you cannot assign strings to ints, nor ints to strings. The types must match (or otherwise be compatible).
Now when you create a function, you can choose which parameters that function accepts. Coming back to my previous example:
1 2 3
|
void func(int foo)
{
}
|
This function takes a single parameter, and that parameter
must be an integer because we defined it to be an
int
.
So now when we call this function, we must give it an integer:
1 2 3 4 5 6 7 8
|
int main()
{
int x = 0;
func(x); // OK. x is an int, so this works -- the types match
string y = "lskjdfs";
func(y); // ERROR. y is a string, and func() can only accept an int! types don't match!
}
|
See now? When you call a function, you have to give it a type that matches what the function expects.
You are not doing this in your original code:
1 2 3 4 5
|
void showArray(const int[], int); // <- showArray takes an array of ints for the first param
//...
showArray(dp, number); // <- but 'dp' is an array of DOUBLES, not of ints.
|
These are incompatible types, and therefore the compiler is complaining at you.
What you need to do is make the types match. So either change showArray to take an array of doubles -- or change dp to be an array of ints.