Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

02-18-2017 , 07:39 PM
Look out for trick questions, our teacher loved questions like this:

Code:
void increment(int x) {
    ++x;
}

main {
int x = 5;
increment(x);
printf("X is now: %d\n", x);
}
Fill in the print out: X is now: __
Programming homework and newbie help thread Quote
02-18-2017 , 07:43 PM
X is undefined
x is 5
Programming homework and newbie help thread Quote
02-18-2017 , 07:46 PM
5 is correct. That one gets tons of students, and it wouldn't surprise me if it slips up a few professionals from time to time.
Programming homework and newbie help thread Quote
02-18-2017 , 08:11 PM
Quote:
Originally Posted by lolfrew
Yeah sorry, with doing nothing i mean they do the same..?
Example:
Int x = 1;
X++; is now 2
++x is now 3.
Lol.. still cant sleep.Programming homework and newbie help thread
To be clear if this was other way around, so:
X=1
++x is now 2
X++ is now still 2 right? (For the love of god say yes)
And on mext line 3.
Programming homework and newbie help thread Quote
02-18-2017 , 08:20 PM
Code:
x=1
++x; // X is now two
x++; // X is now three, after this statement executes
Code:
x=1;
printf("x: %d\n", x);
printf("x: %d\n", ++x);
printf("x: %d\b", x++);
printf("x: %d\n", x);
x: 1
x: 2
x: 2
x: 3

I would expect the answer that is being looked for if the question is:
x=1;
++x;
x++;

What is the value of X?

Would be x is 3.
Programming homework and newbie help thread Quote
02-18-2017 , 08:31 PM
The 'now' was literally in my textbook. . Thanks kato Programming homework and newbie help thread i got it Programming homework and newbie help thread
Programming homework and newbie help thread Quote
02-19-2017 , 09:00 PM
So I got a 2 bit predictor, my starting state is weakly taken and I need to calculate the predictionaccuracy:

Code:
for (int i=0; i < 100; i++)
{
 for (int j=0; j < 50; j++)
{
...
}
}
So with i = 0 we take the branch, so we are at i = 0 and j = 0 and set our predictor to strongly taken, right ? So if we iterate j now, does that mean we are not taking a new branch ? As we are still in the i = 0 branch, or does every iteration count as a new branch ?
Programming homework and newbie help thread Quote
02-19-2017 , 11:00 PM
I don't know that much about branch prediction. But... do you know how to write a nested loop in assembly? If so, does that help?

(my assembly is rusty but but essentially I'd probably do each loop by jumping back to the start, like if you'll forgive pseudocode

Code:
loop1:
loop2:
    .... do stuff
j = j+1
if j == jmax: (j=0, i+=1, goto loop1) else goto loop2
branch prediction is going to expect you to go to loop2, and it'll be right 49/50 times

(I'm missing the check to exit the loop entirely but adding it would be messy and I think you get the idea.)
Programming homework and newbie help thread Quote
02-19-2017 , 11:12 PM
That actually makes more sense that way.

The biggest problem for me right now is that I absolutely hate this subject, so learning it is quite the grind as it just feels like I forget everything instantly again.

Thanks a lot!
Programming homework and newbie help thread Quote
02-20-2017 , 09:13 AM
Have personally never used a preincrement operator (haven't ever written C or C++, don't even know if the operator exists in C#). Are there any circumstances in which it is actually useful?
Programming homework and newbie help thread Quote
02-20-2017 , 12:41 PM
it's syntactic sugar, you can easily get by without it, but it can be handy sometimes, mostly for clarity.
Programming homework and newbie help thread Quote
02-20-2017 , 10:20 PM
Quote:
Originally Posted by ChrisV
Have personally never used a preincrement operator (haven't ever written C or C++, don't even know if the operator exists in C#). Are there any circumstances in which it is actually useful?
A couple of links explaining why pre-increment is preferred.

From Stack Overflow

A Discussion of How They Work "Under the Covers"

In reality with the sophistication of C/C++ compilers today, it probably doesn't make much of a difference.
Programming homework and newbie help thread Quote
02-21-2017 , 01:34 AM
Quote:
Originally Posted by adios
A couple of links explaining why pre-increment is preferred.

From Stack Overflow

A Discussion of How They Work "Under the Covers"

In reality with the sophistication of C/C++ compilers today, it probably doesn't make much of a difference.
Yep it doesn't matter. If you look at the generated assembly code it likely looks the same in most situations such as using ++i or i++ in a loop.
Programming homework and newbie help thread Quote
02-21-2017 , 10:44 AM
I just disassembled 2 lines of C code:

temp = i++;
temp = ++i;

Same number of ops in assembly, just different ordering.
Programming homework and newbie help thread Quote
02-21-2017 , 04:49 PM
Quote:
Originally Posted by Lattimer
I just disassembled 2 lines of C code:

temp = i++;
temp = ++i;

Same number of ops in assembly, just different ordering.
Try creating a vector using STL, then instantiate an iterator for the vector then try it on the iterator. Another idea to test it would be to create a class, overload the ++ operator such that a private member is changed, and then try it. I may do that myself and report back. The idea here is to try and force the creation of a temporary object on the right hand side of the equal sign with the post increment operator. Not sure what gcc will do with that and whether or not gcc optimization levels make a difference.
Programming homework and newbie help thread Quote
02-21-2017 , 08:10 PM
Just checked and the preincrement operator does exist in C# (not surprising since it would assist with porting code). I have never seen it used in the wild. I would be pretty surprised if the .NET compiler didn't optimize away any difference.
Programming homework and newbie help thread Quote
03-20-2017 , 10:51 PM
need some php/ajax help, if anyone has experience

I'm trying to figure out how to upload a value calculated from a .js file to a .php file. I need to send the value plus (I think) the session value for a user id. Afaik, I can only get the user id from the php file running the javascript. I don't seem to be able to make the ajax request from the php page running the javascript, instead needing to run it from the .js file that calculates the value.

I saw something like:
Code:
$.ajax(
    url: "upload.php",
    type: "POST",
    data: { 'u': <?php echo $userid; ?>, 'v': value}
);
but it won't run properly from php, and the .js file doesn't like the syntax and obviously can't parse the <?php ?> statement.

What's a boy to do? I'm sadly very inexperienced with ajax calls. =-/

Edit

Can I just pull the user id from the session variable?

Last edited by Loki; 03-20-2017 at 11:03 PM.
Programming homework and newbie help thread Quote
03-20-2017 , 10:57 PM
Instead of type, it's method. jQuery's documentation on ajax is here:
http://api.jquery.com/jquery.ajax/

Also install firebug on FireFox if you haven't. Chrome has a debugger, and development tools as well.
Programming homework and newbie help thread Quote
03-20-2017 , 11:09 PM
"type" should still work, it's an old alias for method which has not yet been deprecated. Using method is better.

There are a couple of problems with your approach. One is that you can't put php in a .js file. The other is that you don't have quotes around the php output.

Include this somewhere before you load your .js files in your HTML:

Code:
<script type="text/javascript">
    var userid = '<?php echo $userid; ?>';
</script>
Then:

Code:
//note that the userid variable must be set elsewhere on the page
$.ajax(
    url: "upload.php",
    method: "POST",
    data: { 'u': userid, 'v': value}
);
It's kind of a mess, but that's what happens when you try to mesh together server-side logic and client-side logic. One of many reasons things like Angular are superior.
Programming homework and newbie help thread Quote
03-20-2017 , 11:20 PM
Here's what looks like it's working:

Code:
var xhttp = new XMLHttpRequest();
            xhttp.open("POST", "upload.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send("v=" + value);
sticking that inside a function that's passed value, and then using the session to get the user id. Not 100% why it's working, but it's writing to the database as best as I can tell.

yanked partially from here
Programming homework and newbie help thread Quote
03-21-2017 , 12:27 AM
you can't mix php in a .js file, the first code example should work fine if it's inside script tags in a .php (well, maybe you can if you mess with apache's file handlers but that's not normal)

edit: ChrisV said this already, and I missed the lack of quotes too.

Last edited by _dave_; 03-21-2017 at 12:29 AM. Reason: very slow pony
Programming homework and newbie help thread Quote
03-21-2017 , 01:04 AM
ChrisV's code is fine. You have to do that kind of thing sometimes to get the AJAX design pattern working with PHP/jQuery. It is a mess. If you run into problems, it may be time to rethink the design a little.

What happens is the <?php ?> tag code executes first, which then puts it into the string for when the HTML executes, which in turn executes the JavaScript.

Last edited by pokerponcho; 03-21-2017 at 01:20 AM.
Programming homework and newbie help thread Quote
03-21-2017 , 01:12 AM
Quote:
Originally Posted by KatoKrazy
Look out for trick questions, our teacher loved questions like this:

Code:
void increment(int x) {
    ++x;
}

main {
int x = 5;
increment(x);
printf("X is now: %d\n", x);
}
Fill in the print out: X is now: __
I'm immune to these. I trace through the code without looking at all what it's meant to do. Also, when did peeps stop using int for main() function?
Programming homework and newbie help thread Quote
03-21-2017 , 09:10 AM
Quote:
Originally Posted by pokerponcho
ChrisV's code is fine. You have to do that kind of thing sometimes to get the AJAX design pattern working with PHP/jQuery. It is a mess. If you run into problems, it may be time to rethink the design a little.

What happens is the <?php ?> tag code executes first, which then puts it into the string for when the HTML executes, which in turn executes the JavaScript.
Well, the big issue with Chris's way was that all the JavaScript wasn't contained within the single php page that runs the calculation. That was its own .js file, and when I originally tried to add a js function to the php page the external js file couldn't access it. Thought maybe it'd be accessible as a global function or something, but I guess not.

If I didn't explain it well, the situation is:
php page -> contains JavaScript -> calls external JavaScript file -> sends db write request to a different php file

If it's possible for external JavaScript to send data back to the original php page's JavaScript, I dunno how to do it. But it works now, so I'm not too fussed. This was more of a proof of concept.
Programming homework and newbie help thread Quote
03-21-2017 , 11:22 AM
Quote:
Originally Posted by pokerponcho
I'm immune to these. I trace through the code without looking at all what it's meant to do. Also, when did peeps stop using int for main() function?

Hard to trace through code without looking through what it is supposed to do when taking a written exam

You are correct, main should return an int.
Programming homework and newbie help thread Quote

      
m