• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Well all my prof told us was the Cartesian product of AxBxC is the set of all the triples (a,b,c) where a is an element of A, b is an element of B and c is an element of C.

Yeah, sorry about that I got them mixed up. The wiki article makes it seem pretty straight forward. You solution seems to be right.
 

Beowulf28

Member
I'm trying to finish this program and I need help fixing my format. It's a program simulating the game of life and I'm getting the correct output but not in the correct output. For example this is what it should be:

The Game of Life.
How many steps in time? 1
What size is the grid? 2

Enter the initial grid layout:
--
--

After 1 step(s):
--
--

but this is what I'm outputting

The Game of Life
How many steps in time? 1
What size is the grid? 2
Enter the iniial grid layout:

--
--
After 1 step(s)
----

this is the part of main that's printing it (C# btw):

Code:
 LookAround(timeStep, size, grid);
            Console.Write(" After {0} step(s) \n", timeStep);
            for (int y = 0; y < size; y++)
            {
               
                for (int x = 0; x < size; x++)
                {
                   Console.Write("{0}", grid[x,y]);

                }
                
            }
 

usea

Member
Are you referring to "----" being all on one line?

Try adding a line break after your inner for-loop:
Code:
for (int y = 0; y < size; y++)
{
	for (int x = 0; x < size; x++)
	{
		Console.Write("{0}", grid[x,y]);
	}
	Console.WriteLine();
}
 

milsorgen

Banned
"It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration."

I've never actually used Basic so I can't say if I agree :p

Visual Basic 6 along with HTML and within the same time frame HTML/CSS began my interests in coding. VB6 like any tool is appropriate for some jobs but not all.
 
Visual Basic 6 along with HTML and within the same time frame HTML/CSS began my interests in coding. VB6 like any tool is appropriate for some jobs but not all.

Though VB6 would be a good thing to teach people on, its just as easy to learn VB.NET and you get the awesome features of VS and you learn alot about .NET framework which makes you incredibly valuable. I don't think they lost anything not supporting VB6 since its pretty much the exact same language in VB.NET.

Though in the real word companies who still code in Vb.NET do it if they started their code base in VB and want to keep it the same. I have worked a few places that have this idea, since when they started C# was not mature but VB.NET was. Now if you go into a young company that codes in C# or another language like that, you are chastised if you code in VB. An interviewer actually told me that I would chastised if I admitted to coding in VB.NET vs C#. Then again they offered me half of what the position should have paid then dropped the offer by 15% on the official letter vs over the phone so I don't really trust them anyways.
Basically most places use .NET since its pretty good, and most chose C# when making that choice. Ys VB has its place just like any language but if you want to be flexible just learn how languages work and C++ and you can learn any language in a day or 2.
 
but if you want to be flexible just learn how languages work and C++ and you can learn any language in a day or 2.

This is kind of true but be aware that if you want to learn a language that has a radically different paradigm from the imperative/object oriented (like a Lisp or something) it will take a lot longer than 2 days. This is why I think people should learn at least 1 language from each of the main philosophies in language design rather than just stick with OO and call it a day.

Especially since functional is the future. Believe!
 

leroidys

Member
I hate functional languages, but they are so useful for doing currently relevant stuff like NLP and concurrent programming that I think they're going to become more widespread again.
 

Godslay

Banned
Though VB6 would be a good thing to teach people on, its just as easy to learn VB.NET and you get the awesome features of VS and you learn alot about .NET framework which makes you incredibly valuable. I don't think they lost anything not supporting VB6 since its pretty much the exact same language in VB.NET.

Though in the real word companies who still code in Vb.NET do it if they started their code base in VB and want to keep it the same. I have worked a few places that have this idea, since when they started C# was not mature but VB.NET was. Now if you go into a young company that codes in C# or another language like that, you are chastised if you code in VB. An interviewer actually told me that I would chastised if I admitted to coding in VB.NET vs C#. Then again they offered me half of what the position should have paid then dropped the offer by 15% on the official letter vs over the phone so I don't really trust them anyways.
Basically most places use .NET since its pretty good, and most chose C# when making that choice. Ys VB has its place just like any language but if you want to be flexible just learn how languages work and C++ and you can learn any language in a day or 2.

There is still a lot of legacy VB code out there. I've maintained some of it at my last job. Some of the stuff that they ran simply made sense to keep it running in VB rather than update it to C# because of the time and effort it would take to do so.

I can't believe that you were chastised for being able to code in VB. I hate coding in VB, but still I think it's a valuable skill, plus switching to C# would be that much easier due to working with .Net in VB.
 

hateradio

The Most Dangerous Yes Man
This is kind of true but be aware that if you want to learn a language that has a radically different paradigm from the imperative/object oriented (like a Lisp or something) it will take a lot longer than 2 days. This is why I think people should learn at least 1 language from each of the main philosophies in language design rather than just stick with OO and call it a day.

Especially since functional is the future. Believe!
I don't think they'll be the future for web development. Thankfully. :p
 

Kad5

Member
Hey guys i'm taking an intro to computer science class this semester. Would I be ok also taking a pre-calculus class at the same time or would it be too much of a load you think?
 

Omikron

Member
There is still a lot of legacy VB code out there. I've maintained some of it at my last job. Some of the stuff that they ran simply made sense to keep it running in VB rather than update it to C# because of the time and effort it would take to do so.

I can't believe that you were chastised for being able to code in VB. I hate coding in VB, but still I think it's a valuable skill, plus switching to C# would be that much easier due to working with .Net in VB.

Absolutely. There is so much out there that is coded in VB. Basically because it was easy. Criticising someone for having experience in multiple languages seems utterly ridiculous.
 

Fantastical

Death Prophet
Hey guys i'm taking an intro to computer science class this semester. Would I be ok also taking a pre-calculus class at the same time or would it be too much of a load you think?

Are you a computer science major? I can't speak for all universities, but at my university the intro class was pretty easy. A lot of different majors take that class to fill a gen ed. requirement and because of that they took it really slow. The classes after that moved a lot quicker, though.

I think you will be fine personally, but I don't know what kind of workload you are used to.

Also, I have a programming question.

I'm trying to format some simple output to match my professor's output, but I don't understands something.

Right now I have this:

Code:
cout << fixed << setprecision(2) << amount << endl;

This outputs the amount with 2 decimal places. The thing is that when the amount is not a decimal, it should not print out any decimal places. For instance, it would need to output "9" not "9.00" but if the number was 4.01 it should output "4.01".

Is there any easy way to do this without if statements or anything. I thought that it could be done simply, but a Google search isn't helping. If it isn't simple don't bother - I can ask a TA tomorrow, but if it is would you mind helping me out?
 
Hey guys i'm taking an intro to computer science class this semester. Would I be ok also taking a pre-calculus class at the same time or would it be too much of a load you think?
Algebra is pretty much all you need til Discrete Math and Data Structures/Algorithms where Calculus is a pre-req.
 

Gartooth

Member
Learning Assembly code right now and I'm awful at it. Having a hell of a time trying to get a simple "if" statement working in it.
 

leroidys

Member
Hey guys i'm taking an intro to computer science class this semester. Would I be ok also taking a pre-calculus class at the same time or would it be too much of a load you think?

Do it. If you're a CS major, also take statistics, set theory, and a proofs class ASAP. Even if they're not required, they will be immensely helpful.

Are you a computer science major? I can't speak for all universities, but at my university the intro class was pretty easy. A lot of different majors take that class to fill a gen ed. requirement and because of that they took it really slow. The classes after that moved a lot quicker, though.

I think you will be fine personally, but I don't know what kind of workload you are used to.

Also, I have a programming question.

I'm trying to format some simple output to match my professor's output, but I don't understands something.

Right now I have this:

Code:
cout << fixed << setprecision(2) << amount << endl;

This outputs the amount with 2 decimal places. The thing is that when the amount is not a decimal, it should not print out any decimal places. For instance, it would need to output "9" not "9.00" but if the number was 4.01 it should output "4.01".

Is there any easy way to do this without if statements or anything. I thought that it could be done simply, but a Google search isn't helping. If it isn't simple don't bother - I can ask a TA tomorrow, but if it is would you mind helping me out?

Just glancing at it I think you would have to have an if statement. You could do something like

Code:
if (x mod 1 != 0)
     {
//print number
      }
else
     {
//cast as an int and then print
      }

or something like that. I think that would work, I'm not a c wizard though, I'm not sure how it handles FP with mod. You might get some edge cases with bad output as well. A more involved approach would be to read and adjust for the bias, and then & your number with 23 1s (assuming IEEE FP), and if !=0 (etc.).

Anyway, let me know what you find out. I'm always eager to learn more about C.
 
I'm not a c wizard though, I'm not sure how it handles FP with mod. You might get some edge cases with bad output as well. A more involved approach would be to read and adjust for the bias, and then & your number with 23 1s (assuming IEEE FP), and if !=0 (etc.).

Anyway, let me know what you find out. I'm always eager to learn more about C.

It tends to shit itself with FP roundoff. Usually you'd compare it to some suitably small epsilon value and decide it was a whole number based on that.

Something like:

Code:
double epsilon = 10e-7;
if (abs(floor(x) - x) < epsilon || abs(ceil(x) - x) < epsilon)
{
 //it is an integer
}
else
{
 //it is a float
}

That being said there's probably some stream manipulator you can use in c++ to do it for you, I don't know the standard libs all that well.
 

missile

Member
Learning Assembly code right now and I'm awful at it. Having a hell of a time trying to get a simple "if" statement working in it.
Once you got it, it never gets any easier. The reason you struggle is perhaps
you never implemented an 'if' statement with respect to any hardware. Even if
you write millions of lines of highlevel code (C, Java, ...) using 'if'
statements, you never really implemented any 'if' statement for the processor
to chew. Then who did it? The compiler did it. Once you got the hang of it,
'if' statements can be made really efficient using certain techniques in
conjunction with some processor specific registers. The knowledge of these
things may even lead you to eliminate some 'if' statements altogether (and'ing
is your friend), even in higher languages, which is a hot topic on modern
architectures.
 

Tantalus

Neo Member
Hey guys, question about C++ template programming. I'm making a simple 2D game using the SFML library, and have made a ContentManager class for loading and unloading content (textures, fonts, sounds) into and out of memory.

Right now I have one templated function for loading all the different types of resources into memory. They are written like this:

Code:
   template <> sf::Texture& Content::get<sf::Texture>(std::string fileName)
    {
        if(textures.find(fileName) == textures.end())
        {
            textures[fileName] = sf::Texture();
            textures[fileName].loadFromFile(texturePath + fileName);
        }
        return textures[fileName];
    }

    template <> sf::Font& Content::get<sf::Font>(std::string fileName)
    {
        if(fonts.find(fileName) == fonts.end())
        {
            fonts[fileName] = sf::Font();
            fonts[fileName].loadFromFile(fontPath + fileName);
        }
        return fonts[fileName];
    }

content.get<sf::Texture>("this-is-a-texture.png");

The only difference between loading a texture into memory and loading a font into memory is that they are (obviously) declared as different types, and stored in different variables.

So I'm wondering; could I declare textures[fileName] and fonts[fileName] as generic type "T" (which will be substituted in for sf::Texture or sf::Font)? And also, is there anyway I could make this into one templated function instead of using specialization for each data type?
 

Gartooth

Member
Hi I'm having a bit of trouble doing assembly language here, so if anyone is good at understanding MIPs and could help me I would be extremely grateful.

Premise: (from codingbat.com)

Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.

posNeg(1, -1, false) &#8594; true
posNeg(-1, 1, false) &#8594; true
posNeg(-4, -5, true) &#8594; true

I manged to easily make the following Java solution very quickly:

Code:
public boolean posNeg(int a, int b, boolean negative) {
  boolean j = false;
  
  if((a<0||b<0)&&(a>0||b>0) && negative ==false){
  j = true;
  }else if(negative == true && (a<0) && (b<0)){
  j=true;
  }
  return j;
}

My problem however is getting the relations to work in MIPs. When I have one int >0, and the other <0, with the boolean at "0" (false) then my program works.

My issues start to arise when I have a pair of integers that are either both positive or both negative. Positives always result in a false, while negatives normally do unless the boolean value is set to true.

What I originally tried to do, is isolate any positive integer pairs right away in order to return false. Therefore any remaining pairs should be negative, and if I check the two integers against each other, they should return a 1 if both are <0. I then check this 1 against the value of the boolean, and if both are 1 then it should work. (if boolean is 0 though, it returns false because double negatives are only allowed with "negative" set to true) Currently I keep failing one of the test cases though and I have no idea how it isn't going through. It is -4, -5, 1, 1 where (a) is -4, (b) is -5, the negative boolean is true, and thus it should return true.
Code:
posNeg:

	# $t0 is 0
	addi $t0, $0, 0
	#Check if int a is < 0, $t1 is 1 when a<0, $t1 is 0 when a>0
	slt $t1, $a0, $t0
	#Check if int b is < 0, $t2 is 1 when b<0, $t2 is 0 when b>0
	slt $t2, $a1, $t0
	#Check if int a is > 0
	slt $t3, $t0, $a0
	#check if int b is > 0
	slt $t4, $t0, $a1
	
	
	
	#Check value of neg bool, 1 when true, 0 when false			
	slt $t5, $t0, $a3
	
	#if both int a and int b are > 0, condition failed, doesn't matter value of neg bool
	blt $t0, $t3, else2
	j pass
	else2: blt $t0, $t4, else3
	
	pass:
	#Check if a and b are the same, because positives are out, that only leaves negatives
	seq $t6, $t1, $t2
	#Branch if a and b are negative(equals 1), and bool neg is 1 (true)
	beq $t6, $t5, if2
	#Gives 1 if t1 and t2 are different. (one is greater than 0, the other is less than)
	sne $t7, $t1, $t2
	#If there is a difference between a and b, and neg bool is false, then set to true
	blt $t5, $t7, if2
	
	#All other cases set to false
	addi $v0, $0, 0
	
	j target2
	if2: addi $v0, $0, 1
	j target2
	else3: addi $v0, $0, 0
	target2: jr $ra
 

arit

Member

Might not be optimal (many jumps), but should be a valid solution.
Pseudo instructions can be useful, but at least for me they make assembly rather difficult to read and I tend to overuse them (tbh. I first had to check what those s__ ops are for).

Code:
main:
  addi $t0, $0, number 0
  addi $t1, $0, number 1
  addi $t2, $0, 0:false / >0:true
 
  bltz $t0, t0neg    # t0 negative, so jump down
  bltz $t1, true # t0 is positive, so if t1 is negative return true
  j false            # both are positive -> return false

t0neg:               # t0 is negatve
  bgez $t1, true     # if t1 is positive return true
t0t1neg:                     # else check if flag is set
  bgez $t2, true     # both are negative and flag ist true -> return true
  j false            # both are negative but flag is false -> return false

false:
  j exit

true:
  j exit

EDIT:
- Since $0 is always 0 you do not have to load 0 to $t0.
- you are making it way to complicated by using registers to save so many boolean values.

EDIT2: my mistake in understanding the true/false flag, be back in a few minutes
 

Gartooth

Member

Yeah I overcomplicated it because I was trying to keep track of every single calculation. (when really it is only a bunch of 0's and 1's)

Also I haven't seen anything like it yet, but just to make sure, you set register $t2 to represent the boolean right?

(in my program I take parameters of a0 (int a), a1 (int b), and a2 (the boolean) and then set them to different t registers)

Edit: Hahaha, I can't believe I didn't figure it out. Got my program working, problem was that I was using $a3 as the parameter for the boolean instead of register $a2. Thanks for the help!

Here is the finished code:

Code:
posNeg:

	# $t0 is 0
	addi $t0, $0, 0
	#Check if int a is < 0, $t1 is 1 when a<0, $t1 is 0 when a>0
	slt $t1, $a0, $t0
	#Check if int b is < 0, $t2 is 1 when b<0, $t2 is 0 when b>0
	slt $t2, $a1, $t0
	#Check if int a is > 0
	slt $t3, $t0, $a0
	#check if int b is > 0
	slt $t4, $t0, $a1
	
	
	
	#Check value of neg bool, 1 when true, 0 when false			
	slt $t5, $t0, $a2
	
	#if both int a and int b are > 0, condition failed, doesn't matter value of neg bool
	blt $t0, $t3, else2
	j pass
	else2: blt $t0, $t4, else3
	
	pass:
	#Check if a and b are the same, because positives are out, that only leaves negatives
	seq $t6, $t1, $t2
	#Branch if a and b are negative(equals 1), and bool neg is 1 (true)
	beq $t6, $t5, if2
	#Gives 1 if t1 and t2 are different. (one is greater than 0, the other is less than)
	sne $t7, $t1, $t2
	#If there is a difference between a and b, and neg bool is false, then set to true
	blt $t5, $t7, if2
	
	#All other cases set to false
	addi $v0, $0, 0
	
	j target2
	if2: addi $v0, $0, 1
	j target2
	else3: addi $v0, $0, 0
	target2: jr $ra

For now I'm taking it while it works just for the sake of the assignment, but next time I'm definitely going to try and make it more efficient in terms of the amount of registers I use.
 

Wichu

Member
So I'm wondering; could I declare textures[fileName] and fonts[fileName] as generic type "T" (which will be substituted in for sf::Texture or sf::Font)? And also, is there anyway I could make this into one templated function instead of using specialization for each data type?

You can make ContentManager a templated class, and have separate instances of ContentManager for each content type.

Then, if you want a single object to manage everything, you can wrap them in a new class. For example (renaming the old ContentManager to a templated class ContentTypeManager):

Code:
class ContentManager {
ContentTypeManager<Font> fontManager;
ContentTypeManager<Texture> textureManager;
Font& getFont(string name) { return fontManager.get(name); }
Texture& getTexture(string name) { return textureManager.get(name); }
}
 
What is this? I'm 99% sure I'm going to be taking it next semester and haven't a clue what it is.

Boolean Logic, combinatorics, probability, set theory, graph theory.

One of the more difficult CS classes to be quite honest (at least for me) It's far different than the normal algebra/calculus you may have taken.
 

squidyj

Member
What is this? I'm 99% sure I'm going to be taking it next semester and haven't a clue what it is.

Usually it's an introduction to proof writing in addition to things more directly related to discrete math, like sets.

It's where I learned about induction which proved (pun intended) to be kind of a big deal later on. All sorts of proof strategies, good times.
 

Ravager61

Member
Boolean Logic, combinatorics, probability, set theory, graph theory.

One of the more difficult CS classes to be quite honest (at least for me) It's far different than the normal algebra/calculus you may have taken.

Discreet math kicked my ass as a freshman. Worst grade I got in all of college. Mostly because I was still in high school mode and didn't buckle down like I could have.
 

sono

Gold Member
First I learned C, then I learned Java, then I learned C++ (with some pain). Now they are telling me to learn C#.
 
Discreet math kicked my ass as a freshman. Worst grade I got in all of college. Mostly because I was still in high school mode and didn't buckle down like I could have.

Same. Although I didn't take it freshman year, I have to retake it this semester. Boo.

I actually really liked Discrete Math when I took it. Automata has been worse.

I enjoy it now that I am starting to see patterns and how it actually shapes math and computing. Like Pascal's Triangle is a goddamned work of art.
 

usea

Member
Signature of a method I'm debugging:
private void UnifiedIpcCollectorColumnUploadDataHandler()

I'm not opposed to long names when they're descriptive. A descriptive name is a billion times better than an abbreviation or made up word (same for variables). Hardware configuration should be named hardwareConfiguration, not hdwrCfg.

However, in this case it's not even descriptive. Unified is meaningless. Handler is stupid since the method doesn't even handle data. Even if it did handle data, Data would be redundant; what else are you handling?

What this method really does: selects some records from a database that have a certain condition, creates an uploader object, and calls upload with the list of records from the database. If you're not going to split that up into multiple methods, at least call it what it is: SelectAndUploadColumns()

Also I love how it makes a new upload class every time instead of just depending on one in its constructor. Even though I specifically said about a hundred times to stop newing up classes in the middle of methods since you can't test it.

/rant

The problem with the method: the list is populated lazily via yield return and not rendered before uploading. The uploader is asynchronous; it gets the records and holds onto them until another component is ready to upload. By the time the uploader gets around to uploading them, sometimes the collection is suddenly empty. This is because every time you iterate over a lazy collection it re-evaluates. In this case that means even counting the collection will perform the database query again. Due to another bug, the condition on the records stops being true later on, and there are no more records matching the select.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I'm not opposed to long names when they're descriptive. A descriptive name is a billion times better than an abbreviation or made up word (same for variables). Hardware configuration should be named hardwareConfiguration, not hdwrCfg.

Agreed. I hate contractions, at some point it just stops making sense and looks like someone rolled their face on their keyboard. It relies too much on assuming others would contract a word the same way the original programmer would.

Although I'll have to admit my "private var pauseMenuButtonTextOffsetXPercent:float" isn't exactly eyepleasing.
 

arit

Member
Yeah I overcomplicated it because I was trying to keep track of every single calculation. (when really it is only a bunch of 0's and 1's)

Also I haven't seen anything like it yet, but just to make sure, you set register $t2 to represent the boolean right?

(in my program I take parameters of a0 (int a), a1 (int b), and a2 (the boolean) and then set them to different t registers)

Edit: Hahaha, I can't believe I didn't figure it out. Got my program working, problem was that I was using $a3 as the parameter for the boolean instead of register $a2. Thanks for the help!

For now I'm taking it while it works just for the sake of the assignment, but next time I'm definitely going to try and make it more efficient in terms of the amount of registers I use.

At least in my (university) experience, in assembly it helps even more than in higher languages to just think about the problems (but seeing your first post I assume you did so).
(Well and understand them...)

After failing in reading the assignment correctly, my second attempt was just writing down a truth table (since it is a rather small logic problem) and unless I misunderstood it a second time, it can only return true if the inputs are -/+/0, +/-/0 and -/-/1.

So one bulk of false returns can be simply checked by looking at the boolean value, if true just check if a0 and a1 both are negative and then return true if so, else false.

Since the flag does not matter after that anymore, we only have to check for +- or -+, which makes it a lot simpler and my final (semi optimized) code would be:

Code:
posneg:
  add $t0, $a0, $0    # int 1
  add $t1, $a1, $0    # int 2
  add $t2, $a2, $0    # boolean, false if <=0
  add $v0, $0, $0     # init return value with false
  
  blez $t2, notSet   # check if flag is not set
                     # flag is set -> both have to be negative
  bgtz $t0, exit     # $t0 is positive -> exit(false)
  slt $v0, $t1, $0   # $t0 is negative, so set v0 to 1 if $t1 < 0
  j exit             # 

notSet:		     # can only return true if +- or -+
  bltz $t0, t0neg    # t0 negative, so jump down
  slt $v0, $t1, $0   # t0 is positive, so set v0 true if t1 < 0
  j exit             # exit

t0neg:               # t0 is negatve
  slt $v0, $0, $t1   # so set v0 true if 0 < t1
  j exit             # 

exit:
  jr $ra

Just three conditional statements and readable code (of course I could just be in the zone of understanding my code now and not understand it tomorrow...).

And of course registers are there to be used and using more can be beneficial to prevent stalls and raise readability. So if you think, using the registers the way u did helped to solve the problem more easy, do so, it is just an assignment and not some critical code in safety related hardware. Boundaries will be introduced soon enough via register conventions.

Being a bit offtopic, I think it is sad that many courses do not start with assembly from the beginning. It makes understanding pointers in c so much easier, since many coming from java/c# seem to struggle with them.
 

upandaway

Member
I feel like in most states of mind I'd type "hardwareConfiguration" faster than "hdwrCfg" or any other silly shortening of it. Anything you'd gain from a short name is nullified if it's like that.
 
First I learned C, then I learned Java, then I learned C++ (with some pain). Now they are telling me to learn C#.

It's very common if you are in CS. I've worked in a lot, a lot of languages. I would never say I've learned them though.

While working I've done some medium/big projects in PHP, Ruby, Python, PHP, C++, Java, Java ME and C#. While studying I had to do some small/medium projects on Scheme, Haskell, Prolog and C. Also, lot's of other small/mini projects but they don't count.

In the end, switching from one language to another is not hard. The hard part is being good at the new language because it forces you to learn new paradigms and forget the old ones. In that sense, C++, Java and C# are not much different.

I feel like in most states of mind I'd type "hardwareConfiguration" faster than "hdwrCfg" or any other silly shortening of it. Anything you'd gain from a short name is nullified if it's like that.

It's never worth it. Source code is for humans not for robots and in that sense the more legible the better.
 
Boolean Logic, combinatorics, probability, set theory, graph theory.

One of the more difficult CS classes to be quite honest (at least for me) It's far different than the normal algebra/calculus you may have taken.

Discrete Mathematics is an absolute breeze compared to Abstract Algebra. (Abstract is basically Discrete 2). Thank your lucky stars that only Discrete is required. I made the mistake of taking Abstract. Got my A, but holy crap. The main problem with Discrete/Abstract is that basically all mathematics prior to university prepare you for the continuous domain (Calculus and the like). The concepts in discrete are completely new, and most won't have even seen a proof before it. Then all of a sudden it's basically pure proofs. I have one last class I'm in that has proofs (Logic 2) and I'll never look at a proof again afterwards. I don't find them too bad in general but it's amazing how much works goes into proving something you already know is true.
 

usea

Member
Schools will do proofs in different classes from each other. At my school, the main proof classes in the math dept were discrete and linear. Abstract didn't do any proofs afaik.

The CS department offered their own discrete class which didn't do proofs, but covered the same material. If you were getting a math minor, the CS version wouldn't count toward it. You also couldn't take linear without the math version of discrete (because you'd be way behind on doing proofs).

Every school is different.
 
Schools will do proofs in different classes from each other. At my school, the main proof classes in the math dept were discrete and linear. Abstract didn't do any proofs afaik.

The CS department offered their own discrete class which didn't do proofs, but covered the same material. If you were getting a math minor, the CS version wouldn't count toward it. You also couldn't take linear without the math version of discrete (because you'd be way behind on doing proofs).

Every school is different.

How in the hell would you do abstract without the proofs? It would be such a lightweight course if all you had to do was remember basic ring and field theory. I'm a little befuddled on that one.

Also, my discrete class was very heavy on proofs. Proofs as related to Abstract Algebra topics were way more complex than Discrete Math proofs. My school didn't have separate Math/CS math classes. If you're taking CS here, you take the math major versions of the math courses you need. I hadn't considered that schools would "dumb it down" so to speak (at least that's the assumption) for CS majors.
 

barnone

Member
Why is it so difficult to install QT 5 on my Windows 7 Visual Studio 2010? I go through all of these console commands, environment variable settings, just to find out there was no make file for NMAKE to use in my download? What the hell am I doing lmao?

I must be stupid and not seeing the installer, though this complicated process through the command prompt is quite interesting and I'd love to learn more about it..
 
Why is it so difficult to install QT 5 on my Windows 7 Visual Studio 2010? I go through all of these console commands, environment variable settings, just to find out there was no make file for NMAKE to use in my download? What the hell am I doing lmao??

That's windows for you; The most developer unfriendly OS ever. Why don't you use QT's own IDE? It's pretty good.
 

Randdalf

Member
I'm writing a few simple custom memory allocators in C++ and I'm wondering what the best way of allocating the memory to store the free list in a pool allocator is. The book I'm reading suggest using the memory pool itself to store the linked list but I'm not exactly sure how that would work. I've already written a stack allocator and a double-ended stack allocator, but I'm thinking it would be useful to add some debugging features such as the line the memory was allocated at, or storing the size of each block allocated (I might try some of the ideas here: http://www.gamasutra.com/view/feature/131380/play_by_play_effective_memory_.php?print=1). Theoretically, these memory allocators are meant to be used in a game engine (albeit a simple one) I plan to write.
 

leroidys

Member
OK gaf, I'm in need of some SERIOUS help.

I'm taking a concurrency class, which is pretty cool, but the teacher insists that the projects are to be in scheme, despite the fact that everyone in the class insists that they don't know scheme, and that every single example in both of our 2 books is either in C or python. He also has spent 0 lectures teaching us any scheme syntax, or how to create threads in scheme, etc., and scheme help/documentation online is terrible.

Anyway, I had to take an unthreaded program that he wrote that creates a "planet" when you click, and then has a checkbox to make the plants orbit each other or stop orbiting.

We were supposed to make a version that puts all the calculation into one thread. I think he wants us to use thread-suspend and thread-resume, but I couldn't get these to work. Here's what I did using semaphores :

Code:
#lang racket

(require racket/gui)
(require racket/block)

;; Small 2d vector library for the Newtonian physics
(define (x v) (vector-ref v 0))
(define (y v) (vector-ref v 1))
(define (x! v value) (vector-set! v 0 value))
(define (y! v value) (vector-set! v 1 value))
(define (v* v value) (vector-map (lambda (x) (* x value)) v))
(define (v+ v w) (vector-map + v w))
(define (v- v w) (vector-map - v w))
(define (v-zero! v) (vector-map! (lambda (x) 0) v))
(define (v-dot v w) (let ((vw (vector-map * v w))) (+ (x vw) (y vw))))
(define (v-mag v) (sqrt (v-dot v v)))
(define sem (make-semaphore))


;; Planet object
(define planet%
  (class object%
    (public m p v calculate-force move draw)
    (init-field (mass 1)
                (position (vector 0 0 ))
                (velocity (vector 0 0 ))
                (force (vector 0 0 )))
    (define (m) mass)
    (define (p) position)
    (define (v) velocity)
    ;; Use Newton's law of gravitation.
    ;; I assume the gravitational constant is one
    (define (calculate-force planet-list)
      (v-zero! force)
      (for-each (lambda (other-planet)
                  (when (not (equal? this other-planet))
                    (let* ((direction (v- (send other-planet p) position))
                           (dist (max 1 (v-mag direction)))
                           (other-mass (send other-planet m))
                           (new-force (v* direction (/ (* mass other-mass) (* dist dist))))
                          )
                      (vector-map! + force new-force))))
                planet-list)
      )
    ;; Simple Euler integration of acceleration and velocity
    (define (move) 
      (let ((acc (v* force (/ 1.0 mass))))
        (vector-map! + velocity acc)
        (vector-map! + position velocity)))
    ;; Draw a circle 
    (define (draw dc) 
      (send dc set-brush brush)
      (send dc set-pen pen)
      (send dc draw-ellipse (x position) (y position) radius radius ))
    ;; Initialize to random velocity, mass, and color
    (x! velocity (random))
    (y! velocity (random))
    (set! mass (+ 1 (* 10 (random))))
    (define radius (* 5 (sqrt mass)))
    (define color 
      (let* ((r (random))
             (b (real->floating-point-bytes r 4)))
        (make-object color% (bytes-ref b 0) (bytes-ref b 1) (bytes-ref b 2) )))
    (define brush (make-object brush% color))
    (define pen (make-object pen% color))
    ;; Don't forget the super-new!
    (super-new)
    ))
;; Abstract the list-handling for a list of planets
(define planet-list%
  (class object%
    (public add-planet calculate-force move draw)
    (init-field (planets '()))
    (define (add-planet planet)
      (set! planets (cons planet planets)))
    (define (calculate-force)
      (for-each (lambda (planet)
                  (send planet calculate-force planets))
                planets))
    (define (move)
      (for-each (lambda (planet)
                  (send planet move))
                planets))
    (define (draw dc)
      (for-each (lambda (planet)
                  (send planet draw dc))
                planets))
    (super-new)
    )
  )
(define planet-list (new planet-list%))
    
;; The GUI
(define frame (new frame% 
                   (label "Planets")
                   (min-width 120)
                   (min-height 80)
                   ))
(send frame create-status-line)
(send frame show #t)

(define h-panel
  (new horizontal-panel%
       (parent frame)
       (stretchable-height #f)
       (style '(border))
       (border 2)))

(define run-checkbox
  (new check-box%
       (parent h-panel)
       (label "Run animation")
       (callback
        (lambda (button event)
          (cond
            [(send run-checkbox get-value)(semaphore-post sem)]
            [(not (send run-checkbox get-value)) (semaphore-wait sem)]
          
          

       )))
        ))

(define my-canvas%
  (class canvas%
    (override on-paint on-event)
    
    (define (on-paint)
      (let ((dc (send this get-dc))
            (w (send this get-width))
            (h (send this get-height)))
        (send dc clear)
        (send planet-list draw dc)
        ))
    (define (on-event event)
      (when (send event button-down?)
        (let ((x (send event get-x))
              (y (send event get-y)))
          (send frame set-status-text (format "Mouse at ~a ~a" x y))
          (send planet-list add-planet (new planet% (position (vector x y))))
          
          (send this refresh)))
      )
    (super-new)
    (send (send this get-dc) set-background (make-object color% 8 8 64))
    ))

(define canvas
  (new my-canvas%
       (parent frame)
       (style '(border))
       (min-width 640)
       (min-height 480)))

;; planet animator
(define thread-a (lambda ()
(let loop ()
  (semaphore-wait sem)
  (sleep/yield .1)
    (send planet-list calculate-force)
    (send planet-list move)
    (send canvas refresh)
    (semaphore-post sem)

  (loop))))

; this creates the thread-a and starts the program
(thread thread-a)

The next part of the assignment, we are supposed to make a thread for every single planet, which is started and stopped when the checkbox is clicked.

tl;dr threading in scheme HALP
 

nOoblet16

Member
Guys, I've got a question, I'm not sure if this is the right place to ask but since it's related to programming I think it's relevant to this thread.

I don't really follow the tablet PC scene so I've got no idea about what's going on in it, so I'd like to know if there are any tabs out there that I can use for programming (prominently Java and C) because I don't really want to be carrying my laptop to university with charger every single day as it's a bit heavy.
 
Top Bottom