What coding can removes the negative integers from integers??

can anyone suggest?
Something tells me you're looking for std::abs.
Standard algorithm std::remove_if can remove elements of a sequence by condition.
If you mean an array then you can not remove elements from an array but you can substitute them for some predefined values. In this case you should use std::remove_if together with std::fill.

For example,

1
2
3
4
int a[] = { 1, -2, -3, 4, 5, 6, -7, 8, -9 };

std::fill( std::remove_if( std::begin( a ), std::end( a ), 
                                     [] ( int x ) { return ( x < 0 ); } ), std::end( a ), 0 );
Last edited on
any other easy coding??
It is not clear what are your "negative integeres in integers".:)
for example : 60 59 -76 -90 53 99 -28
the question what coding that i can removes this negatives values without changing the order?
And what is it? Where are these values stored? If they are stored in array you can use the code I showed above.
but my coding is like these..hopes not affect the problem..


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
using namespace std;
#define size 10

class number
{
private:
	int array[size];
	int head,tail,count,item;
public:
	number();
	~number();
	void add(int);
	void removes();
	bool empty();
	int display();
};


number::number()
{
	head=0;
	tail=-1;
	count=0;
}

number::~number() {}

bool number::empty()
{
	return (count==0);
}



void number::add(int value)
{
	if(count<size)
	{
		tail++;
		if (tail==size)
			tail=0;
		if(count==0)
			head=0;
		array[tail]=value;
		count++;
	}
	else
	cout<<"Queue is full!!"<<endl;
		
	
}

void number::removes()
{
	if(!empty())
	{
		array[head]=item;
		head++;
		if(head==size)
			head=0;
		if(count=0)
			head=0;
		count--;

	}
	else
		cout<<"\t\tQueue is empty!!"<<endl;
}

int number::display()
{
	if(empty())
		return false;
	else
	{
		
		cout<<array[head]<<" ";
		head++;
	}
}

void main()
{
	number value;
	int num;
	

	cout<<"Please insert the integers that you want: ";
	for(int count=0;count<10;count++)
	{
		cin>>num;
		value.add(num);
		
	}

	for(int count=size;count>0;count--)
	{
		value.display();
		
	}
	
}
Your code is incorrect because the value of class member item is undefined.
Last edited on
Topic archived. No new replies allowed.