Writing a function with pointers and arrays

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. :)
1) You cannot create array like int reverse_values [size] = *a; In fact you don't even need it.
2) a: Array size should be const; b: it should be integral type. What do you think int x[0.5] means?
3) You are getting address of pointe to the array beginning and dereferencing it.
4) You are passing dereferenced pointer (value) instead of raw pointer, as declared.
You don't need an array to reverse a series of values? So just:
1
2
3
void reverse (double* a, int size){ 
    for ( *a = 0; *a < size; *a--){  
         cout << " " << size [*a]

?

And x [0.5] denotes an array element. I suppose you don't have a half element so an array has to be set up with type int but can contain elements with double integers. Is that what you're getting at?

1
2
3
int size = 1000;  
const int values[size]; 
 int current_size = 0



And, So no ampersand?
1
2
double* b = values[i];
reverse (*b, current_size);

Last edited on
your everse function would be better written as:
1
2
3
4
5
6
7
8
9
void reverse(double* arr, size_t size)
{
/*loop arr starts from beginning, end fom end of array and bot moving towads center*/
    for(double* end = arr + size - 1; arr < end; ++arr, --end) {                                              
        double temp = *arr; //Swap with third variable
        *arr = *end;
        *end = temp;
    }
}


Array size should be const
So: const int size = 100;

and reverse it like:
reverse(values, current_size);//You are passing pointer to beginning of array and size to function.
Last edited on
If you are allowed algorithms you could just do this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>

int main()
{
    int test[5] = { 0 , 2 , 4 , 3, 1 };
    std::cout << "Normal ->" << std::flush;
    for( const auto &it : test )
    {
        std::cout << " " <<  it << std::flush;
    }
    std::reverse( test , test + 5 );
    std::cout << std::endl << "Reverse ->" << std::flush;
    for( const auto &it : test )
    {
        std::cout << " " << it << std::flush;
    }
    return( 0 );
}
Normal -> 0 2 4 3 1
Reverse -> 1 3 4 2 0
Process returned 0 (0x0)   execution time : 0.034 s
Press any key to continue.


*forgot space in output code
Last edited on
We don't learn algorithms until next semester "/
I need to keep the void function named as void reverse(double* a, int size)
Here's my updated code and the errors I'm getting:
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
#include <iostream>
using namespace std;
void reverse (double* a, int size){ 
    for (double* end = a+size-1; a < end; ++a, --end){
        double temp = *a;
        *a = *end;
        *end = temp;   
    }
       cout << size[] << endl;
}

int main () {
 const int size = 1000;  
 int values[size];
 int current_size = 0; 
 
 cout << "Please enter values, Q to quit:" << endl;
 double input;
 while (cin >> input) 
 { if (current_size < size) 
 { values[current_size] = input; 
 current_size++; }              
 for (double i = 0; i < current_size; i++){ 
 double* b = values[i]; 
 reverse (values, current_size); 
 }
 }
 return 0;
 }

Errors:

main.cpp:29: error: invalid types `int[1000][double]' for array subscript
main.cpp:33: error: invalid types `int[1000][double]' for array subscript
main.cpp:34: error: cannot convert `int*' to `double*' for argument `1' to `void reverse(double*, int)'
1) I you want your array be double, not int.
2) you might want to change your input part a little:
1
2
while ((std::cin >> input) && (current_size < size))
    values[current_size++] = input;

3) Loop on lines 23-27 is meaningless. Replace it with single reverse function call.
4) You should output your array after reverse call.
My program's running now but my output isn't right.
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>
using namespace std;
void reverse (double* a, int size){ 
    for (double* end = a+size-1; a < end; ++a, --end){
        double temp = *a;
        *a = *end;
        *end = temp;   
    }
}

int main () {
 const int size = 1000; 
 double values[size]; 
 int current_size = 0; 
 
 cout << "Please enter values, Q to quit:" << endl;
 double input;
 while ((cin >> input) && (current_size < size))
 {  
 { values[current_size] = input; 
 current_size++; }              
 reverse (values, current_size); 
 cout << "Reversed input is: " << values [current_size];
 } 
 return 0;
 }


Please enter values, Q to quit:
5
Reversed input is: 0
RUN FAILED (exit value 1, total time: 34s)
I just moved the cout outside of the brackets, run is successful and it lets me put in however much input I want, but the output is always 0. What's wrong? "/
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
#include <iostream>
using namespace std;
void reverse (double* a, int size){ 
    for (double* end = a+size-1; a < end; ++a, --end){
        double temp = *a;
        *a = *end;
        *end = temp;   
    }
}

int main () {
  const int size = 1000; 
  double values[size]; 
  int current_size = 0; 
 
  cout << "Please enter values, Q to quit:" << endl;

  /*INPUT*/
  double input;
  while ((cin >> input) && (current_size < size))
    values[current_size++] = input; //Braces were wrong and caused problems

  /*REVERSE*/
  reverse (values, current_size); 

  /*OUTPUT*/
  std::cout << "Reversed input is: "
  for(int i = 0; i < current_size; ++i)
    std::cout << values [i] << ' ';
  return 0;
}
Last edited on
This line

cout << "Reversed input is: " << values [current_size];

always prints out just the null teminator of your string.

If you want to display the string after every input, as it appears you want to, then you'll need to use a for-loop.

Andy
Awesome!! Thanks so much for showing me how to do that. :)
Here's my 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
#include <iostream>
using namespace std;
void reverse (double* a, int size){ //sets parameters using a pointer
    for (double* end = a+size-1; a < end; ++a, --end){
        double temp = *a;
        *a = *end;
        *end = temp;   
    }
}

int main () {
  const int size = 1000; 
  double values[size]; 
  int current_size = 0; 
 
  cout << "Please enter values, Q to quit:" << endl;

  //INPUT
  double input;
  while ((cin >> input) && (current_size < size))
    values[current_size++] = input; 

  //REVERSE
  reverse (values, current_size); 

  //OUTPUT
  cout << "Reversed input is: ";
  for(int i = 0; i < current_size; ++i)
  cout << values [i] << ' ';
  return 0;
}


Please enter values, Q to quit:
78
46
2.5
q
Reversed input is: 2.5 46 78 
RUN SUCCESSFUL (total time: 5s)
Topic archived. No new replies allowed.