Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
void flip ( int i )
 
{
  assert( i==1 && i==0);
  if(i = 1)
    i=0;
  else
    i=0;
}
 
int main()
{
  int i = 1;
  assert(i==1);
  flip(i);
  assert(i==0);
  flip(i);
  assert(i==1);
  cout << "Success!\n";
  return 0;
}
 
[HB: add pre tags]
Posted 12-Feb-13 18:21pm
Edited 12-Feb-13 18:44pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The assertion in flip() will always fail because you are trying to assert that i is equal to both 0 and 1. You also change the value of i (locally) to zero regardless of input value.
 
The call to flip() does not change the value of i in main(). You need to make flip refer to the variable i instead of a copy of its value: ie. "void flip(int* i)".
  Permalink  
Comments
Albert Holguin at 13-Feb-13 18:16pm
   
Good input... but there are oh so many errors in that code. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Couple of things, i think are wrong
 
assert( i==1 && i==0); // Why &&. I think this should be ||
  if(i = 1)
    i=0;
  else
    i=0;   // Here you will have to set i = 1 (assuming this is what flip function is supposed to do
 
EDIT: the "=" in your if conditions should be changed to "=="
  Permalink  
v2
Comments
Albert Holguin at 13-Feb-13 18:13pm
   
You missed the if(i=1) error, that's setting the value to 1 all the time.
Rahul Rajat Singh at 13-Feb-13 23:15pm
   
OOPs, My bad. I will correct the mistake. Apparently I am spending too much time with sql nowadays and this "=" started seeming ok with if :)
Mas11 at 13-Feb-13 23:39pm
   
Nice Answer ! My 5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100