Reading an array backwards & printing!

closed account (ENhkSL3A)
Hey guys! I'm having a user input a vector and blah blah. Any who, I need to know how to use the member function
vest.reverse();
in order to read the vector backwards. Here is my code so far :)
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
#include <iostream>

using namespace std;

int main()
{

	cout << "Kaitlin Stevers" << endl;
	cout << "Exercise 9A - Print Array" << endl;
	cout << "October 31, 2016" << endl;
	cout << endl;
	cout << endl;
	int numOfValues;
	cout << "Please enter the number of values you need stored. " << endl;
	cin >> numOfValues;
	int count;

	const int ARRAY_SIZE = 50;
	int val[ARRAY_SIZE]; 
	
	
	for (count = 0; count < numOfValues; count++)
	{
		cout << "Enter an integer value: ";
		cin >> val[count];
	}

		cout << "Here are the values you entered:\n";
		for (count = 0; count < numOfValues; count++)
			cout << " " << val[count];
			cout << endl;
		
		cout << "Here are the values backwords: /n";
		for (count =0; count < numOfValues; count++)
			vect.reverse(val);
		
	return 0;
}
Last edited on
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
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int i;
	cout << "Kaitlin Stevers (kns2872) on Cplusplus forum" << endl;
	cout << "Exercise 9A - Reading a vector backwards" << endl;
	cout << "October 31, 2016" << endl;
	cout << endl;
	cout << endl;

	int numOfValues;
	while(true)
	{
		cout << "Please enter the number of values you need stored : ";
		if(cin >> numOfValues && numOfValues >= 1 && numOfValues <= 200) break;

		if(!cin)
		{
			cin.clear();
			cin.ignore(1000, '\n');		
		}
		cout << "Invalid number or number out-of range. Please try again\n\n";
	}

	vector<double> val;
	val.resize(numOfValues);

	for(i = val.size() - 1; i >= 0; i--)
	{
		cout << "- Enter value " << i + 1 << " : "; 
		
		if(!(cin>> val[i]))
		{
			i++;
			cin.clear();
			cin.ignore(1000, '\n');
			cout << "Invalid value. Please try again.\n\n";
		}
	}

	cout << endl;
	cout << "The array you entered : ";

	for(i = 0; i < val.size(); i++)
	{
		cout << val[i];
		if(i < (val.size() - 1)) cout << ", ";
	}

	cout << endl;

	cin.ignore();
	cin.get();
	return 0;
}


Kaitlin Stevers (kns2872) on Cplusplus forum
Exercise 9A - Reading a vector backwards
October 31, 2016

Please enter the number of values you need stored : 12
- Enter value 12 : 33
- Enter value 11 : 76
- Enter value 10 : 24
- Enter value 9 : 85
- Enter value 8 : 42
- Enter value 7 : 55
- Enter value 6 : 22
- Enter value 5 : 15
- Enter value 4 : 64
- Enter value 3 : 34
- Enter value 2 : 100
- Enter value 1 : 22

The array you entered : 22, 100, 34, 64, 15, 22, 55, 42, 85, 24, 76, 33
Last edited on
closed account (ENhkSL3A)
SakurasouBusters can I ask you something?
Sure, no hesitation.
closed account (ENhkSL3A)
Did you read the part where I said, "I need to know how to use the member function
 
vest.reverse(); 

in order to read the vector backwards."
Last edited on
You are asking us to use std::vector of course. And read the vector backwards.
But your code shows that you are using a normal array.

Edit : Should you change the title to
"Reading an array backwards!"
?
Last edited on
closed account (ENhkSL3A)
I'm so confused.......... omg... Now I feel stupid. I kept thinking vector and saying it but it is array... Humm. Newbee question, what is the difference in the two?? & How do I read the array backwards and output it.
> what is the difference in the two??

See: https://cal-linux.com/tutorials/vectors.html


> How do I read the array backwards and output it.

To iterate through an array (or any standard sequence) backwards, where seq is the sequence:
1
2
// C++14
for( auto iter = std::rbegin(seq) ; iter != std::rend(seq) ; ++iter ) { /* do something with *iter */ }

http://en.cppreference.com/w/cpp/iterator/rbegin

For instance:
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
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <array>
#include <iterator>

int main()
{
    const int a[] { 0, 1, 2, 3, 4, 5 } ;
    for( auto iter = std::rbegin(a) ; iter != std::rend(a) ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << '\n' ;

    const std::vector<double> b { 0.1, 2.3, 4.5, 6.7, 8.9 } ;
    for( auto iter = std::rbegin(b) ; iter != std::rend(b) ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << '\n' ;

    const std::list<std::string> c { "zero", "one", "two", "three", "four", "five" } ;
    for( auto iter = std::rbegin(c) ; iter != std::rend(c) ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << '\n' ;

    const std::array<long,6> d { { 10, 11, 12, 13, 14, 15 } } ;
    for( auto iter = std::rbegin(d) ; iter != std::rend(d) ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/73f1bca671a0d74a
closed account (ENhkSL3A)
I'm sorry but the std::cout << is throwing me off. Is that the same as cout <<? Is that what you do when you do not use, using namespace std;

> Is that what you do when you do not use, using namespace std;

Yes.
More information: http://www.cplusplus.com/forum/beginner/142171/#msg750694

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> // std::cout
#include <iterator> // std::rbegin, std::rend

int main()
{
    using namespace std ; // ****

    const int a[] { 0, 1, 2, 3, 4, 5 } ;
    for( auto iter = rbegin(a) ; iter != rend(a) ; ++iter ) cout << *iter << ' ' ;
    cout << '\n' ;
}
closed account (ENhkSL3A)
I'm still unsure what to do in my case. Yours uses an actual set of numbers programmed in. In my case the user is entering the array in and I need to spit it out forwards and then backwards. Unfortunately, no one has this anywhere I've looked. :(
closed account (ENhkSL3A)
I need some advice soon. Please make sure it works and makes since with my code. Thanks guys.
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>

int main()
{
    const int ARRAY_SIZE = 50;
    int val[ARRAY_SIZE];

    int num_values;
    std::cout << "Please enter the number of values [0," << ARRAY_SIZE
              << "] you need stored.\n" ;
    std::cin >> num_values;

    if( num_values <= ARRAY_SIZE )
    {
        for( int i = 0; i < num_values; ++i )
        {
            std::cout << "Enter an integer value: ";
            std::cin >> val[i];
        }

        std::cout << "Here are the values you entered:\n";
        for( int i = 0; i < num_values; ++i ) std::cout << val[i] << ' ' ;
        std::cout << '\n' ;

        std::cout << "Here are the values backwards:\n";
        int pos_last = num_values - 1 ; // position of the last value that was entered
        for( int i = 0; i < num_values; ++i ) std::cout << val[ pos_last - i ] << ' ' ;
        std::cout << '\n' ;
    }
}
Last edited on
closed account (ENhkSL3A)
Thank you sooooooo much!!!! I've been trying to figure this out for hours... Obviously I failed lol It makes since now though! so, THANK YOU!
Topic archived. No new replies allowed.