• 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

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
Need some PHP SQL help please!

I have been staring at this crap hour 2 hours now, and I've finally narrowed the problem down to this code.

What I want to happen: The BIDS database is checked to see if there's a row where the user_id is equal to $userID AND auction_ID = $auctionID. If there is no such combination in the BIDS table, set that $listBidder to a string so we can insert that value later. If the the combination IS found, set $listBidder so we can update that row in the table later.

What actually happens: (mysql_num_rows($rowCheck)) ALWAYS returns 0 (I think), so I always hit the INSERT INTO version of $listBidder.

The code listed below are the ONLY instances of $listBidder in the code right now that aren't commented out, so I am sure the problem exists here somewhere.

Code:
$bidsCheck = mysql_query("SELECT * FROM BIDS WHERE (user_id='$userID' AND auction_id='$auctionID')");
$rowCheck = mysql_fetch_array($bidsCheck);
			
if(mysql_num_rows($rowCheck) == 0)
	{
	$listBidder = "INSERT INTO BIDS (user_id,auction_id,amount)VALUES ('{$userID}','{$auctionID}','{$bidPrice}')";//First bid for user

	}
else
	{
	$listBidder = "UPDATE BIDS SET amount='$bidPrice' WHERE (auction_id='$auctionID' AND user_id='$userID')";//Second bid for user and beyond
	}
 
Need some PHP SQL help please!

I have been staring at this crap hour 2 hours now, and I've finally narrowed the problem down to this code.

What I want to happen: The BIDS database is checked to see if there's a row where the user_id is equal to $userID AND auction_ID = $auctionID. If there is no such combination in the BIDS table, set that $listBidder to a string so we can insert that value later. If the the combination IS found, set $listBidder so we can update that row in the table later.

What actually happens: (mysql_num_rows($rowCheck)) ALWAYS returns 0 (I think), so I always hit the INSERT INTO version of $listBidder.

The code listed below are the ONLY instances of $listBidder in the code right now that aren't commented out, so I am sure the problem exists here somewhere.

Code:
$bidsCheck = mysql_query("SELECT * FROM BIDS WHERE (user_id='$userID' AND auction_id='$auctionID')");
$rowCheck = mysql_fetch_array($bidsCheck);
			
if(mysql_num_rows($rowCheck) == 0)
	{
	$listBidder = "INSERT INTO BIDS (user_id,auction_id,amount)VALUES ('{$userID}','{$auctionID}','{$bidPrice}')";//First bid for user

	}
else
	{
	$listBidder = "UPDATE BIDS SET amount='$bidPrice' WHERE (auction_id='$auctionID' AND user_id='$userID')";//Second bid for user and beyond
	}

Way I always go about solving this type of problem is by actually printing the query string. So say

Code:
echo "SELECT * FROM BIDS WHERE (user_id='$userID' AND auction_id='$auctionID')";

Quite often you'll find that the values of userID or auctionID aren't what you're expecting.

Your if statement is otherwise correct, so I really don't see this being anything else than a query problem.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
*edit*
Scratch that, my idea was horrible. I'll try your idea, thanks!
 

nan0

Member
Need some PHP SQL help please!

I have been staring at this crap hour 2 hours now, and I've finally narrowed the problem down to this code.

What I want to happen: The BIDS database is checked to see if there's a row where the user_id is equal to $userID AND auction_ID = $auctionID. If there is no such combination in the BIDS table, set that $listBidder to a string so we can insert that value later. If the the combination IS found, set $listBidder so we can update that row in the table later.

What actually happens: (mysql_num_rows($rowCheck)) ALWAYS returns 0 (I think), so I always hit the INSERT INTO version of $listBidder.

The code listed below are the ONLY instances of $listBidder in the code right now that aren't commented out, so I am sure the problem exists here somewhere.

mysql_fetch_array moves the internal data pointer ahead, so there is nothing left to count for mysql_num_rows. You need to call num_rows first (directly on $bidsCheck).
 

upandaway

Member
Does that count as a community? It's more like a.. um.. well it is pretty cool for help on a particular thing, but it doesn't seem too reliable to actually get better.
 

nan0

Member
You guys, what are some neat programming communities to be part of? Like places to get help or news or whatever.

For help, definitely stackoverflow.com (and all of its sister sites). Just be aware that it isn't a forum, but a QA-site for specific questions and that discussions, very general or very basic questions are likely to get locked very quickly. On the other hand, you can even get answers on obscure problems since you can ask a very broad audience. There are also a couple of developers from the actual Android/C#/whatever teams registered, so it's also a good place to ask in-depth questions.

Edit: You seem to know it already. Yea, it's not really for pure learning purposes, but it's always interesting how other developers tackle a specific problem.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
mysql_fetch_array moves the internal data pointer ahead, so there is nothing left to count for mysql_num_rows. You need to call num_rows first (directly on $bidsCheck).

That was it! I don't know why I was so hellbent on using fetch_array in the first place, I didn't need it at all. Thanks a bunch nan0 and dutch. It's kind of scary because I don't know when or if I would have considered looking up the documentation for that function. It never occurred to me that my problem would be there.

Ballin, think I just fixed my other big problem right after you guys helped me too. 7AM, time for bed @_@
 
Does that count as a community? It's more like a.. um.. well it is pretty cool for help on a particular thing, but it doesn't seem too reliable to actually get better.

It will help you get better if you find the standouts and simply learn from the answers they provide to whatever the question is. Generally, they're going to answer the most interesting questions, or they're going to make a non-interesting question interesting because of their answer. If you're in C#, for example, I'm talking about folks like Jon Skeet and Eric Lippert and Marc Gravell, for example. Other languages will have their own gurus (and Jon Skeet is equally applicable on the Java side).

While you're right, it's a focused Q & A site, you can learn enough when the answerers challenge your thinking, taking the "teach a man to fish" route. Supplement that with reading and following interesting questions on the sister site Programmers.SE, follow a few blogs and read some interesting books, and you'll see your code improve.
 
I need help with an assignment. I'll put all the code i am using here. Use jsbin.com to copy the code in there to see the output. It will show a map and some marker points. What i need to do is create a text box in the bottom where the white space is at and also a submit button. I need to make whatever is typed in that box appear on the markers.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 80% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAqW-83zPPzSKLLIj5zjakXFSJ9EjMKqkc&sensor=true">
</script>
<script type="text/javascript">
function initialize() {


var mapOptions = {
center: new google.maps.LatLng(40.718111, -74.012194),
zoom: 8,
maxZoom:12,
minZoom:7,
mapTypeId: google.maps.MapTypeId.SATELLITE
};


var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);

for (i=0; i<360; i+=60){

var lat = map.getCenter().lat()+Math.sin(i*Math.PI/180.0);
var lng = map.getCenter().lng()+Math.cos(i*Math.PI/180.0);
var latLng = new google.maps.LatLng(lat, lng);

var markerOptions = {
position: latLng,
map: map,
title: 'Click to zoom'
};

var marker = new google.maps.Marker(markerOptions);


google.maps.event.addListener(marker,'click',function(e){
console.log(e);

});


}

}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"/>
</body>
</html>
 

usea

Member
I need help with an assignment. I'll put all the code i am using here. Use jsbin.com to copy the code in there to see the output. It will show a map and some marker points. What i need to do is create a text box in the bottom where the white space is at and also a submit button. I need to make whatever is typed in that box appear on the markers.
What's the problem? What kind of help do you need? Are you stuck anywhere in particular? Are you getting some kind of error, or do you not even know where to begin?
 

Magni

Member
"Easily" got through the Qualification Round, although I somehow screwed up the large input for the first problem.

Problem B was easy, C was easy for the small problem, timed out on the first large one (I generated my own large set to test first but misread it as having 100 cases instead of 10,000...). I didn't even write a line of code for D, just some whiteboarding.

I might have been able to solve C on the large inputs had I had more time, but I was only able to code from 1 to 3ish am when the round started, and then from 1ish to 2am when it closed since I had another coding competition all day today at school, an optimization problem in Java that my team horribly failed at :lol It was fun at least.

And now, time to fail all three subrounds of Round 1 like last year :D

If anyone wants to add me as a friend, search for Magni :)
 

Kalnos

Banned
@Magni

I solved C for the first large input after the fact but I have no idea how you're supposed to handle 10^100 lol. For the first large input I just calculated all the possibilities between the max range, stored them in a list, and then simply iterated through that list to determine how many within each given range. For the first large input I have it executing in <10 seconds.

Didn't even get to attempt B/D. :<

I didn't have much time so I only ended up with 40 points but that's enough if I have more time when Round 1 starts!
 
Qualified as well solving all of A and C small lol. I'll try to make an effort in the first round just for some lulz, but I don't really expect to go through.
 

Magni

Member
@Magni

I solved C for the first large input after the fact but I have no idea how you're supposed to handle 10^100 lol. For the first large input I just calculated all the possibilities between the max range, stored them in a list, and then simply iterated through that list to determine how many within each given range. For the first large input I have it executing in <10 seconds.

Didn't even get to attempt B/D. :<

I didn't have much time so I only ended up with 40 points but that's enough if I have more time when Round 1 starts!

I was lazy (and didn't have much time), so I just whipped up a quick Ruby script:

Code:
class Fixnum
  def fair?
    self.to_s == self.to_s.reverse
  end

  def square
    self ** 2
  end
end

out = File.new('C.out', 'w')

File.open('C-large-1.in', 'r') do |f|
  t = f.gets.to_i
  t.times do |n|
    a, b = f.gets.split.map(&:to_i)
    alpha = Math.sqrt(a).ceil
    beta  = Math.sqrt(b).floor
    count = (alpha..beta).select(&:fair?).map(&:square).select(&:fair?).count

    out.puts "Case ##{n+1}: #{count}"
  end
end

out.close

It clearly needs optimizing, but at that point I was already qualified and worn out. I hope they put up the analysis soon, I want to see how C-large-2 and D can be done.
 

Kalnos

Banned
I used C# and all your code is doing is making me feel bad because it looks so elegant. :p

I'm baffled by C-large 2. D is possibly some form of recursive backtracking? That may be too inefficient though.
 

Magni

Member
I used C# and all your code is doing is making me feel bad because it looks so elegant. :p

I'm baffled by C-large 2. D is possibly some form of recursive backtracking? That may be too inefficient though.

Well it's easy to find out the number of fair numbers, even for intervals up to 1e100: There are 9e(ceil(d/2)-1) fair numbers with d digits. I just don't know if there is also a simple formula for square numbers..

edit: oh, and that's Ruby for you :D I can't imagine a more elegant scripting language (it's half the reason Rails is so awesome IMO).
 

Exuro

Member
Does anyone know of any LabVIEW tutorials that I could pick up on pretty quickly? My instrumentation class has us doing a project using it and other than using some prebuilt DMM stuff I'm not very familiar with it.
 

sca2511

Member
Wondering if an GAFfers know how to code in Processing? I'm taking an intro to programming class and that is the language we're working with.
 

jokkir

Member
How do you suppress warnings in visual basic? I think it's not letting me compile because im using functions like strcpy, strlen and stuff >__>

EDIT - I meant Visual Studio ><
 

usea

Member
How do you suppress warnings in visual basic? I think it's not letting me compile because im using functions like strcpy, strlen and stuff >__>
A warning doesn't stop you from compiling. It just warns you. And suppressing them just makes them not show up, it doesn't change any behavior.

I don't know anything about VB or those functions though.
 

Ixian

Member
A warning doesn't stop you from compiling. It just warns you. And suppressing them just makes them not show up, it doesn't change any behavior.

I don't know anything about VB or those functions though.
Unless you have treat warnings as errors enabled, which we did back when I was in school. :) Also, I'm pretty sure he meant Visual Studio, not Visual Basic, since the stuff I helped him with before was in C++ and I don't think strcpy exists in VB.
 

Spoo

Member
Unless you have treat warnings as errors enabled, which we did back when I was in school. :) Also, I'm pretty sure he meant Visual Studio, not Visual Basic, since the stuff I helped him with before was in C++ and I don't think strcpy exists in VB.

Just friendly advice: treat warnings as errors regardless of whether or not your compiler is. I know a lot of people who let many, many warnings through without giving them a second thought; this isn't just bad practice, it's straight up dumb. Compile-time warnings are a great way of letting you know you're leaving the backdoor open for a lot of undefined behavior, or just accidents waiting to happen.

Obviously, exceptions to every rule, but yeah.

edit: Not directed at you Nekura; just quoted you for segue purposes ;)
 

usea

Member
Unless you have treat warnings as errors enabled, which we did back when I was in school. :) Also, I'm pretty sure he meant Visual Studio, not Visual Basic, since the stuff I helped him with before was in C++ and I don't think strcpy exists in VB.
Ah, well yeah in C++ that's different.
 

AcciDante

Member
I have an idea for an app that I'd like to try and take a stab at making once this semester ends. I've never done any work with networking programming though, and I'd like to read up on it so I'll at least have a starting point when I'm ready to begin. Besides that, I'm pretty confident with my programming abilities.

Anyone have any recommended resources on the subject? The app would involve people being to able to upload and share pictures. (Like every other app, I know!)
 

Madtown_

Member
There's been a lot of talk about IDEs lately in this thread, but what about the perfect programming font? I like Consolas but I'm experimenting with some alternatives. Does anyone use Inconsolata? Adobe's Source Code Pro?

What's your pick?
Consolas is my favorite. I've used inconsolata as well but Consolas is now my default on all machines.

Consolas looks great down to really small sizes, don't think that was the case for inconsolata.
 

jokkir

Member
Unless you have treat warnings as errors enabled, which we did back when I was in school. :) Also, I'm pretty sure he meant Visual Studio, not Visual Basic, since the stuff I helped him with before was in C++ and I don't think strcpy exists in VB.

Ah yes, my mistake. Visual Studio and not Basic :p I probably should have started coding with Visual Studio first instead of using the school compiler >__>
 

Milchjon

Member
I've asked about this problem a few days ago:

Is there a way to find out more about errors in Visual Studio 2012?

Because right now I can't seem to create Win 32 console programs/projects. I can create websites and single C++ files, and open old projects.

It only gives me a "Error creating the project" (Or something similar, it's German) when I try to create a new one, and apparently that isn't specific enough to efficiently hunt for solutions on Google. Or at least I haven't found much relevant stuff so far.

Is there any way to get more info within Visual Studio? Or will I have to just try and reinstall it?

That's what I was trying to ask here, how to find more info on it :-D


Basically, I'm trying to create a new Win32 console project, it does nothing and gives me this message:

GQxR6ch.png


Like I said, that's the only thing it says down in the left corner, and since that obviously isn't enough information, I can't find much on it. No error code or anything else.

So is there some place within Visual Studio which expands upon the info in the message?

Or will I have to search forever on Google.

But now for the weird part:

The same fucking thing happens in every version of Visual Studio I can get a hold of on my PC.

I tried to repair Visual Studio 2012 Premium. Nothing. I installed Visual Studio Express 2010. Same thing. I installed Visual Studio 2012 Express. Same thing.

Is there any underlying part of the system that it relies on that could be in any way corrupted? Any way to check?

I'm getting kinda desperate here...
 

JaMarco

Member
This question is really tricky! Even if it didn't include duplicate letters it'd be tricky. To help you start looking, the problem relates to the "factoradic" number system. It's a system that's useful in mapping between an integer N and the Nth permutation in lexicographical order. This process is often called ranking and unranking. However, the explanations of this process you'll most commonly find assume that the elements in the input distinct (ie: a set). It gets a lot harder with repeating elements.

Here are some words that will help googling: factoradic permutation lexicographical repeat

Here is the best explanation I know of, for finding the Nth permutation (or finding N, given a permutation) in lexicographical order. http://www.keithschwarz.com/interesting/code/?dir=factoradic-permutation
But again, it assumes no duplicates.
I can see how to do it relatively efficiently. starting at the end of the string with only one character to 'choose' you so far have a rank of 0 (number 1, top rank) and have only 1 permutation. furthermore you have a total of 1 character to choose from and it's weight is 1, store this information somewhere

working backward through the string for each character, add this character to the list, if we let n be the number of permutations of our previous substring then
this substring will have n * (n+1) / k substrings where k is the count for the character we added at this point.

If you look at the alphabetical ordering of this character compared to the list of available characters then you take the sum of the counts of all of the characters that precede the current character alphabetically, multiply them by n. add this value to your current rank.

repeat this process and you should have a rank that is off by 1. add 1.

Example:

Let our string be apple

starting at e we have 1 permutation and 1 instance of e. we have rank 0

looking at l, we add p to the list of characters, giving it a count of 1
this means at this point there are 2 permutations
also, l is after e so we take 2 / 2 * 1 = 1
our rank is 1

next up is p, p is new too so it gets a count of 1
2 * 3 / 1 = 6 permutations for 'ple'
p is worse than both l and e so 6 / 3 * 2 = 4
our rank is now 5

a second p, p's count goes up to 2
6 * 4 / 2 = 12 permutations for 'pple'
again, p is worse than both l and e
12 / 4 * 2 = 6
our rank is now 11

finally the a, a is better than everybody
12 * 5 / 1 = 60
a is better than everyone, we add nothing to our rank

our final rank is 11

lets check the 'pple' substring because checking all 60 permutations for apple will be tiresome

elpp, eplp, eppl, lepp,
lpep, lppe, pelp, pepl,
plep, plpe, ppel, pple

that looks like a complete list of 12 to me, and with pple as the final element in that list, number 12, 11+1. Also, since apple starts with a, it can't lose rank so
apple must have the same rank, and it does.

since it works this time it must work every time :p

You can save a division by multiplying the new permutation by (n+1) after the rank at this point has been calculated. which is also preferrable because it means for the last (and thus largest) number of permutations you can avoid calculating that whole number, which could, in all probability wind up breaking your 64 bit barrier even if you're given a string with a rank that's under the limit.
These helped me a lot, thanks for the responses.
 

Chris R

Member
I need help with an assignment. I'll put all the code i am using here. Use jsbin.com to copy the code in there to see the output. It will show a map and some marker points. What i need to do is create a text box in the bottom where the white space is at and also a submit button. I need to make whatever is typed in that box appear on the markers.

Here is my code:

Well you are firstly going to need the two other input elements (one for text, one for the button). Then I'd make sure you can access the markers again later after you create them. Then you just need another javascript function to fire when the button is clicked that gets whatever text is in the textbox, iterates over the markers and updates their text.

All in all you are really close to getting it to work, just a few lines really. Google Maps API is really powerful.
 

arit

Member
I've asked about this problem a few days ago:





But now for the weird part:

The same fucking thing happens in every version of Visual Studio I can get a hold of on my PC.

I tried to repair Visual Studio 2012 Premium. Nothing. I installed Visual Studio Express 2010. Same thing. I installed Visual Studio 2012 Express. Same thing.

Is there any underlying part of the system that it relies on that could be in any way corrupted? Any way to check?

I'm getting kinda desperate here...

Are there umlaute or other special characters somewhere within the filepath or your user account name? At least Eclipse had problems when I tried to launch it located at "# tools" directory, maybe VS has some similar problems.
 

jts

...hate me...
I need some database help. I know this is not proper programming.

Lets say I have a periodic sales report, for which the number of days can vary. Usually one, at most 3, but it can be many.

Each report is a record.

But then I also have expenses, which have a specific day (one expense can have only one day).

I also may have have other tables with a field for date.

I've been debating for weeks how should I link all this together to generate business reports efficiently.

If it was not for the one report to many days thing, it would be simple.

Should I create a table just for days?
 
I'm not sure why you're talking about my print statement. It sounds like you think my threads are picking up at the point they were created as if it was a fork, but that's not what is happening or at the very least it shouldn't be.
I had initially read your code wrong. I thought the print statement that you had was in the middle of the while loop and being called after every child was created. Under that context, my following reply would make sense:


Lets say you're in the function that is creating the children (which is a part of your main process, which I will henceforth refer to as the parent).

The parent creates child and then does a print statement. What you will often find here is that the print statement will actually take a long time and the child will have already returned. Sometimes this won't be the case, and you'll get interleaving children accessing and modifying the stack values which will eventually in some cases cause a segfault.

My point was that if you remove that print statement, my bet is that these segfaults would be highly reproducible. Since your print statement is so slow to execute, the next child is most of the time being created after the first child has already returned in which case you don't have more than one child modifying the shared stack.


I was reading it on my smartphone and I didn't see that the print statement was outside of the while loop that spawned children. Either way, it's obvious why the problems are occurring when you consider that the stack is a shared piece of memory that every child you create will be modifying.

But Postcrypt you're fine with?

LOL, don't think he looked it up and realized the joke.
 

Omikron

Member
I need some database help. I know this is not proper programming.

Lets say I have a periodic sales report, for which the number of days can vary. Usually one, at most 3, but it can be many.

Each report is a record.

But then I also have expenses, which have a specific day (one expense can have only one day).

I also may have have other tables with a field for date.

I've been debating for weeks how should I link all this together to generate business reports efficiently.

If it was not for the one report to many days thing, it would be simple.

Should I create a table just for days?

Why not just have a date range on the report record?
 

Slavik81

Member
There's been a lot of talk about IDEs lately in this thread, but what about the perfect programming font? I like Consolas but I'm experimenting with some alternatives. Does anyone use Inconsolata? Adobe's Source Code Pro?

What's your pick?

I rather like Bitstream Vera Sans Mono. I spend my day staring at a page like this:
i4T2FNiFslFyw.png

Though, I need to disable ClearType on Windows in order to make it look right.

Though, I don't mind Consolas, I feel like I need to use a larger font size for it. Maybe for no good reason.
Consolas.png
 

hateradio

The Most Dangerous Yes Man
There's been a lot of talk about IDEs lately in this thread, but what about the perfect programming font? I like Consolas but I'm experimenting with some alternatives. Does anyone use Inconsolata? Adobe's Source Code Pro?

What's your pick?
Oh man, Souce Code Pro looks awful in Notepad++. I guess it needs some sub-pixel AA. It looks okay in Sublime.

I used to use Droid Sans Mono a lot, but now I've defaulted to Consolas . . . but if I don't care, I use Arial Unicode MS (because Unicode and because I get used to the spacing).

I rather like Bitstream Vera Sans Mono. I spend my day staring at a page like this:
https://i.minus.com/i4T2FNiFslFyw.png
Though, I need to disable ClearType on Windows in order to make it look right.

Though, I don't mind Consolas, I feel like I need to use a larger font size for it. Maybe for no good reason.
http://img.photobucket.com/albums/v48/Slavik81/Consolas.png
That first one's not Bitstream Vera Sans.
 
Top Bottom