Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>but the code seems to work ok.
Perhaps my wording was poor. If a and b refer to the same object, the result will be 0, regardless of what the original value was. Sure, you may argue that this is an unlikely event, but I've seen it fairly often, especially in sorting algorithms.

Oh you mean x ^= x?. I see your point about it not working when you want to swap the two values when they are both the same variable. swap function should never be called when the two variables are the same.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Without special case tests, the XOR swap is hopelessly broken.

How? my compiler has no problems with it. Of course you can't see the difference if x = y, but the code seems to work ok.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

How is x ^= y; y ^= x; x ^= y; unsafe when x and y are equally sized integers? Is there a problem with using xor for signed integers?

it has the same overflow problem that my template has.

Well, I wouldn't really use it, because I've never swapped integers in my life.

Are you still a student? If not, how long have you been programming? Swapping integers (and other data types) is a common thing in sorting algorithms.

As for swapping strings, there is a member function named 'swap' which tends to achieve the task in constant time.

probably implemented like this

void swap(std::string& s1, std::string& s2)
{
   std::string temp = s1;
   s1 = s2;
   s2 = temp;
}

I know of no other way to do it with std::strings.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

i'll remind you of the func I was commenting on...
Obviously this is flawed. Cant you see that? It has potential for overflow and producing wrong results.

That was my first reaction too -- and yes, it does indeed produce overflow. But when tested with very large numbers -- INT_MAX from limits.h -- the template still produced the correct results with my compiler. I don't think there is a safe way to swap two integers without using a temp variable.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>
What are you babbling about? Your template function cannot swap strings, so why are you talking about strings? I get the feeling you're in the wrong thread.

Yup. got my threads confused :D

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

no it doesnt work you disregard any overflow errors. That to me makes it superflawed!

That was aimed at ancient dragon on his template swop two numbers function.The perils of replying before reading page 2

[edit]my stupid comments deleted because they were for wrong thread :sad: [/edit]

Page 2 didn't exist when I posted that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>And why do you think it is sooo stupid
Because it's useless trivia. Questions like this are basically like tricky riddles: unless you already know it, you probably won't figure it out on your own. You also won't use it in the real world.

yes, its a trivia question, but one that will separate the real programmers from the dabblers, the chaf from the weat, the men from the boys, thinkers from the robots -- you get the idea :) It teaches creative thinking, and isn't that one of the goals of higher eduational instutions?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>
>2.Exchange two numbers without using a temporary variable.
Translation: Explain why exchanging two numbers without a temporary variable is the height of stupidity.

And why do you think it is sooo stupid (unrelated to you comments about use of macros, which I agree with)? Did you read the template I posted (not my original code)? works with integers, floats and doubles. Won't work with c++ classes or C-style strings. I think the only way they can be swapped is via temp variable.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Now you got me started, for swapping either integers or floats this will work too ...

Starting with your example, you could use a c++ template.

#include <iostream>
#include <limits.h>
using namespace std;

template<class T>
swapm(T &a,T &b)
{
	a = a + b;
	b = a - b;
	a = a - b;
};

int main()
{
    int a = 1;
    int b = 7;
    
    cout << "a = " << a << "   b = " << b << endl; 
	swapm(a,b);    
    cout << "a = " << a << "   b = " << b << endl; 

    
	float c = 1.23F;
	float d = 2.34F;
	
    cout << "c = " << c << "   d = " << d << endl; 
	swapm(c,d);    
    cout << "c = " << c << "   d = " << d << endl; 

    cin.get();   // wait
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

gcc will never work under DOS, it's 32 bit only ;)

Yup -- Narue already gave me a bloody nose for that one :mrgreen:

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you are quite right -- I should have said MS-Windows, not MS-DOS (which is almost, but not quite, a dead os). That was just a slip-of-the-fingers :)

Never heard of Textpad program. I'll google around for it. I do all my programming with Microsoft tools (VC++ 6.0 and eVC++ 3.0/4.0 compilers). I love vi when doing *nix programming, which isn't all that often.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Comeau may be a conforming c++ compiler but it contains no C support, no standard libraries either C or C++ and no STL. You have to get all that from someplace else. And as far as I can tell it is only command-line driven c++ compiler with no IDE -- no big deal for *nix programmers because they work in the ancient past anyway.

For MS-DOS, the best FREE conforming compiler is Dev-C++ from bloodshed.net. There are better compilers, but they are not free.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I turned off pm because someone started spamming me.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

search google -- that compiler is free for the downloading.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
if( *src <= '9' )

what if *src < '0' ? isdigit() will catch that, but your if statement won't.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can use javadoc notations in any program file written in any computer language. Not sure what program reads them and creates the html file. I had to make javadoc comments in my programs about a month or so ago, but I did not have to create the html files.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Computer software developed for the public domain, which can be used or copied without infringing copyright. Programmers typically get paid a small one time fee from users who find the software useful.

you worked very very hard to make that post with so many different colors :mrgreen: Is there an editor that will do it for you or did you have to do all that manually :?:

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That does not supprise me. You cannot just plunk the example code I posted into your program and expect it to compile. You will have to change it to fit in your program, or modify your program to use the code.

The prototype error means that there is a difference between what you put in the class and what you write in the implementation file. Both functions must match exactly (except for virtual keyword if it exists).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

almost

t1 = (time1.hour*60) * time1.minutes;
t2 = (time2.hour*60) + time2.minutes.
diff = abs(t2 - t1);
hours_in_interval  = diff % 60;
minutes_in_interval = = diff/60;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Make it easy for yourself -- convert both time1 and time2 into minutes, subtract them and use the absolute value. No need for all those comparisons.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

the digits in the file have to be converted to binary when inserting them into an int array. For example the string "123" can be converted to it like this

char nm[] = "123";
int n = 0;
for(i = 0; nm[i]; ++i)
  n = (n * 10) + nm[i] - '0';

you can also use scanf

char nm[] = "123";
int n = 0;
sscanf(nm,"%d",&n);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is the desired output?

JOVIAN1 m=8.08181550000000040E+001 r=20.D0 d=112595.65

if that last number is what you want, then you have to remove the scientific flag. You can combine some of those setflags by using | operator.

file<<" JOVIAN1     m="<< setprecision(17) << setiosflags(ios_base::uppercase | ios_base::scientific) 
     << j1 <<" r=20.D0 d=" << resetiosflags(ios_base::scientific) << setprecision(2) << setiosflags(ios_base::fixed) << ((j1/JUPVOL) * SOLM2G) << endl;

Also -- srand() (and its variants) should only be called once at the very betinning of the program. you should remove it from that loop.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have not coded the implementation of interval_since().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

for floats look again at the cout line in my original post.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

functions in <ioman>. Here are other values that can be used for setioflags function.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	float n = 123.456F;
	// output float with 5 decimal places
	cout << setprecision(5)  << setiosflags(ios_base::fixed) << n <<  endl;
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

since you are using dev-c++ compiler I assume you are programming on some version of MS-Windows operating system. See win32 api function MoveFile() or MoveFileEx(), which will move a file or an entire directory.