Reversing an array

I was given this problem for my class:
Write a program in C++ that asks the user to enter an array of size N and pass the array to a function named reverse_array that takes as its arguments an array of floating point values and an integer that tells how many floating point values are in the array. The function must reverse the order of the values in the array. The function should not return any value. Do not forget to write the main function as well. Display the original array and the reversed array in the main function.

The following program is what i have typed up, but I have 2 issues. This compiling issue in line 4:
4:23: error: expected unqualified-id before 'int'
4:23: error: expected ')' before 'int'

Also, I tried to write the function with 20 numbers, but I don't know how to make it be able to take unlimited numbers in the array. Any help is appreciated, Thank you!

#include<iostream>
using namespace std;

struct reverse_array(float array,int arraylength){
for (int i = 0; i < (arraylength / 2); i++)
{
temporary = array[i];
array[i] = array[(arraylength - 1) - i];
array[(arraylength - 1) - i] = temporary;
}
cout << "The new array order is " << endl;

for (int i = 0; i < arraylength; i++)
{
cout << "Array[" << i << "] = " << array[i] << endl;
}

}
int main()
{
int temporary, arraylength;
float array[50];
cout << "Enter the array size: ";
cin >> arraylength;

cout << "Enter the numbers in the array:" << endl;
for (int i = 0; i < arraylength; i++)
{
cout << "Array[" << i << "] = ";
cin >> array[i];
}
cout<< reverse_array(array,arraylength);

}
Please use code tags.
http://www.cplusplus.com/articles/jEywvCM9/

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
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

// struct isn't a return type
/*struct*/ void reverse_array( float array[], int arraylength )
{
    for (int i = 0; i < (arraylength / 2); i++) {
        float temporary = array[i];                 // temporary wasn't declared
        array[i] = array[(arraylength - 1) - i];
        array[(arraylength - 1) - i] = temporary;
    }
}

int main()
{
    int /*temporary,*/ arraylength;     // unused variable temporary?
    //float array[50];
    /*
    You need to dynamically allocate memory for the array,
    as its size is not know at compile time.
    */
    cout << "Enter the array size: ";
    cin >> arraylength;
    
    float* array = new float[arraylength];

    cout << "Enter the numbers in the array:" << endl;
    for ( int i = 0; i < arraylength; i++ ) {
        cout << "Array[" << i << "] = ";
        cin >> array[i];
    }
    
    reverse_array( array, arraylength );
    
    cout << "The new array order is " << endl;
    for ( int i = 0; i < arraylength; i++ ) { 
        cout << "Array[" << i << "] = " << array[i] << endl;
    }
    
    // free the memory, to prevent memory leaks
    delete[] array;
}


Enter the array size: 5
Enter the numbers in the array:
Array[0] = 1
Array[1] = 2
Array[2] = 3
Array[3] = 4
Array[4] = 5
The new array order is 
Array[0] = 5
Array[1] = 4
Array[2] = 3
Array[3] = 2
Array[4] = 1

http://cpp.sh/3nl56
Last edited on
Well sir, you are awesome. I feel like an idiot not trying that as a void. I have been confused on which to use, and what time. My teacher has gone over functions where he will use void, struct, string, float, etc. and I have no clue when to you which. Do you by any chance know the differences?
My teacher has gone over functions where he will use void, struct, string, float, etc.


A struct is just a user-defined type that you'll create yourself, basically. It's just a class with public members by default. You know how you make something and use it, like:

1
2
int i = 0;
cout << i << endl;


int is a TYPE, and struct allows you to use your own user TYPE.

Float is just floating point numbers. You're probably more familiar with "double." It too is just a type.

String is also a type, and it's used for strings (hence the name). You use that for words/text:

1
2
3
string s1 = "Hello";
string s2 = "world!";
cout << s1 + ' ' + s2 << endl;


void is a "type" in the sense that it doesn't return anything. It's quite useful, especially for functions where you want to do something to something else, but (obviously) have no need or desire for a return type. You could use a void function, using the variables s1 and s2 above, in a manner such as:

1
2
3
4
5
6
7
8
9
10
void print(string x, string y)
{
cout << x + ' ' + y << endl
}

int main(){
//define s1 and s2 -- see above
print(s1, s2);
return 0;
}


So hopefully now you see that I could create any number of strings and concatenate them (put them together two at a time -- "stitch" them together, if you will) by simply passing two strings to the function print(), rather than typing cout instructions every time I wanted to do it. No return value is needed for this, so we use void.

Hope that helped.
Y'all were both helpful! Thank yo very much!
Topic archived. No new replies allowed.