Rashakil Fol 978 Super Senior Demiposter Team Colleague

Define 'provider' and define 'consumer'. What do you mean by 'putting a car in a garage'? That analogy can mean different things. (Maybe just tell us what you're doing.)

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Thanks, but just to recap, my question is: Is it possible to create a data type which is not bounded to predefined C++ data types. I can create my own classes. but that would still rely on them. ( int, char, double whatever.. ). E.g.: what If I want to create a 6000 byte long number? ( I know, it would be insane, but just for the sake of curiosity.)

What the fuck is wrong with you? The mechanism for creating datatypes in C++ is classes. Why wouldn't you use classes?

In the future, please keep your insanity to yourself. The insanity isn't wanting a 6000 byte number, it's asking how to create datatypes when the mechanism for creating datatypes is staring you in the face.

I'm being mean. If you wanted to create a 6000 byte long number, you would do something like this:

class int6000 {
    uint32_t data[1500];
  public:
    // ...
};

uint32_t is not a C++-standard type, I don't think, so substitute whatever 4-byte integer type you want if it doesn't work on your compiler (try #include <limits.h> to make uint32_t available).

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Your question makes no sense -- ArrayList is already in .NET 1.1.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well, Partition can clearly return an array. And you can obviously alter the array that it returns. And you can easily construct an an array. I don't understand your question.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I suggest you call Partition on the tail _before_ you examine the first element of the list. Then update the array that Partition returns appropriately to account for the first element of the list. Only construct the array when Partition receives an empty list.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The danger lurks because the pointer could be null, or the pointer could be pointing at something that no longer exists. It's easy for programmers to make mistakes like that. It's slightly harder to do this with references. Also, there ends up being less syntax involved, and the person using the function doesn't have to wonder whether the argument may be a pointer to an array.

The reason the first const is used in that code example (in const sVec4& other ) is because that means the function is promising not to modify the value of 'other'. It receives a reference to the variable and promises not to modify it (and if a developer accidentally tried to do so later, the code wouldn't compile). There are other de facto agreements too -- because the variable's passed by reference, the function won't do anything that assumes the variable exists after it has returned. I mean, it could, but that would look weird and the programmer would have to be really drunk to do such a thing.

The reason for the second const, that goes after the argument list, is to promise that the function dot_xyz won't modify the object it's called with. Suppose the function dot_xyz is a member of the class X. If you had a const X lying around somewhere, you would not be able to call the member function dot_xyz if it didn't have that const declaration.

The reason const declarations are useful is because …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It's the same thing -- once the code has been compiled -- and used for the same purpose. It's better to use the second way, passing "by reference," because the first desensitizes you toward pointers. (Your spidey sense should tingle when you see a pointer -- it means danger lurks.)

It's very common, especially, for functions to take arguments passed by a const reference.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

There's no "**" operator in Java. Either use Math.pow or write your own squaring function.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

is there any c++ equivalent of doing that?

Nope :)

cikara21 commented: ok.. +1
Rashakil Fol 978 Super Senior Demiposter Team Colleague

Things are case-sensitive in Java.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

A struct defines a value type; a class defines a reference type.

A value type is one whose values cannot be separated from the variable. If you try copying it to a different variable, you'll make a complete copy. You don't really modify value types -- you modify the variables that contain them.

Compare this with class types, where variables contain references that point to objects -- where two variables of that type may contain references that point to the same object.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Also, there's the option of inlining the code, so that you don't incur the cost of a function call (which seems to me like it would be a problem in Python).

No seriously, write this performance-intensive part in C.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It's not necessary to right-shift a value immediately before you multiply it by a constant. So:

def luminance(pixel):
    """Calculate and return the luminance of a pixel."""

    return 0.299 * (pixel & 0xFF) + 0.002293 * (pixel & 0xFF00) + .00000174 * (pixel & 0xFF0000)

I assume removing the doc comment doesn't help, but maybe you shouldn't.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Write it in C.

Edit:
Well, for starters,

g = (pixel >> 8) & 255;
Rashakil Fol 978 Super Senior Demiposter Team Colleague
enter region = O;
enter commodities = 2;
enter saler = 1;
prise = 500$;

What does this code even mean? It is not C#.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

No, it says you have non-exhaustive patterns!

In particular, you defined the function binadd for non-empty lists with:

binadd (x:xs) (y:ys) n = ...

But you didn't define the function for empty lists! What is the value of binadd ([True]) [] True ? Your function's behavior is not defined, because the empty list [] doesn't match the pattern (y:ys) . You need new cases below to handle empty lists, probably something like

binadd xs [] n = ...
binadd [] ys n = ...

But whatever you do, make sure your function can handle all the cases. I'm pretty sure there is some warning you can turn on that will tell you about functions that don't handle all the conceivable cases.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It makes a copy of the string. For it not to do so would be counter-intuitive -- if it wanted a pointer to a string, it would ask for one.

In particular, you'll note that the constructor takes a const string. Which means that there's no way in heck that the stringstream could use the string it receives for its internal buffer.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The idea is to learn about Lists, not to learn the different ways to iterate. I doubt that the above example would have helped in understanding how to add and get elements from lists.

Yeah, I know, and you're right. Please regard my comment as a nonsequitor.

Rashakil Fol 978 Super Senior Demiposter Team Colleague
for (int i=0;i<list.size();i++) {
  String s = list.get(i); //you do not need to cast it to String like the previous example because of the way you defined the list above
  System.out.println(s);
}

Leave it to a Javacoder to write code in the most obtuse manner possible. (Yes, I'm being unfair :))

for (String s : list)
    System.out.println(s);

my question is, is it true that there is no need to write a LinkedList class in java?

There's no need to write one in C++, either. #include <list>

Rashakil Fol 978 Super Senior Demiposter Team Colleague

d2b2 evaluates to something of type [Int] , right? Which means the expression (d2b2(n / 2):1) is trying to pass an [Int] as the left-hand argument of the (:) function. Since the (:) function is of type a -> [a] -> [a] , that means it expects on the right-hand side a list of whatever was on the left hand side. That means it expects a list of [Int] , i.e. something whose type is [[Int]] . Instead of an expression of type [[Int]] , you're supplying 1, or 0, something of type Int . That's the reason your error message is the way it is.

The function (:) is for appending elements to the left end of a list. If you want to append an element to the right end, wrap the element in a list an use a (++) operator. For example: [1,2,3] ++ [x] => [1,2,3,x] .

You have a number of other problems with your code. One of them has to do with this question: What do you expect 'return' to do?

chunalt787 commented: Great responses very informative and patient, thanks so much +1
Rashakil Fol 978 Super Senior Demiposter Team Colleague

Where you wrote ostream &Month::operator<<(ostream &strm, const Month &obj) , you should be writing ostream &operator<<(ostream &strm, const Month &obj) . The function you're defining is not declared as a member function of the Month class.

Edit: (The same goes for your operator>>.)

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The reason is that blah = 1 sets the lvalue blah to 1. You want blah == 1 , which evaluates to true if blah is 1. It's generally a good idea in C++ to get in the habit of putting constants like 1 on the left-hand side of equality comparisons. That reduces the probability of making this kind of error.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Aren't those manuals normally reserved for teachers and educational instutions???

No.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

What is the purpose of this post?

For instance, although c# is a powerful language for developing many types of applications, it is impossible to create an operating system using c# because the language is not close the hardware. For such needs C is the language of choice as it is really close to hardware.

Reality disagrees with you.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Nobody could help you unless you listed specific books for which you wanted solutions manuals. And since nobody here would know how to find them off the top of their heads, you might as well do the googling yourself.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Why do you have a class named Swap?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well from what I see, it walks through the permutations but eventually overwrites the array with 3 3 3 3. There is no way anything will be able to see the array in a different state.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

But what if that child has children, and it's children have children? See my problem?

Read my function (now functions) again, you'll see that it deletes the children.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

In PHP, I would have just set up parentID as a foreign key referencing the CatID on delete cascade; however I've found that in C#, I'm not allowed to set up a foreign key constraint that self-references a column in the same table (lame!).

This doesn't make any sense. Constraints aren't specified in C#. What database are you using?

Edit: oh, I see. More reply pending.

Edit 2: One option: write a function that does what you say, deleting the children first.

void DeleteCategoryTree(int catID) {
    int[] children = GetChildIDs(catID);
    foreach(int child in children)
        DeleteCategoryTree(child);
    DeleteCategoryRow(catID);
}

Edit 3: And if you think there can be children so deep as to cause a stack overflow, use a Stack.

void DeleteCategoryTree(int catID) {
    var stack = new Stack<int>();
    var deletionStack = new Stack<int>();
    stack.Push(catID);
    while (stack.Count != 0) {
        int id = stack.Pop();
        deletionStack.Push(id);
        foreach (int child in GetChildIDs(id));
            stack.Push(child);
    }
    while (deletionStack.Count != 0) {
        DeleteCategoryRow(deletionStack.Pop());
    }
}
Rashakil Fol 978 Super Senior Demiposter Team Colleague

You're not describing what you expect the code to do. I would expect the code to fill the array with threes.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Flow charts are basically an awful way to design a program and an awful way to represent the design of a program. If you asked me to name any "good" ways to _represent_ the design, I couldn't. But I think type systems are pretty good.

The very idea of just jumping in and coding is completely nonsensical. It's not like you just jump in and write code that (somehow) will lead you to the solution. You surely need think about what information you will be manipulating and how you will communicate information first. The phrase "what information you will be manipulating and how you will communicate information" is also called "the design".

Rashakil Fol 978 Super Senior Demiposter Team Colleague

But when a static variable is required, then that is completely different. This way ALL calls to the method regardless of instance have the same static variable.

Yes, we know what a static variable is.

That is typical for specialization of event type in a monte-carlo, base-class has getCell and specialization class knows about the event that can take place, the new energy/velocity of the particle.

I have no clue what you're talking about.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

A better solution would just to make that variable a member of the class. Yours would require extra code to check that the same class is receiving the call as before. And it wouldn't be thread safe.

But okay, unnoticeable static variables are pretty reasonable. C++ makes doing things a more safe way (regarding risk of bugs) so awfully verbose that I can understand.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The ability to have static variables in the middle of functions is a crufty feature left over from the old days of C.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I'm guessing it would just act like a static variable. I wouldn't make use of it at all -- put any static constants you have in your class definition and don't use static variables.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Why do you say static variables will be disposed of at the end of the function call?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well it seems you didn't read my reply, so never mind.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

What the fuck? Your random function isn't random any more.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Your problem is that you're recreating the file every time through the loop. Thus, the old data gets replaced.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

One reason you can't just "catch and handle" pointer errors is that if you overwrite something in the wrong place of memory, it can totally screw with the behavior of your program. You could have overwritten other variables, especially hidden values like the addresses the processor would jump to after it returns from a function. In particular, the mechanism C++ uses for propagating exceptions might have been totally corrupted. That's why you can't rely on the exception system of C++ to handle errors where pointers point to unmodified locations.

The only reasonable way to protect yourself from these kinds of errors is to watch things from a separate process. This is one reason why the web browser Google Chrome has all its tabs in separate processes.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Using pointers directly is undesirable because pointers are dangerous. If you have a pointer, the thing it's pointing to could become destroyed, so you have to make sure that hasn't been done. Also, if you're pointing to things manually on the heap, you have to make sure to destroy those things -- so you have have to keep track of who owns what. Keeping track of things like this is a pain. There are lots of ways to avoid using pointers. For starters, use vectors instead of raw C-style arrays. Pass arguments to functions by reference instead of passing pointers. Use the various kinds of smart pointers instead of raw C-style pointers.

Since you are new to C++, you don't need to worry about any of the things I've said! Continue to use pointers so that you can learn the low-level details of the language, and later, you will appreciate the other tools that are provided.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well, error catching is what try-catch blocks are for...

But if you're talking about some pointer-related bug, the right answer is to avoid using pointers directly in the first place. I don't know what your program is trying to do -- why are you deliberately indexing past the end of an array?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Hm yes, VernonDozier identified what was actually causing your problem. Fix your random function, too, though.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Your function random is broken! This is an awesome bug. It's broken in two ways.

First, it's broken because rand() may return RAND_MAX. In such a case, your random function is clearly broken, because it will return the value ceiling.

So the quick fix is to divide by RAND_MAX + 1.0 .

Now, you have to be careful. Even then your code is broken! Try running this code.

#include <cmath>
#include <cstdlib>

#include <iostream>

int main()
{
  double f = static_cast<float>(RAND_MAX - 64) / (RAND_MAX + 1.0);
  double g = static_cast<float>(RAND_MAX - 63) / (RAND_MAX + 1.0);

  std::cout << "f < 1.0: " << (f < 1.0)
	    << "; g < 1.0: " << (g < 1.0)
	    << std::endl;
}

You'll notice that it outputs false for one of the values, and true for the other!

Why? Why, the datatype float doesn't have enough precision to represent the value RAND_MAX on most computers (on some computers, RAND_MAX is 32768, which is small enough so that it can). So when you convert RAND_MAX to a float, it rounds to the nearest acceptable float. You can see that if rand() returns the value RAND_MAX - 63 or greater, your function random() will treat it the same as if it had returned RAND_MAX. float can only exactly represent integers up to 16777216, which is 2 to the 24th power.

So your problem's probably that rand() is returning a value too large for a float to distinguish between …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Also, it's important to delete them because they might have other stuff to do (like flushing data to filehandles, and such).

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It doesn't remain "in memory" because the process has completed and all of its memory is reclaimed by the operating system.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Yes, it does remain. If new operator is used u need to delete explicitly.

What the fuck are you talking about? When the program ends, nothing remains.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Header files typically contain declarations of functions, not their definitions. Typically, the implementations are placed in a similarly named cpp file. Your project will then have multiple cpp files.

Also, #define is not how you declare or define functions; I don't know what you're thinking.

For example, I might want to have a function fooz available by a header file "foo.h". So in foo.h I'll have

#ifndef guard_foo_h_
#define guard_foo_h_

int fooz();

#endif /* guard_foo_h_ */

And in foo.cpp

int fooz() {
    return 4;
}

Then, some other file, like bar.cpp, might #include "foo.h" so that the compiler sees the declaration of fooz. Or the writer of bar.cpp could just manually write

int foo();

to inform the compiler about the declaration.

Then, after you compile foo.cpp and bar.cpp and link them together, you'll get a working program where a function written in bar.cpp calls a function written in foo.cpp.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This is absolutely awful. Do not do what you are talking about doing.

Tell us what your problem is, what you really are trying to achieve, instead of asking about one particular mechanical option, so that a better solution can be recommended.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Maybe this is a stupid question, but why you writing myvector->push_back(...) instead of myvector.push_back(...) ? myvector is a cliext::vector<T>, not a cliext::vector<T>^.