What coding can removes the negative integers from integers??
Jun 8, 2012 at 7:27pm UTC
can anyone suggest?
Jun 8, 2012 at 7:28pm UTC
Something tells me you're looking for std::abs.
Jun 8, 2012 at 7:31pm UTC
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 Jun 8, 2012 at 7:35pm UTC
Jun 8, 2012 at 9:05pm UTC
any other easy coding??
Jun 8, 2012 at 9:12pm UTC
It is not clear what are your "negative integeres in integers".:)
Jun 9, 2012 at 2:50am UTC
for example : 60 59 -76 -90 53 99 -28
the question what coding that i can removes this negatives values without changing the order?
Jun 9, 2012 at 2:59am UTC
And what is it? Where are these values stored? If they are stored in array you can use the code I showed above.
Jun 9, 2012 at 3:19am UTC
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();
}
}
Jun 9, 2012 at 3:24am UTC
Your code is incorrect because the value of class member item is undefined.
Last edited on Jun 9, 2012 at 3:24am UTC
Topic archived. No new replies allowed.