Need help with reversing array

I need to take the array that was generated and put into a file and reverse it. How can i do that? This is what i have so far. How can i make a function to do that?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void populate();
void display();
void writeFile(ofstream& ,int[],int);

int main()
{
int choice;
cout << "Please Enter (1) if you wish to create a new set of randomly generated numbers \n";
 cout	<<  "Enter (2) if you wish to display the numbers that were generated \n"; 
 cout << "Enter (3) if you wish to see the randomly generated numbers in reverse order/n";
cin >> choice;
if(choice == 1)
{
      populate();
}
else
	  display(); 
    return 0;
}

void populate()

{int values[]={5, 10, 15, 20, 25, 30, 35, 95, 100};
int elements,seed,i;
int *array;
char filename[30];
ofstream out;
cout<<"Enter number of elements: ";
cin>>elements;
cout<<"Enter random number generator seed: ";
cin>>seed;
srand(seed);
array=new int[elements];
for(i=0;i<elements;i++)
    array[i]=values[rand()%7];
cout<<"Enter file name: ";
cin>>filename;
out.open(filename);
writeFile(out,array,elements);
cout<<"Operation successfully completed\n ";
 }

void display()
{
	char filename[50];
	ifstream my;

	cin.getline(filename, 50);
	cin >> filename;
	my.open(filename);
	if(!my.is_open()){
		exit(EXIT_FAILURE);
	}
		while(my.good())
		{
			int num;
			my >> num;
			cout << num << " ";
		}
		
}

void writeFile(ofstream& out ,int a[],int m)
{int i;
for(i=0;i<m;i++)
    out<<a[i]<<endl;
}
Use this to generate each number to store in the array/vector:
http://www.cplusplus.com/reference/cstdlib/srand/

Writing to the file is simple enough:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
    int poot[5]{0, 1, 2, 3, 4};
	ofstream iambored("hello.txt");
	for(int i : poot)
    {
        iambored<<poot[i];
    }
    iambored.close();

	return 0;
}

Reading from a file is just as easy.

Reversing the array of ints. This depends on whether you want to reverse the actual number or the characters of the int. I will assume you want to reverse the characters of the int.

After you convert the int to a string:
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c

reverse it:
http://www.cplusplus.com/forum/beginner/11633/

then convert it back:
http://www.cplusplus.com/forum/general/13135/
Last edited on
Also keep in mind you should probably switch to vectors and std::arrays. Also:
int poot[5]{0, 1, 2, 3, 4};

and

for(int i : poot)

are C++11 features which your compiler might not support
You should keep the number of elements and then read the file in dynamically allocated array and reverse it.
You can also even use this really handy library called algorithm if your teacher allows it. Then try this.
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>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

int unique_number( void )
{
    return( rand() % 100 );
}

template <typename T>
void output( const std::vector<T> &vec )
{
    for( const auto &it : vec )
    {
        std::cout << it << " " << std::flush;
    }
    std::cout << std::endl;
}

int main()
{
    srand( (unsigned) time( NULL ) );
    const auto size( 9 );
    std::vector<int> vec( size );
    std::generate_n( vec.begin() , size , unique_number );

    output( vec );
    std::reverse( vec.begin() , vec.end() );

    output( vec );
}
33 37 49 85 28 85 84 27 12
12 27 84 85 28 85 49 37 33

Process returned 0 (0x0)   execution time : 0.244 s
Press any key to continue.
Topic archived. No new replies allowed.