Rashakil Fol 978 Super Senior Demiposter Team Colleague

- How do I get the one's complement operator ~ to work? I've tried inserting it here and there but all i get is smiley faces or some funky symbols.

The one's complement operator subtracts your number from the maximum possible unsigned integer that can be stored. For example, if you're using an unsigned char, the maximum possible unsigned char is 255. So ~ch is equivalent to 255 - ch. This is equivalent to flipping the value of all the bits in ch. If you're using an int, the maximum possible unsigned integer is equal to 4294967295, so ~x is equivalent to 4294967295 - x.

Try unsigned int x = 45; printf("%u", ~x); . I have a feeling you tried printing out bitwise complemented value as a character, instead of printing its decimal representation.

- In putchar, what does '0' + r do?

'0' is a way of writing the integer value that represents the character 0. This value (in the ASCII character set) is 48. So '0' + r is equivalent to writing 48 + r. Remember that a character is stored internally as an integer.

So if r is 1, then 48 + r gives the value 49, which is the ascii value for the character '1'.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Run the algorithms out by hand in the worst case scenario (you have to figure this out), and while doing so, count the number of swaps and comparisons made. This will be your answer. Troublingly, merge sort would not be implemented with any swapping... and selection sort with 8 elements has a worst case of 28 comparisons, 28 swaps, assuming you move values over by swapping successive pairs of values. (But if moving values is implemented more efficiently, you'll have only 1 instance of what could be called a 'swap'.) Now, 28+1 is closer to the answer 16 than 64, but I'm sure whoever asked this question thinks that '64' is the correct answer.

In conclusion, whoever wrote these questions is nearly an absolute idiot. They want you to go from O(N log N) to 8*log8 = 24? That's lunacy, and that number has nothing to do with the actual number of swaps made. For example, the exact answer to your second problem is 17 comparisons. (Merge sort algorithms generally do not implement any swapping.) I don't blame you for being confused; the questions are written by somebody who does not understand big O notation.

Big O notation says that a value will be no more than a certain _multiple_ of some expression. That multiple could be any positive number imaginable, so big O notation can't be used to get approximations to actual, numerical answers.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Just think: How could your problem be made easier? If you had a built in utility that does [something useful], you'd be able to write this program easily, right? And you'd surely have an easier time it had a member function that [gives some useful information about the ____]. You need to think in this fashion.

Giving you the datatype organization and member functions is basically doing the problem for you -- the actual implementation is a triviality. You need to think harder and longer, and if you can't design the whole thing at once, work on some small part of the problem that you know will be useful.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

You meant BSTree::getRoot.

Rashakil Fol 978 Super Senior Demiposter Team Colleague
Rashakil Fol 978 Super Senior Demiposter Team Colleague

CSS background-image

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Um... yes. But not with the + operator, of course; with some specially built procedure. I don't know what you mean by 'add', either, but surely there's some way for you to write the algorithm.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I understood that you (Rashakil) said that i do have to return something.

No, I recommended using the version (that Narue posted) that returns a value.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The main use of the factorial function is that it's the Hello World of recursive functions. Along with the Fibonacci numbers, it is the first example used in many classes.

Computing individual factorials is usually not useful. But understanding the concept may be useful. They are often found in formulas such as sum n=1..N ((x^n)/n!), which approximates e^x.

If you were to approximate the result of this formula, you would not want to recompute 1!, 2!, 3!, ..., N! over and over again; you'd want to compute the products incrementally. If you have written a bare factorial algorithm before, this would not be hard to do.

If you see code that contains and actually uses a factorial function, it is probably written inefficiently. Somewhat more useful is a 'falling power' function, where falling_power(n, k) = n * (n - 1) * (n - 2) * ... * (n - (k - 1)) = n! / k!. The formula n!/k! is a convenient way of writing and manipulating the falling power (also known as n `permute` k), but it is not the most efficient means of computation.

The only time a factorial function is not an inefficient way to compute things is when you've computed exactly one factorial; otherwise, intermediate results could be used to compute further factorials.

Out of curriosity, how are factorals applied? I've never had to use them out side of math class, and we where never given any real world applications for …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

That way we don't have to correct your silly mistakes.

dubeyprateek: Please don't consider iamthwee's style of response to be representative of anybody else's attitudes here.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

If you bothered to learn the basics of the language, you'd know the answer.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Never knew that programming was so hard..... :sad:

That's just a result of the programming language you decided to learn. C wasn't designed to be the most user friendly language. But in most ways, C is relatively simple.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Your problem is that you don't have a function named Ford, and that's what your Lisp interpreter is looking for. If you want to assign a list to the value, you should assign a list; right now your code wants to call a procedure named Fieata, too...

Rashakil Fol 978 Super Senior Demiposter Team Colleague

If flipbit doesn't return a value, you need to pass a reference so that any changes are reflected back in the calling function:

void flipbit(unsigned& a, unsigned b)

Yustme, note that what Narue posted is valid C++ code, but not C code (and it looks like you're using C based on your other thread). In C you'd want to pass the memory address of the integer, via a pointer:

void flipbit(unsigned* a, unsigned b) 
{
    *a ^= (1UL << b);
}

These are pretty much different ways of doing, but only one is allowed in C. Then you'd need to pass the memory address of your integer, instead, with:

res = v;
flipbit(&res, 4);

But it makes more sense to return the value.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Look on your search engine of choice for 'stringstream'.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

System Command- Non-Portable,Very Expensive

Are you thinking of the same system() I'm thinking of? Are you thinking? system("PAUSE") is not expensive. The amount of time spent waiting for user input is about 3000000000000000000000000000000000000000000000000000000000000000000000000 times (give or take a few dozen powers of ten) as long as the amount of overhead that system() produces.

Portability is not an issue here; a command line program to be ported to other platforms would have the end-of-program wait-for-input removed anyway.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

If it's a substantial amount of money, then you can afford to buy some extra cheap computers.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

And here's the one I was looking for:
http://www.gigamonkeys.com/book/

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Sigh...

If you don't know anything about the language you're using, how do you expect to do anything useful with it? Are you just going to go around asking people to write code for you, without learning anything?

Here's one book you can buy:

http://www.paulgraham.com/acl.html

Here is a pdf to On Lisp, which assumes you know a bit about the language already...

http://www.paulgraham.com/lib/paulgraham/onlisp.pdf

There are countless resources for learning this language online, and I am not in the mood to tell you basics that you could look up for yourself.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Nice to meet you.

Maybe you meant 'else if'? This is not the right forum for your question.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I've spotted a bug! The google ads have images in them!!

Okay, it's just St. Patrick's Day. Never mind...

Rashakil Fol 978 Super Senior Demiposter Team Colleague

i dont know nothing about lisp

Then learn something about it.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

What is the best way to attack any math such as colllege algebra, calc and geometery?

People who do poorly in math tend to stare at problems and say "I don't know how to do this," without trying anything. People who do well in math think, "What if I try this?" where 'this' is something totally arbitrary. Problem solving is a guess-and-check game. After enough experence guessing and checking, human beings tend to become good at guessing what "this" is when dealing with similar problems, and those that do very well at it end up looking like geniouses.

The easiest way to fall behind in math classes is to go through the motions of solving a problem without deeply understanding what's going on. It's better to work harder early in the semester than late, especially in math classes. If you don't, you'll get by for a few weeks, but then you'll reach some point where you really need to understand what is going on, or you'll suffer. I fell into that trap this semester; last week I ended up having to blow off two weeks' homework assignments and about 18% of my course grade.

Things can become really difficult if you have a bad teacher, though. At that point, you could fall back on your textbook, but many math textbooks SUCK.

You should use the word 'college' more carefully online; in some places it means university; in others it means high school or middle school; and I …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Mentioning bias, all of the news reported is biased, you just have to find the one that leans toward your own.

Personally, I pick the ones that lean away.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well, I somehow got converted to threaded mode, while my preferences claimed I didn't...

Rashakil Fol 978 Super Senior Demiposter Team Colleague

(This message put here to point out that I edited my post while you were replying.)

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I'm being shown threaded mode for some reason, while my preferences say otherwise.

[edit: Okay, it looks like I can change it.]

But still, threaded mode on by default is a bad idea. Threaded discussion structures are a bad idea in general; they only got created in the first place because of technical considerations in the design of Usenet. They're only useful for loooooong threads, such as the ones on Slashdot (and we know how brainless the quality of discussion is there).

Rashakil Fol 978 Super Senior Demiposter Team Colleague

You forgot to read my hidden message _jsw. Tee he he.

You
Are
Wrong
Narue
= YAWN

I got it!
[/sarcasm] :-)

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Not bad, but where you say (floor(dAUX) - dAUX) < (ceil(dAUX) - dAUX) , I think you mean (dAUX - floor(dAUX)) < (ceil(dAUX) - dAUX) .

Personally, I prefer floor(dAUX + 0.5) , if I want to always round .5s in the positive direction.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Alternatively, if all you need is to print the rounded double, any formatted output option will do it for you (which opens up ideas for those clever workarounds I was talking about).

Eww! That would just be sick.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

So you'll need to use a list of categories in your function.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Writing a function for this is less efficient than writing a list with these organized into sublists; in fact, a properly written procedure will contain such a list anyway.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

just store the data in an organized fashion in the first place.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Write them in English, and don't be wrong, and they'd be much better.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

My high school had a math teacher teach the few programming classes there. The guy was an absolute buffoon. (He enforced the following programming style:

20 IF x = 3 THEN GOTO 100
...
90 GOTO 200
100 ...
...
200 ...

To his credit, he was also a buffoon at teaching math classes :-)

They replaced him with another math teacher, that they had recently hired, who taught the 'intro to programming' class using Java (since AP CompSci used the same). She was apparently not so much of a buffoon.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

If you can't learn trigonometry and calculus, I don't think you'll be able to get through a bachelor's in college, no matter what your degree is, since most require calc as part of the general education requirements. Though universities do have different definitions of what constitutes having 'learned' calculus...

Also, 'math' is not a verb.

Third, if you're good at writing interesting computer programs, then you're good at math. You might not be inclined to find it interesting, but I see the two abilities as inseperable. Trigonometry and calculus are simple relative to writing any decent piece of software, and if it doesn't seem this way, it's the fault of your teacher. Trigonometry is part of geometry, after all, and so is calculus. Your math teachers are probably making the mistake of looking at the subjects too abstractly, or by enforcing rote learning of at-first-incomprehensible things like trig identities -- as if anybody ever needed to know them in high school.

The main benefit of mathematics courses in computer science (or maybe software 'engineering') curriculums is that good mathematics classes (if you're lucky enough to be in one) teach students to attack problems from many different angles, as if it were of second nature. Also, the ability to think abstractly is an important byproduct -- but just so you know, nobody really thinks abstractly, they all fake it. The actual content of the courses is largely irrelevant, except for some particular subjects, like discrete math.

The …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I can see Java having to be around, for web-based applications,

That's not where Java shines.

and Visual Basic for teaching programming fundimentals,

What?????????????
????????
??????

~cries~

(The problem with Visual Basic, as a relative of mine who uses it jokes, is that it's "just not nerdy enough.")

And, um, Narue answered you with the right answer. I don't know what you expected.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Um, why would you even expect this code to work? Do equalsList, precedesList, and stringToList work as advertised?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

atoi() is a non-standard function;

atoi is part of the C89 standard.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Unfortunately, it gives the idiotic FOX News side of the story.

Of course, all news channels are idiotic.

But I don't get what FOX News has to do with Daniweb.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This depends on your operating system.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This depends on the implementation of malloc.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Yes. Of course, I don't care about BASIC :P

Rashakil Fol 978 Super Senior Demiposter Team Colleague

shd? abt? What do these acronyms stand for???

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It is O(length), but what is the variable s? Is it a string of fixed size? Is it a variable length string? If so, then generally, your running time is O(length * s.length()), because the indexOf method should take O(s.length()) time in the average and worst case. Holding s.length() fixed, your running time is O(length).

Rashakil Fol 978 Super Senior Demiposter Team Colleague

stringstream

Rashakil Fol 978 Super Senior Demiposter Team Colleague

There are three things you must do when you write a solution to a problem:
1. write an algotihm by hand on the paper

BTW, I don't think the English word 'must' means what you think it means.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Note: There is always the risk that the list of powers of M will be very long. That is, even N-1 numbers long. I am not sure; maybe there is some theoretical limit to M, or maybe some probabalistic limit, where getting long lists is very, very, very unlikely. I would personally expect the average list length to tend towards either sqrt(N), log(N), or 0, in terms of relative size. I don't know offhand.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

[Edit: What follows is a horrible description, and I can't even follow it an hour after I've written it, so you might want to prepare for some deliberate reading. It gets a bit better, halfway down. At the end is an actual step-by-step algorithm.]


Example:

Powers of 2 (mod 20)

By taking successive powers 2^0, 2^1, 2^2, ..., we can get this graph.

1 -> 2 -> 4 -> 8 -> 16 -> 12
               ^          |
               \----------/

Notice that the cycle (8 -> 16 -> 12 -> 8 -> ...) repeats every 3 numbers.

Sample problem: Suppose M = 2 and N = 20, and suppose M^(d + t*N) = 16.

We can't talk about anything like M^(-t*N) or M^(-N), because they don't really mean anything. But we can do the equivalent, by backing up N steps in the cycle (8-> 16 -> 12 ->). Because we're moving backwards, let's redraw the whole thing with backwards arrows.

1 <- 2 <- 4 <- 8 <- 16 <- 12
               |          ^
               \----------/

(If M^x = 16, then we know that M^(x - 1) = 8. But we don't know the value of M^(x - 2). It could be 4; it could be 12.

We're going to assume (first) that by backing up 20 steps, we remain within the 8->16->12->... cycle. Since every 3 steps brings us back to the same value, this can be expedited by computing (20 MOD 3). (Or, …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

More info: The only case where you'd be able to step backwards, without knowing the values of d or t, is if M's powers form some kind of cycle whose duration is a factor of N. If you can ensure that d is greater than some minimal value (dependent on M and N), then you can step backwards.

In short, powers of numbers in any modular arithmetic system enter cycles at some point. For example, in the mod-7 number system, 2^0 = 1, 2^1 = 2, 2^2 = 4, 2^3 = 1, 2^4 = 2, and so on. This is a cycle of duration 3. If you know that M is 2, you can calculate the elements of and the length of this cycle. Then if you find that M^(d + t*N) = 4, you can at least know that M^d is either 1, 2, or 4, but nothing else. If you know, for example, that t = 100, then 4 = M^(d + 700) = M^d * M^700 = M^d * M^1 = M^d * 2. So you know that 2 * M^d = 4. This alone does not mean that M^d = 4, though (you have to be careful because you're working with a modular number system). But you know that M^d is either 2, 4, or 1. And 2*2 = 4, 2*4 = 1, and 2*1 = 2, so then you know that M^d = 2.

The reason you sometimes need a sufficiently large power …