-7

I am attempting to take a value N, and create a list/vector with all of the values from N to 0 (not inclusive), and then N again.

After defining N, I have the following code:

for (int i = N; i > 0; i--) {
    cout << i << endl;
}

This neatly prints out the required values. For example, with N=3:

3
2
1
3

The issue is that I can't manipulate these to compute what I want to (namely the mean, range, maximum and minimum values). So what I need to do is to get each iteration to put itself into an array, the same array that the previous iteration put itself in.

I have found a similar question on here (How to store the result of each iteration of a for loop into an array (Javascript)), except it's in javascript, which is causing some issues for me.

Why does the following not work on C++, and how can I change it?

var array = []
for (int i = N-1; i < N; i--) {
    array.push(i);
}

The same thing with int replacint var does not work either.

Any suggestions?

Community
  • 1
  • 1
quanty
  • 824
  • 1
  • 12
  • 21
  • Possible duplicate of [how do i add elements to an empty vector in a loop?](http://stackoverflow.com/questions/17984268/how-do-i-add-elements-to-an-empty-vector-in-a-loop) –  Jan 30 '17 at 17:22
  • 3
    First of all, the `for (int i = N-1; i < N; i--) { cout << i << endl; }` won't result in printing `3 2 1 3`. I suggest first to run your code, then ask what's wrong with it – Fureeish Jan 30 '17 at 17:23
  • 1
    Your first `for` loop keeps running semi-infinitely (i.e. it definitively runs until it reaches `INT_MIN`, and after that - it's undefined behavior). – Algirdas Preidžius Jan 30 '17 at 17:25
  • 1
    "Why does the following not work on C++, and how can I change it?" You can't just make up syntax. In C++ `for (int i = N-1; i < N; i--)` would invoke [undefined behaviour](http://en.cppreference.com/w/cpp/language/ub). C++ is not Js and vice versa, and they have very different common paradigms and idioms, you'll have to learn them separately. – George Jan 30 '17 at 17:26
  • Code edited to what it should be, wasn't concentrating when I typed up the Q. – quanty Jan 30 '17 at 17:34
  • `int N = 3; vector array; for (int i = N; i > 0; i--) array.push_back(i); array.reverse( array.begin(), array.end() );` – Geoff Jan 30 '17 at 17:35

1 Answers1

0

First of all, this: var array = [], and the rest of your code, is not c++ syntax at all, it's pure javascript. You can't copy code from one language to another expecting that would compile and run properly.

In order to insert all elemnts from N to 0 and then N again to a vector you need to :

  1. include the vector library : #include <vector>
  2. initiate an N value (from a user, using cin in by including iostream for example OR manually, i.e. N = 6)
  3. Read through the following code to see how to enter all numbers wanted numbers to a vector here (use push_back). Note: Also, as @NathanOliver mentioned, you can use reverse if you know N in advanced. look here for simple reference.
  4. And then, push also N in the end manually (vec.push_back(N))

And I'm really saying that for you, try to work this out yourself before someone here gives you the complete code.

Community
  • 1
  • 1
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25