How to form a vector with only the negative numbers from another vector with mixed numbers

Hey guys, I just started a week ago using C++. And I need your help to do my homework, so please be gentle with me.
Anyway, I looked for answers in this forum but it seems like other people use different logic to solve their problems. There is the vector A which consist different members with different values. And I need to form another vector B, which consists only the negative numbers that are at A . I looked at the book that I'm learning at school about C++. And could only find how to take the negative numbers from a matrix and form a vector from them. Kind of followed that logic and came up with this similar code. but the problem is that the vector B should have -1 and -4 as it's members, but instead when I run the code, it gives me the -4 and -4. Test it for yourself and please help me. If you can modify this code where it gives me the -1 and -4, I'd be happy.
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int const m = 4;
	int A[m] = { 4, -1, -4, 2 };
	int k, i, B[m];
	char v[] = "---------------------";
	k = -1;
	i = 0;
	while (i < m)
	{
		if (A[i] < 0)
		{
			k = k + 1;
			B[k] = A[i];
		}
		i++;
	}
	cout << "\nFormed vector B\n"
		<< "\n Index    Member \n"
		<< v
		<< endl;
	for (i = 0; i <= k; i++)
		cout << setw(5)
		<< i
		<< setw(10)
		<< B[k]
		<< endl;
	cout << v
		<< endl;
	return 0;
}
Last edited on
I would do it like 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
34
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main()
{
  const int m = 4;
  int num_negative = 0, k = 0;

  int A[m] = { 4, -1, -4, 2 };
  int B[m] = {0};

  for (int i = 0; i < m; i++)
  {
    if (A[i] < 0)
    {
      B[k++] = A[i];
      num_negative++;
    }
  }

  cout << "Negative numbers" << endl;
  for (int i = 0; i < num_negative; i++)
  {
    cout << B[i] << "  ";

  }

  system("pause");
  return 0;
}
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
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    const std::vector<int> mixed { 4, -1, -4, 2, 0, 7, -3, -4, -9, 8 };

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : mixed ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    {
        // custom loop (simple)
        std::vector<int> negatives ;

        for( int v : mixed ) if( v < 0 ) negatives.push_back(v) ;

        for( int v : negatives ) std::cout << v << ' ' ;
        std::cout << '\n' ;
    }

    {
        // algorithm copy_if
        std::vector<int> negatives ;

        // http://en.cppreference.com/w/cpp/algorithm/copy
        // http://en.cppreference.com/w/cpp/iterator/back_inserter
        std::copy_if( std::begin(mixed), std::end(mixed), std::back_inserter(negatives),
                      []( int v ) { return v < 0 ; } ) ; // http://www.stroustrup.com/C++11FAQ.html#lambda

        // http://en.cppreference.com/w/cpp/iterator/ostream_iterator
        std::copy( std::begin(negatives), std::end(negatives), std::ostream_iterator<int>( std::cout, " " ) ) ;
        std::cout << '\n' ;
    }

    {
        // erase-remove idiom https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
        std::vector<int> negatives = mixed ; // make a full copy

        // remove non-negative values
        negatives.erase( std::remove_if( std::begin(negatives), std::end(negatives), []( int v ) { return v >= 0 ; } ),
                         std::end(negatives) ) ;

        std::copy( std::begin(negatives), std::end(negatives), std::ostream_iterator<int>( std::cout, " " ) ) ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/8630cc75b6c3004e
alright guys, thanks thomas and borges, both of your methods worked, and they made me see where my mistake was, I should've written at the end B[i] instead of B[k]
because when it was B[k] it only showed the last negative number. B[i] is the whole vector, B[k] it's just a member I guess (in this case)
well thanks :D
Last edited on
B[i] is the whole vector, B[k] it's just a member I guess

No no. In your loop at lines 24-30, The value of i changes each time though the loop, but the value of k doesn't change. So the loop basically says:
1
2
for i = 0 to 3
    print B[3];
got it, thanks ;)
Topic archived. No new replies allowed.