• 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

I figured this may be the best place to ask this question, but I recently setup a Raspberry Pi to act as a wireless access point, and it works great... when it works. The problem seems to be that if I reboot it or disconnect it form power, sometimes the DHCP server fails to launch and all of my connected devices can't acquire an IP address and assign themselves a 169.x.x.x IP address that results in them not connecting to the internet. Through some hunting, it sounds like the DHCP server is starting before the Raspberry Pi, through hostpad, hasn't yet assigned itself an IP address, so DHCP is starting too early or too fast. I would think this is easy to solve, but since this happens randomly on reboots, sometimes it is too fast and sometimes it is slow enough to work correctly, I can't figure out how to resolve the issue.

I have DHCP setup to start on boot via "sudo update-rc.d isc-dhcp-server enable", but should I disable that and just execute a script that waits a few seconds and then starts the DHCP server? Do you think that may be my problem?
 

upandaway

Member
I've got a small question:

They told us to implement a UML chart for a project we'll make later in the semester (in C++), and I want to use a compare functions to sort a list in multiple ways. That means those functions can't be members of a class, right? In that case where am I supposed to put them (where in the code and more importantly where in the UML)?
Is there an OOP way to do this? In Java I'd use a comparator but in C I used function typedefs to do this before...
 

Martal

Neo Member
I have a very simple Python (Python 3, following the book Think Pyton 2nd Ed) question. I want to make an exercise for myself. I want to create a program that generates x random numerical strings of y length. Usually its 10 strings, 3 digits each. I store these in a list.

I want the program to display the string[n], then string[n+1] and then ask me for string[n] again and check if it is correct. This would then be repeated for all the 10 strings.

I can manage most of this, but my main obstacle is that its supposed to be a memory exercise. The only way I know of display text is using print(). Problem is , of course, that the first string is still displayed when the prompt the checking it appears.

I have tried looking online but the suggested answer (end="\r") hasnt yielded results.

Here is my code so far:

def get_strings(n):

strings = []

for i in range(1, 11):

strings.append(''.join(random.choice(string.digits) for i in range(n)))

return strings

strings = get_strings(3)


print("Exercise")
print(strings[0])
print(strings[1], end="\r")

answer = input("What was the first string? \n")

print(answer)

if answer == strings[0]:
True


This is just a basic test. I would use for loops and such in the actual program.
Also, I have only tested this in Pythons own interpreter (only way I know how). Might that have something to do with it?
 
Write a die-rolling/gambling game with the following requirements:

1.The user begins with 100 dollars.
2.User enters an amount to bet. If the input is negative or if there's not enough money, prompt the user for another input.
3.The user wins double the bet if the number rolled is 5 or 6.
4.The user keeps playing until he's broke.

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
 
int main () 
{
    srand(time(NULL));
    int money = 100, bet;
 
    while (money > 0) {
 
        cout << "You currently have " << money << " dollars. \n";
        bet = getBet(money);
 
        if (wonTheBet()) {
            int amount = bet * 2;
            money += amount;
            cout << "Congratulations! You just won " << amount << " dollars!\n\n";
        }   
 
        else {
            money -= bet;
            cout << "Ah, you lost! Try again! \n\n";
        }
 
    }
 
    cout << "You are out of money!\n";
    return 0;
}

im supposed to complete this game by completing the getBet and wonTheBet function and i have no idea where to begin

we went from nested loops to this nightmare and i dont get any of it

any ideas?

this is due tonight ><
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Basically you need to write two functions somewhere so the main function can call them.

Did they explain how this game works?

getBet should ask the user to bet some money. It takes in money as a parameter because this is what the user has to work with.

wonTheBet I'm less certain. I have no doubt you received some instructions on how to go about winning the bet.

EDIT: Don't leave programming assignments to the last minute, seriously. Unless you're a genius you need to start that shit early.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Have you covered functions? Do you know about return types and parameters?
 
yes, we went through all of that and i get the idea of functions, i just have trouble approaching a solution like in this problem that will make the program work

i have no idea what my prof expects me to fill these functions with, we never made a game like this in class

the only instructions are the ones listed, i guess he wants us to get creative? idk
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Think about what the function expects and then write it down in plain english (pseudo code). Then translate that english into actual code.

Code:
bet = getBet(money);

So we know getBet returns an int, because 'bet' is expecting it, and it takes in another int, because 'money' was passed to it.

Code:
int getBet(int dineros)

Requirement 2 tells you how getBet() works.

Code:
int getBet(int dineros) {
   // User enters an amount to bet. If the input is negative or if there's not enough money, prompt the user for another input.
}

Then you break it down into statements
Code:
int getBet(int dineros) {
    // Ask user for amount to bet.
    // Save it somewhere
    // If the input is negative or if there's not enough money
      // Prompt the user for another input.
      // This will require a loop until the user inputs a valid number
    // return the user's bet
}
That should look like one of your previous assignments, so refer back to them to see what you should do.
 

upandaway

Member
I've got a small question:

They told us to implement a UML chart for a project we'll make later in the semester (in C++), and I want to use a compare functions to sort a list in multiple ways. That means those functions can't be members of a class, right? In that case where am I supposed to put them (where in the code and more importantly where in the UML)?
Is there an OOP way to do this? In Java I'd use a comparator but in C I used function typedefs to do this before...
Found out about the idea of storing the function in a designated struct with the () operator, that should be good enough
 

cyborg009

Banned
Never know unless you ask :)

Well here I go

So I'm trying to do the tower of hanoi from this code

Code:
class Hanoi { 
    public static void main (String args[]) {
	 // This is the number of disks, say 5
      int N = 5; 
             
      // This says: Move 5 disks from peg 1 to peg 3, using peg 2
      hanoi(N, 3, 1, 2);
    } 
     
    static void hanoi(int n, int t, int f, int u) {
        if (n == 0) return;
        else {
            hanoi(n-1, u, f, t); 
            System.out.println("move disk " + n +
                               " from " + f + 
                               " to " + t);
            hanoi(n-1, t, u, f);
        } 
    } 
}


to this in assembly mips:

Code:
hanoi:  

#store values in stack pointer
addi $sp,$sp,-20
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
sw $ra, 16($sp)
							
bne $t0, 0 else
#return
jr $ra							 

else:

#call hanoi 
#rearrage parameters
add $a0, $a0, -1

add $t1, $a1, $zero

add $a1, $a3, $zero
add $a2, $a2, $zero
add $a3, $t1, $zero


	jal hanoi

lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
lw $ra, 16($sp)

addi $sp, $sp, 20


addi $sp,$sp,-20

sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
sw $ra, 16($sp)

add $a0, $a0, -1
add $a1, $a1, $zero

add $t1, $a2, $zero

add $a2, $a3, $zero
add $a3, $t1, $zero


	jal hanoi
 lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
lw $ra, 16($sp)
 addi $sp,$sp, 20

But I see to be getting address out of range.
 
Well here I go

So I'm trying to do the tower of hanoi from this code

Code:
class Hanoi { 
    public static void main (String args[]) {
	 // This is the number of disks, say 5
      int N = 5; 
             
      // This says: Move 5 disks from peg 1 to peg 3, using peg 2
      hanoi(N, 3, 1, 2);
    } 
     
    static void hanoi(int n, int t, int f, int u) {
        if (n == 0) return;
        else {
            hanoi(n-1, u, f, t); 
            System.out.println("move disk " + n +
                               " from " + f + 
                               " to " + t);
            hanoi(n-1, t, u, f);
        } 
    } 
}


to this in assembly mips:

Code:
hanoi:  

#store values in stack pointer
addi $sp,$sp,-20
sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
sw $ra, 16($sp)
							
bne $t0, 0 else
#return
jr $ra							 

else:

#call hanoi 
#rearrage parameters
add $a0, $a0, -1

add $t1, $a1, $zero

add $a1, $a3, $zero
add $a2, $a2, $zero
add $a3, $t1, $zero


	jal hanoi

lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
lw $ra, 16($sp)

addi $sp, $sp, 20


addi $sp,$sp,-20

sw $a0, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
sw $a3, 12($sp)
sw $ra, 16($sp)

add $a0, $a0, -1
add $a1, $a1, $zero

add $t1, $a2, $zero

add $a2, $a3, $zero
add $a3, $t1, $zero


	jal hanoi
 lw $a0, 0($sp)
lw $a1, 4($sp)
lw $a2, 8($sp)
lw $a3, 12($sp)
lw $ra, 16($sp)
 addi $sp,$sp, 20

But I see to be getting address out of range.

Ouch, mips assembly. I assumed x86 / x64. I'm out, sorry :)
 

wolfmat

Confirmed Asshole
I have a very simple Python (Python 3, following the book Think Pyton 2nd Ed) question. I want to make an exercise for myself. I want to create a program that generates x random numerical strings of y length. Usually its 10 strings, 3 digits each. I store these in a list.

I want the program to display the string[n], then string[n+1] and then ask me for string[n] again and check if it is correct. This would then be repeated for all the 10 strings.

I can manage most of this, but my main obstacle is that its supposed to be a memory exercise. The only way I know of display text is using print(). Problem is , of course, that the first string is still displayed when the prompt the checking it appears.

I have tried looking online but the suggested answer (end="\r") hasnt yielded results.

Here is my code so far:




This is just a basic test. I would use for loops and such in the actual program.
Also, I have only tested this in Pythons own interpreter (only way I know how). Might that have something to do with it?

You need escape sequences.
Escape sequences are an ancient thing that instructs a terminal to do uncommon things, like "now write with red background", or "remember this position" to then "jump back to the remembered position".

Here's an extensive list of VT100 escape sequences. VT100 is pretty much the reference for escape sequences. Most terminals support a significant amount of VT100 sequences.

You need a setup and a jump flow to get this right though because you want to ignore standard terminal behavior. To figure that shit out, you want to think carefully about what's supposed to happen.

I recommend clearing the screen before doing anything. Then conciously pick a line where you want to print instructions, and a line where you want to print the value it's about.

To demonstrate, I'm going to list a program that
1. Clears the screen
2. Increments a counter
3. Prints some text on line 1
4. Prints a placeholder character on line 2
5. Prints some text on line 3
6. Jumps to line 2 and prints the counter value over the placeholder
7. sleeps for a second

But there's one important thing to note before anything else: The escape character is a special character -- character 27 (dec) in the ASCII table, hex value: 1B. You can produce it with "\x1b" in a string inside your scripts.
(Or, on Linux, press CTRL-V and then Esc)

Here's the code:
Code:
import time
counter = 0
while True:
    print("\x1b[2J") # Clear screen
    counter += 1
    print("\x1b[HSeconds since start of program:") # Jump to upper left and spit out some text
    print("?") # This character is on line 2 and will be overwritten later with the counter value
    print("Time flies by so fast...") # This text is on line 3
    print("\x1b[2;0f%d" % counter) # We jump up to line 2 and overwrite the content with the counter value
    time.sleep(1)

Now, to get your program right, apply the learned oldschool magic.

I hope you're enjoying your time travel. Escape sequences are fun!

Bonus fun: "Clear screen" is a totally straight-forward operation. After executing my example, try scrolling up for a bit. (Yes, that's the way it works! Seriously! Was like this for decades!)
 
So I'm trying to get this code to work but I'm having trouble with the if else, can anyone help?

Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
	int num1, num2, num3, userNum, minNum, maxNum, rNum1, rNum2, reply;
	string operate;
	string addition;
	string subtraction;

    minNum = 0;
    maxNum = 100;
	reply = 'addition' || 'subtraction';
    rNum1 = (minNum) + (rand() % (maxNum));
    rNum2 = (minNum) + (rand() % (maxNum));

	cout << "Would you like to do a addition or subtraction problem?";
	cin >> operate;
	num1 = rNum1;
	num2 = rNum2;

	if (operate == addition)
    {
        num3 = num1 + num2;
        cout << num1;
        cout << "+ " << num2;
        cout << "-------------\n";
        cout << num3;
        cout << setprecision(5);
        cout << "What answer did you get?";
        cin >> userNum;
            if (num3 == userNum)
		{
			cout << "Congratulations! You got it right.\n";
		}
            else

			cout << "Sorry but the answer was " << num3 << " Try again" << endl;
    }
	else  (operate == subtraction)
    {
        num3 = num1 - num2;
        cout << num1;
        cout << "- " << num2;
        cout << "-------------\n";
        cout << num3
        cout << setprecision(5);
        cout << "What answer did you get?";
        cin >> userNum;
            if (num3 == userNum)
		{
			cout << "Congratulations! You got it right.\n";
		}
            else

			cout << "Sorry but the answer was " << num3 << " Try again" << endl;
    }
    else if
        cout << "Incorrect Reply"
	return 0;
}
 

X05

Upside, inside out he's livin la vida loca, He'll push and pull you down, livin la vida loca
So I'm trying to get this code to work but I'm having trouble with the if else, can anyone help?

Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
	int num1, num2, num3, userNum, minNum, maxNum, rNum1, rNum2, reply;
	string operate;
	string addition;
	string subtraction;

    minNum = 0;
    maxNum = 100;
	reply = 'addition' || 'subtraction';
    rNum1 = (minNum) + (rand() % (maxNum));
    rNum2 = (minNum) + (rand() % (maxNum));

	cout << "Would you like to do a addition or subtraction problem?";
	cin >> operate;
	num1 = rNum1;
	num2 = rNum2;

	if (operate == addition)
    {
        num3 = num1 + num2;
        cout << num1;
        cout << "+ " << num2;
        cout << "-------------\n";
        cout << num3;
        cout << setprecision(5);
        cout << "What answer did you get?";
        cin >> userNum;
            if (num3 == userNum)
		{
			cout << "Congratulations! You got it right.\n";
		}
            else

			cout << "Sorry but the answer was " << num3 << " Try again" << endl;
    }
	else  (operate == subtraction)
    {
        num3 = num1 - num2;
        cout << num1;
        cout << "- " << num2;
        cout << "-------------\n";
        cout << num3
        cout << setprecision(5);
        cout << "What answer did you get?";
        cin >> userNum;
            if (num3 == userNum)
		{
			cout << "Congratulations! You got it right.\n";
		}
            else

			cout << "Sorry but the answer was " << num3 << " Try again" << endl;
    }
    else if
        cout << "Incorrect Reply"
	return 0;
}
"if else" blocks has this format:
Code:
if (condition) {
    // code to execute
}
else if (anotherCondition) {
    // code to execute
}
(you can have several "else if (condition)" here)
else {
    // code to execute
}
Does your code follow that?

Also, there are a couple of errors in the code (missing semicolons, and the fact you are not correctly checking the input), so reread it carefully :)
 

Martal

Neo Member
You need escape sequences.
Escape sequences are an ancient thing that instructs a terminal to do uncommon things, like "now write with red background", or "remember this position" to then "jump back to the remembered position".

Here's an extensive list of VT100 escape sequences. VT100 is pretty much the reference for escape sequences. Most terminals support a significant amount of VT100 sequences.

You need a setup and a jump flow to get this right though because you want to ignore standard terminal behavior. To figure that shit out, you want to think carefully about what's supposed to happen.

I recommend clearing the screen before doing anything. Then conciously pick a line where you want to print instructions, and a line where you want to print the value it's about.

To demonstrate, I'm going to list a program that
1. Clears the screen
2. Increments a counter
3. Prints some text on line 1
4. Prints a placeholder character on line 2
5. Prints some text on line 3
6. Jumps to line 2 and prints the counter value over the placeholder
7. sleeps for a second

But there's one important thing to note before anything else: The escape character is a special character -- character 27 (dec) in the ASCII table, hex value: 1B. You can produce it with "\x1b" in a string inside your scripts.
(Or, on Linux, press CTRL-V and then Esc)

Here's the code:
Code:
import time
counter = 0
while True:
    print("\x1b[2J") # Clear screen
    counter += 1
    print("\x1b[HSeconds since start of program:") # Jump to upper left and spit out some text
    print("?") # This character is on line 2 and will be overwritten later with the counter value
    print("Time flies by so fast...") # This text is on line 3
    print("\x1b[2;0f%d" % counter) # We jump up to line 2 and overwrite the content with the counter value
    time.sleep(1)

Now, to get your program right, apply the learned oldschool magic.

I hope you're enjoying your time travel. Escape sequences are fun!

Bonus fun: "Clear screen" is a totally straight-forward operation. After executing my example, try scrolling up for a bit. (Yes, that's the way it works! Seriously! Was like this for decades!)

Thank you so much for this! Im very excited to try it out. Also Im pretty happy that it looks pretty complicated. Makes me feel better about not being able to figure it out myself.

EDIT: One more thing actually- I tried this now in the python interpreter and the command line, but its still printing new lines :/ Is there anything I might be doing wrong?
 

wolfmat

Confirmed Asshole
Thank you so much for this! Im very excited to try it out. Also Im pretty happy that it looks pretty complicated. Makes me feel better about not being able to figure it out myself.

EDIT: One more thing actually- I tried this now in the python interpreter and the command line, but its still printing new lines :/ Is there anything I might be doing wrong?

Are you on Windows? Which Windows?
 
You need escape sequences.
Escape sequences are an ancient thing that instructs a terminal to do uncommon things, like "now write with red background", or "remember this position" to then "jump back to the remembered position".

Here's an extensive list of VT100 escape sequences. VT100 is pretty much the reference for escape sequences. Most terminals support a significant amount of VT100 sequences.

You need a setup and a jump flow to get this right though because you want to ignore standard terminal behavior. To figure that shit out, you want to think carefully about what's supposed to happen.

I recommend clearing the screen before doing anything. Then conciously pick a line where you want to print instructions, and a line where you want to print the value it's about.

To demonstrate, I'm going to list a program that
1. Clears the screen
2. Increments a counter
3. Prints some text on line 1
4. Prints a placeholder character on line 2
5. Prints some text on line 3
6. Jumps to line 2 and prints the counter value over the placeholder
7. sleeps for a second

But there's one important thing to note before anything else: The escape character is a special character -- character 27 (dec) in the ASCII table, hex value: 1B. You can produce it with "\x1b" in a string inside your scripts.
(Or, on Linux, press CTRL-V and then Esc)

Here's the code:
Code:
import time
counter = 0
while True:
    print("\x1b[2J") # Clear screen
    counter += 1
    print("\x1b[HSeconds since start of program:") # Jump to upper left and spit out some text
    print("?") # This character is on line 2 and will be overwritten later with the counter value
    print("Time flies by so fast...") # This text is on line 3
    print("\x1b[2;0f%d" % counter) # We jump up to line 2 and overwrite the content with the counter value
    time.sleep(1)

Now, to get your program right, apply the learned oldschool magic.

I hope you're enjoying your time travel. Escape sequences are fun!

Bonus fun: "Clear screen" is a totally straight-forward operation. After executing my example, try scrolling up for a bit. (Yes, that's the way it works! Seriously! Was like this for decades!)

Stupid question: Why not:

Code:
def clear_screen():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

def beginning_of_line():
    sys.stdout.write('\r')
 

wolfmat

Confirmed Asshole
Stupid question: Why not:

Code:
def clear_screen():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

def beginning_of_line():
    sys.stdout.write('\r')

print() implicits a line feed, so Martal needs to resort to sys.stdout.write() instead of print(), and either has to flush() manually or reload stdout unbuffered because stdout buffers until flush, of course.

It's totally okay of course, but one can't arbitrarily jump around on the screen that way.

Example (unbuffered stdout):
Code:
import time
import sys
import os
counter = 0
print("Let's count seconds!!!")
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # Unbuffered stdout
while True:
    counter += 1
    sys.stdout.write('\r%d' % counter)
    time.sleep(1)

Escape sequences are more awesome in my opinion. But whatever works is good, of course.

Windows nowadays seems to need something like https://github.com/adoxa/ansicon to get escape sequences in cmd right; unfortunate. Didn't know this myself until now.
 
print() implicits a line feed, so Martal needs to resort to sys.stdout.write() instead of print(), and either has to flush() manually or reload stdout unbuffered because stdout buffers until flush, of course.

If you're using Python 3 or later you can write:

Code:
print(str, end='')

so it doesn't implicit a newline, and if you're using Python 3.3 or later, you can additionally write:

Code:
print(str, end='', flush=True)

so it doesn't implicit a newline but *does* flush immediately.
 

Martal

Neo Member
Once again guys thank you very much. I'll take some time to parse it all.

Since it looks like this need some external add-ons I think I might wait until I get some introduction to GUIs. Its something I want my friends to use as well so the easier to use, the better :)
 
I have been working with Java and only been programming for about a year so I am still quite raw. My biggest goal right now is to create some great stuff for my experience level. I know about Git but I want to be more comfortable with my skills before moving on to group efforts. Does anyone have any advice?
 

Makai

Member
I have been working with Java and only been programming for about a year so I am still quite raw. My biggest goal right now is to create some great stuff for my experience level. I know about Git but I want to be more comfortable with my skills before moving on to group efforts. Does anyone have any advice?
Maybe read Clean Code and Head First Design Patterns. Will help prepare you for group work.
 

Makai

Member
I loved C#'s += and -= syntax for event subscription. But I now prefer F#'s implementation, especially after writing a shorthand wrapper.

type Action<'T> () =

let event = Event<'T> ()

let handler f = Handler<'T> (fun o -> f)

member this.Subscribe f = handler f |> event.Publish.AddHandler

member this.Unsubscribe f = handler f |> event.Publish.RemoveHandler

member this.Trigger a = event.Trigger a
 
"if else" blocks has this format:
Code:
if (condition) {
    // code to execute
}
else if (anotherCondition) {
    // code to execute
}
(you can have several "else if (condition)" here)
else {
    // code to execute
}
Does your code follow that?

Also, there are a couple of errors in the code (missing semicolons, and the fact you are not correctly checking the input), so reread it carefully :)
Thanks, I got that fixed.

So for some reason even though I'm using a rand I keep getting the numbers 41 and 67 for num1 and num2.
Code:
    minNum = 0;
    maxNum = 100;
    reply = 'addition' || 'subtraction';
    rNum1 = (minNum) + (rand() % (maxNum));
    rNum2 = (minNum) + (rand() % (maxNum));

    cout << "Would you like to do a addition or subtraction problem? " << endl;
    cin >> operate;
    num1 = rNum1;
    num2 = rNum2;

Can anyone tell me what I'm doing wrong?

edit: Nevermind, I forgot to use srand :p
 
Thanks, I got that fixed.

So for some reason even though I'm using a rand I keep getting the numbers 41 and 67 for num1 and num2.
Code:
    minNum = 0;
    maxNum = 100;
    reply = 'addition' || 'subtraction';
    rNum1 = (minNum) + (rand() % (maxNum));
    rNum2 = (minNum) + (rand() % (maxNum));

    cout << "Would you like to do a addition or subtraction problem? " << endl;
    cin >> operate;
    num1 = rNum1;
    num2 = rNum2;

Can anyone tell me what I'm doing wrong?

edit: Nevermind, I forgot to use srand :p

Better yet, don't use either one. rand() is a piece of dog excrement.

Code:
#include <iostream>
#include <random>

int main(int argc, char **argv)
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0, 100);

    for (int i=0; i < 100; ++i)
        std::cout << distribution(generator) << "\n";
}
 
I have been working with Java and only been programming for about a year so I am still quite raw. My biggest goal right now is to create some great stuff for my experience level. I know about Git but I want to be more comfortable with my skills before moving on to group efforts. Does anyone have any advice?

Stuff like JHipster (requires some JS skill on the front end but I think rudimentary front-end JS is something everyone should get familiar with) is killer for getting going with something for the web. If you want to avoid Spring you can look into something like Spark ( http://sparkjava.com/ ) which doesn't carry a lot of baggage and gives you move room to add elements to your application as you see fit.

You can also make a desktop application with JavaFX or do something on mobile, but with a year's worth of experience in the language-basically the ability to read code and write methods and classes, but no design or pattern experience-having a framework around to plug code into to make things work seems like a higher chance of success than rolling your own.
 

Water

Member
I m currently teaching myself C++ which is fun, and developing a complex program as the intial challenge.

I'm aware of Visual Assist by Whole Tomato, but I was wondering if there is any "must have" tools/extensions that one should look into in the C++ world?

What kind of program are you working on?

Good C++ coding style keeps you out of most potential memory trouble. But if you are forced to mess with raw memory a lot, or you just have a very large and serious application, you'll want to use a memory analyzer.

On Linux everybody uses Valgrind, but on Windows there isn't a single go-to option. I think serious devs mostly use expensive stuff like Purify, Insure++ or BoundsChecker.

Anybody: recommendations for free memory analyzers on Windows? Has anybody here used DrMemory, for instance? I'm particularly interested in detecting memory stomping, reading uninitialized memory, etc. - not memory leaks.
 
What kind of program are you working on?

Good C++ coding style keeps you out of most potential memory trouble. But if you are forced to mess with raw memory a lot, or you just have a very large and serious application, you'll want to use a memory analyzer.

On Linux everybody uses Valgrind, but on Windows there isn't a single go-to option. I think serious devs mostly use expensive stuff like Purify, Insure++ or BoundsChecker. I'd personally like to hear recommendations for free memory analyzers on Windows since I'll be teaching C++ basics to my students soon. Has anybody here used DrMemory, for instance?

Most Windows people use Application Verifier.

Clang cl has ASAN which is becoming more and more mature on Windows, it's probably the best option.
 
I've used Dr.Memory and I quite liked it. Very simple to use and gave me i.m.o. good results but I haven't used any other similar software so I don't have anything to compare it to.
 
DrMemory is ok but it has some serious limitations. For starters, it's slow. Really slow. And I mean FUCKING slow. I mean it's basically an executable loader and assembly language interpreter. It uses dynamic instrumentation, so it literally interprets your program and injects new code into various places and monitors the state of your program as instructions change memory and do stuff.

Because of the fact that it uses dynamic instrumentation, there are some things it simply can't do. For example, consider this code:

Code:
char foo[100];
char bar[100];

memset(foo, 0, 101);

Dr. Memory can never find this bug. All your local variables are laid out at compile time, and Dr. Memory has no way of knowing that a 101 byte write is an invalid thing to do.

On the other hand, there is a class of bugs that *only* Dr. Memory can find, and that is use of uninitialized data. Technically MSan (Memory Sanitizer) can find this class of bugs too, but MSan hasn't yet been ported to Windows and it would be difficult to do so for various reason. Even if it were ported to Windows, there's a lot of complexity involved in using and deploying MSan.

Address Sanitizer (ASan) provides the best performance / detection compromise in my opinion, and it's what we've standardized on here. We do have some things using Dr. Memory, but it's being deprecated in favor of ASan. ASan still has some issues on Windows (notably x64 support is lacking / missing) but those are being actively worked on.

I mentioned earlier that a lot of people use App Verifier. This is mostly because ASan requires compiler support, and clang is the only compiler that supports it. clang-cl is still a work in progress (although to be fair it's about 95% complete on Windows) so it doesn't have near the adoption that MSVC has. But of course MSVC doesn't support ASan. So App Verifier with page heap enabled and various other things in gflags can find many common problems.

I don't know much about commercial options. I've heard of Purify and BoundsChecker, but not in over a decade, so I have to assume it's dead or something.
 

RustyO

Member
What kind of program are you working on?

Audio / DSP stuff. Windows / OSX / iOS

Good C++ coding style keeps you out of most potential memory trouble. But if you are forced to mess with raw memory a lot, or you just have a very large and serious application, you'll want to use a memory analyzer.

On Linux everybody uses Valgrind, but on Windows there isn't a single go-to option. I think serious devs mostly use expensive stuff like Purify, Insure++ or BoundsChecker.

Good style is a habit I'm trying to learn/force as I go along... I'll have a look into those you mentioned. Thank you.
 

NotBacon

Member
Any good modern C++ books out there similar to Accelerated C++? I'm guilty of treating it mostly like C with classes whenever I use it and I want to move away from that.

An example: std::default_random_engine. I had no idea about that as I've always used rand(). There seems to be a lot of outdated C++ information on the web...
 

Rur0ni

Member
Any good modern C++ books out there similar to Accelerated C++? I'm guilty of treating it mostly like C with classes whenever I use it and I want to move away from that.

An example: std::default_random_engine. I had no idea about that as I've always used rand(). There seems to be a lot of outdated C++ information on the web...
Tell me about it.

I've gotten a decent cherry popping in C++, but I can't really say for sure about good resources. C++ Primer seems just a touch behind as a learning resource (though good in general).

Perhaps this: Effective Modern C++ by Scott Meyers

Maybe cpp_is_king can shine some light.
 
Tell me about it.

I've gotten a decent cherry popping in C++, but I can't really say for sure about good resources. C++ Primer seems just a touch behind as a learning resource (though good in general).

Perhaps this: Effective Modern C++ by Scott Meyers

Maybe cpp_is_king can shine some light.

That book is reallllllly advanced. I strongly don't recommend it unless you already have a decent grasp on C++11.

I don't know what the best intro to C++11 is right now. Probably this: http://www.amazon.com/gp/product/0321563840/?tag=neogaf0e-20

Edit: Also this for the standard library: http://www.amazon.com/gp/product/0321623215/?tag=neogaf0e-20

Any good modern C++ books out there similar to Accelerated C++? I'm guilty of treating it mostly like C with classes whenever I use it and I want to move away from that.

An example: std::default_random_engine. I had no idea about that as I've always used rand(). There seems to be a lot of outdated C++ information on the web...

Fun fact about rand(). RAND_MAX is only required by the standard to be at least 32767. And on MSVC, that's exactly what it is. Let that sink in.
 

Fishlake

Member
I'm currently debugging AES. I hate how stuff goes in junk comes out. Why is it the wrong junk??? Debugging has never been more difficult than DES and AES.
 

Erudite

Member
Wondering if anyone can help verify this pseudocode I've come up with is correct, specifically finding the minimum value in A that is not in B, where A and B are min-Heaps (binary trees). What I could find on Google are all array based.

So the general idea given to me is:
a = extract_min from A
b = extract_min from B until( a == b OR b >a )
if(b > a) return a
else(back to first step)

(Recall that extract_min basically takes out the root of the heap and ensures the next minimum value is at the root)

The pseudocode I've come with is:
Code:
// Two heaps A and B
// Looking for the minimum of A not in B
// Assume all values in A and B are > 0

list_min_diff(A,B){
	while( A!=empty() || B!=empty() ){
		min_a = extract_min(A)
		min_b = extract_min(B)
		while(min_a > min_b){
			min_b = extract_min(b) // Since it is a min-Heap, continuously extract values from B until one is greater than the current min_a
		}
		if(min_b > min_a) return min_a // min_a cannot be in B since the last extracted value from B is greater than the current min_a
	}
	if(min_a > min_b) return min_a // In the case where B is emptied and A still contains other values
	return 0 // Where 0 indicates there exists no such value of A that is not in B
}
Just want to make sure I've set all my conditions correctly (specifically, I'm unsure about the initial while loop checking whether A or B is empty), taken care of all the edge cases, or if there's perhaps a better way to do it.
 
I'm currently debugging AES. I hate how stuff goes in junk comes out. Why is it the wrong junk??? Debugging has never been more difficult than DES and AES.
...why are you debugging aes code? Unless you're a cryptographer, you should be treating it as a black box.
 

phoenixyz

Member
I'm currently debugging AES. I hate how stuff goes in junk comes out. Why is it the wrong junk??? Debugging has never been more difficult than DES and AES.
I have never implemented AES myself but afaik there are test vectors for every part of the algorithm to isolate where stuff went wrong. Maybe this helps (AES Known Answer Test).

...why are you debugging aes code? Unless you're a cryptographer, you should be treating it as a black box.

Why not? It might be he does it out of interest.
 

Zeus7

Member
Android development question:

I am creating an app which monitors screen touches over a certain period of time and displays them as a heatmap in realtime. I would like to save these touches in a file so I can see the touch/heatmap again if I need to.

I am new to this, so far I have the basic app structure with a Navigation Drawer and an item selected listener. Each option opens a fragment instead of a new activity, I am still not 100% sure if this is the correct decision, I need to do further reading to compare the two.

My main concerns are am I right to use fragments and what is the best way to detect and show these screen touches? For the screen touches I have been doing some research and currently looking to use an onTouchEvent listener and use a draw function to plot these points on the screen. Am I on the right track here? Is there any advice or anything I should be aware of moving forward for this project?

Thank you :)
 
Android development question:

I am creating an app which monitors screen touches over a certain period of time and displays them as a heatmap in realtime. I would like to save these touches in a file so I can see the touch/heatmap again if I need to.

I am new to this, so far I have the basic app structure with a Navigation Drawer and an item selected listener. Each option opens a fragment instead of a new activity, I am still not 100% sure if this is the correct decision, I need to do further reading to compare the two.

My main concerns are am I right to use fragments and what is the best way to detect and show these screen touches? For the screen touches I have been doing some research and currently looking to use an onTouchEvent listener and use a draw function to plot these points on the screen. Am I on the right track here? Is there any advice or anything I should be aware of moving forward for this project?

Thank you :)
First of all, fragments are best used when you have to plan layout changes on the fly aka screen orientation, drawers, etc. If you need to fill your screen with new content use activities instead, it will also be faster to load.

For your app, I'd save the coordinates and the timestamp of the touches on a file (an SQLite database would do nicely here), then re-open that file as needed to color the pixels appropriately.
As with most things, there might be an api for that.
(I'm also a bit new on this so it might not be the best solution :p)
 

Nesotenso

Member
I asked this in the linux distro thread but someone here might be able to help as well

so I have a question about running csh scripts from bash. I defined the environmental variables the csh script uses in my bashrc file by using export instead of setenv. And then I executed the script from bash. It seems to work fine.

I was wondering if there is another efficient way to do this instead.

examples would be great.
 

Fishlake

Member
In my computer security class we have projects to build DES and AES without the use of extra libraries especially ones that do this for you.

I'm just about done debugging it. I've identified a problem in my mix columns and also probably in my inverse one. I've learned so much doing this it has been quite the experience.

I was just ranting yesterday when I couldn't limit it down to where it was going wrong. Every other function appears to be working correctly when I force feed it a state that I know the answer to.
 
Top Bottom