Help me with vectors please

Guys, it's me again, the newbie, I just started working with vectors and kind of have no idea how to deal with them :@
I need to create a vector which has n members and the member is the negative number of it's index. For example the 1st step at the console should be to type a value for n=
then for example if n=5, the vector will have five members, A{5}= {-1, -2, -3, -4, ,5}
so the members will always start from 1 and go up to n but since we want them to be negative, we want them to start from -1
or like it is written in my book ai= -i
(where "i" is the index...)
please help me :(
Last edited on
Here is one to do it:
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void main(void)
{
  int num_elems = 0;

  cout << "Number of elements: ";
  cin >> num_elems;

  vector<int> numbers;

  for (int i = 1; i <= num_elems; i++)
  {
    numbers.push_back(-i);
  } 

  cout << "Numbers in vector: ";
  for (int i = 0; i < num_elems; i++)
  {
    cout << numbers[i] << '\t';
  }
  system("pause");
}
thanks again ;)
Anyway what's with the #include <fstream> ? I removed it and nothing happened, is it unnecessary there? The compilation went normal
the same with <strings> and <vectors>
although when I removed #include <vectors> there are some changes at the compilation, it tells me that the vector is undeclared identifier, but when i execute it, it gives me the same result with the numbers...
i started learning c++ because of school, but now im in love with it :v , and i know that in the future i'm going to need it (im going to study automatics at university) , so i have this need to question everything
please give me some explanations why did you use fstream, string and vector?
You dont need #include <fstream> no. He probably had it from something before. And use int main not void main
already changed it to int main ;)
thanks
Topic archived. No new replies allowed.