I need to write a function named " void reverse(double* a, int size) " to reverse the number in an array. But instead of using indexes to transcribe from one array to the function's array so it can be reversed, I have to use pointers. I tried to comment almost every line of my code to figure out the logic but I'm afraid I just don't understand pointers very well. Not to mention that my instruction use a double and an int and when I mix those together I usually get all screwed up. Help is really appreciated.
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
|
#include <iostream>
using namespace std;
void reverse (double* a, int size){ // sets up function before main
int reverse_values [size] = *a; // sets up array using the parameters
// size for size and a for inputed values.
for ( *a = 0; *a < size; *a--){ // a decrements to reverse the order.
cout << " " << size [*a]; //displays the reversed order
}
}
int main () {
double size = 1000; // sets max input.
double values[size]; // initializes array values of max input
int current_size = 0; // initializes value that lets me adjust the size of the
// array later.
cout << "Please enter values, Q to quit:" << endl;
double input;
while (cin >> input) //end program when user enters a non integer
{ if (current_size < size)
{ values[current_size] = input; //adjusts size of array to the amount of input
current_size++; }
for (double i = 0; i < current_size; i++){ // creates a loop that cycles
// through the array
double* b = &values[i]; // initializes a pointer and sets it to the address
// of each value?
i = *b ; // sets each input value to a pointer?
reverse (*b, current_size); // puts the pointer and the size of an array
// as the parameters for the function reverse ^
}
}
return 0;
}
|
Here are my errors.
main.cpp:10: error: variable-sized object `reverse_values' may not be initialized
Do I not need two arrays? Why would I not need to initialize a second array to store values from the first array to manipulate it into being backwards?
main.cpp:13: error: invalid types `int[double]' for array subscript
This is where I get all confused from using two different types and how to keep them separate but working together.
main.cpp:19: error: size of array `values' has non-integral type `double'
Same.
main.cpp:31: error: invalid types `double[1][double]' for array subscript
Not sure how array subscripts work. You cant use type double?
main.cpp:33: error: cannot convert `double' to `double*' for argument `1' to `void reverse(double*, int)'
Where am I not keeping type double* consistent?
Thanks again. :)