Wikipedia:Reference desk/Archives/Computing/2011 July 17: Difference between revisions
Line 250: | Line 250: | ||
There are two problems:<br> |
There are two problems:<br> |
||
1. The foreach loop runs only once for each row, but I need to run |
1. The foreach loop runs only once for each row, but I it need to run thrice (as many times as the number of columns in the first array). If I put a for loop just before or after the second foreach loop, the row counter does not get incremented, but I need it to. How do I go about it? <br> |
||
2.The row and col counters keep changing, so how do I store the result for a particular element into the right location? That is, how can I store the product for a[0][1] into c[0][1]?<br> |
2.The row and col counters keep changing, so how do I store the result for a particular element into the right location? That is, how can I store the product for a[0][1] into c[0][1]?<br> |
||
So you see, it's almost done,only these two problems need to be resolved. Thank you very much once again for telling me the basic way to traverse a 2D array. |
So you see, it's almost done,only these two problems need to be resolved. Thank you very much once again for telling me the basic way to traverse a 2D array. |
Revision as of 21:36, 24 July 2011
Computing desk | ||
---|---|---|
< July 16 | << Jun | July | Aug >> | July 18 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
July 17
Web site for selling diamonds
I find many web sites for buying diamonds, like this one: [1]. There you pick the parameters you want, then they sort through their DB and list those diamonds (with prices) which match your search criteria.
I want just the reverse, to list my diamonds and get quotes from multiple buyers. Is there such a site ? (I realize I could use a general auction site, like eBay, but would prefer a site specifically designed for diamond sales.) 68.79.93.3 (talk) 02:12, 17 July 2011 (UTC)
- Okay, you've reached the limit here. You're not even sure that you have diamonds, and now you want to sell them on the web? Please take your stuff to somebody who is qualified to evaluate it, and come back when you have a meaningful question to ask. Looie496 (talk) 02:17, 17 July 2011 (UTC)
- Personally I'd be pretty suspicious of any site that allowed you to do that. Who would buy gemstones from some random joe on the Internet? APL (talk) 02:28, 17 July 2011 (UTC)
- I would expect them to be verified, at some point (although if they were certified, then this verification was already done beforehand). The point is to get an agreement that "if the diamond is as you say, then we will pay X for it". Note that there are sites where you describe your diamond and one buyer (who owns the site) will quote you a price. I just want the same thing with multiple buyers. 68.79.93.3 (talk) 02:55, 17 July 2011 (UTC)
- You have many questions (e.g. here) about these diamonds that may after all not be diamonds. And "Give me advice to help me make as much money as possible" is an odd line of questioning for a "reference desk". Simple: donate the whole lot to charity. Then the charity has to figure out what to do with them, it gets the money, and you have the warm feeling that comes from financing something even more worthwhile than your average kickstarter project. You might even get a little tax break. -- Hoary (talk) 03:29, 17 July 2011 (UTC)
- I will have them appraised (or even certified, if required), but would then like the option to sell them online. 68.79.93.3 (talk) 03:46, 17 July 2011 (UTC)
- All right, let's return to your question I want just the reverse, to list my diamonds and get quotes from multiple buyers. Is there such a site ? (emphasis added). That appears to be a googling question, a diamonds question, or a selling stuff question. Try the "Miscellaneous" desk (or google). -- Hoary (talk) 04:45, 17 July 2011 (UTC)
- I tried Google, but kept findings sites wanting to sell me diamonds, or sites wanting to buy diamonds, but only by a single buyer, versus multiple bidders. So, how would I do a Google search to exclude those ? 68.79.93.3 (talk) 08:07, 17 July 2011 (UTC)
- APL, many stupid people have some money. Just think of the market for "generic" or "herbal" varieties of "Viagra", etc etc. Or the nitwits who pay a bit upfront for nonexistent Nigerian millions. -- Hoary (talk) 03:29, 17 July 2011 (UTC)
- Many stupid people might have money at some point, but as you see they will have trouble keeping it, therefore, people with money tend to be those who can deal with it. — Preceding unsigned comment added by 88.8.79.148 (talk) 13:49, 17 July 2011 (UTC)
- APL, many stupid people have some money. Just think of the market for "generic" or "herbal" varieties of "Viagra", etc etc. Or the nitwits who pay a bit upfront for nonexistent Nigerian millions. -- Hoary (talk) 03:29, 17 July 2011 (UTC)
- The answer to the original question is eBay, which because of the network effect contains the maximum number of buyers-of-diamonds-from-random-Joes-on-the-Internet you are likely to find. Comet Tuttle (talk) 04:18, 19 July 2011 (UTC)
Command Prompt problem
This is probably incredibly simple and I'm just missing something. I have three batch files: One "menu.bat" which prints a text file "menu.txt" displaying two options ("1" and "2"), and two other batch files, "1.bat" and "2.bat" (the options printed in menu.bat). Here's my question: I want to run menu.bat, but Command Prompt disappears as soon as I load it. What am I doing wrong? -- 68.0.166.142 (talk) 14:17, 17 July 2011 (UTC)
- Do you open CP and run it or double click to run it? Does it contain an exit command? ---— Gadget850 (Ed) talk 14:21, 17 July 2011 (UTC)
- I don't open CP and then run the command, I just want to be able to doubleclick the batch file and for CP to remain up so I can see menu.txt printed and respond to it ("1" and "2"). It does not contain an exit command. Unless you know of a way to load CP and automatically navigate to menu.bat? -- 68.0.166.142 (talk) 14:39, 17 July 2011 (UTC)
- You can do that in several ways. Probably the most straightforward is to use the start command. You can read documentation by typing start /? in the command prompt, or at the Microsoft help website for Cmd.
- You may also want to use the pause command at the end of your script, to hold a terminal open even if it has completed all its tasks. Nimur (talk) 16:05, 17 July 2011 (UTC)
- The easiest way of achieving what you want is to use the /K switch, which treats the remainder of the line as a command to be executed without terminating the shell. Here's the .bat file:
@cmd /K type c:\yourpath\menu.txt
- This script can be run by clicking its icon. The at-sign before cmd suppresses echoing of the command (so that you just see your menu, and not the command used for printing it). If your 1.bat and 2.bat are located in c:\yourpath, you may want to change to that directory first, like so:
@c: @cd \yourpath @cmd /K type c:\yourpath\menu.txt
- --NorwegianBlue talk 20:33, 18 July 2011 (UTC) In the second example, it's not necessary to state the full path in the third line, @cmd /K type menu.txt is sufficient. --NorwegianBlue talk 20:41, 18 July 2011 (UTC)
Malicious program in my computer
I am only recently having a problem with editing WP (last few days), in that when I edit (hopefully it won't happen here), words like "P u r f u m e", etc. (here I intentionally separated each letter with a space) turn into a hyperlink to ads. They are the same blue color as WPs blue links, with a throbbing, dashed underline. It also tends to delete words such as other instances of the above example that are on the page, hyperlinking only the one remaining. I saved one edit by accident and had to revert myself, because it made these alterations I didn't mean to make. Has anyone had this problem; know how it can be removed from my computer? I have taken the steps of removing any recent software, applications and Windows updates with no effect, and Norton Anti-virus isn't picking it up as a virus. Any feedback would be appreciated, as this hampers my editing substantially here. (by the way, this hyperlinking effect goes on anywhere I go on the web - normally I just ignore it, but it makes editing WP sometimes impossible) This message originally posted at Wikipedia:Help desk#Malicious program in my computer Thanks, Hamamelis (talk) 17:09, 17 July 2011 (UTC)
- You might give your system a Malwarebytes scan, for starters. ¦ Reisio (talk) 17:19, 17 July 2011 (UTC)
- Thanks, I'm trying it now, Hamamelis (talk) 17:45, 17 July 2011 (UTC)
- If that does not work, I would then try SUPERAntiSpyware, and after that Avast! which can do boot-time scans before the malware has loaded up. Then try some of these: http://www.techsupportalert.com/content/probably-best-free-security-list-world.htm In any case, cleaning out the garbage beforehand with Ccleaner should make the scans a little quicker. 2.97.209.26 (talk) 21:21, 17 July 2011 (UTC)
- I appreciate all of your suggestions. Alas, to no avail so far. For some reason, I was not able to download Avast!, but I tried the rest; they certainly cleaned up a lot of junk, but not whatever this little bastard is. Doing some of my own research suggests this is something very new, that attaches itself to your registry (thus apparently cloaking itself from a lot of antivirus-, etc-ware). Manually trying out regedit, I was able to clean it up some (got rid of some old g a m e s and such, listed there but no longer on my PC), but I could not find the evil files I turned up in my research, which were blackice.exe, blackice.ini, etc. (files attached to something called "win32 maleware.gen"), but I think whatever I've caught is something like those/("it"). I'll watch here for a while for suggestions, and also to see if anyone else has a similar experience. Thanks again, Hamamelis (talk) 09:57, 19 July 2011 (UTC)
- The next thing to do would be to create a HijackThis log and post it on one of the several forums that are dedicated to dealing with them, such as this one: http://www.bleepingcomputer.com/forums/forum22.html after following their instructions. I don't know why, but now the HijackThis download link redirects to something else - doubtless the forums can advise on where to get it from now.
- Apart from that you could try running an anti-malware program in safe mode. I don't know if your computer would allow you to install Avast! in safe mode, but in any case you would need to un-install your existing anti-virus. If you want to do that I would download the Avast! instalation file, unplug your modem, uninstall your existing anti-virus, install Avast!, plug in your modem to allow it to update its files, then tell it to do a boot-time scan. I've also just found this http://forums.majorgeeks.com/showthread.php?t=35407 which may be worth looking at. 2.97.220.86 (talk) 18:11, 19 July 2011 (UTC)
- Thanks a lot for the links and advice 2.97.220.86, will report back here sometime later (but I can't say when). Hamamelis (talk) 00:08, 20 July 2011 (UTC)
- I appreciate all of your suggestions. Alas, to no avail so far. For some reason, I was not able to download Avast!, but I tried the rest; they certainly cleaned up a lot of junk, but not whatever this little bastard is. Doing some of my own research suggests this is something very new, that attaches itself to your registry (thus apparently cloaking itself from a lot of antivirus-, etc-ware). Manually trying out regedit, I was able to clean it up some (got rid of some old g a m e s and such, listed there but no longer on my PC), but I could not find the evil files I turned up in my research, which were blackice.exe, blackice.ini, etc. (files attached to something called "win32 maleware.gen"), but I think whatever I've caught is something like those/("it"). I'll watch here for a while for suggestions, and also to see if anyone else has a similar experience. Thanks again, Hamamelis (talk) 09:57, 19 July 2011 (UTC)
Multiplication and addition of arrays using foreach() loop in PHP
I would like to know the PHP code/program for multiplying and adding two 3x3 arrays using the foreach() loop. I've been trying to do it for days, but nothing seems to work. Somebody please help!!!
I've tried the following code for adding the arrays:
<?php
$a=array(1=>array(1,2,3),2=>array(4,5,6));
$b=array(1=>array(9,8,7),2=>array(6,5,4));
$c=array();
foreach($a as $v1)
{ foreach($b as $v2)
{ $c=$v1+$v2;
}}
print_r($c);
?>
There are no errors, but it doesn't work,either. The output is incorrect. I don't know where I'm going wrong. If someone could tell me what to do, how to go about it,instead of giving me the code directly, that would do as well, just as long as I get to know how to do it. Thanks in advance! Zebec 21:12, 17 July 2011 (UTC)
- You're still trying to add two array types together (both $v1 and $v2 are arrays in your loops). You cannot do that in PHP — it doesn't understand, and it just assigns $c to whatever the first array in your equation is. So the result you're getting, in the end, is the final instance of $a, every time, which is 4,5,6. If you are trying to make the internal values of the array add, you have to do another few foreach loops so that you're dealing with the values, and not arrays themselves. --Mr.98 (talk) 21:35, 17 July 2011 (UTC)
- Here, also, is a re-written version of exactly what you have above. Note two things: 1. I've added some formatting which makes it clear what is going on (an absolutely essential programming habit!), and 2. I've added debugging calls so you can see, when you run it, what's going on. Both of these sorts of things are common when trying to figure out how a piece of code does or doesn't work.
<pre>
<?php
$a = Array(
1 => Array(1,2,3),
2 => Array(4,5,6)
);
echo "a = ";
print_r($a);
echo "\n";
$b = Array(
1 => Array(9,8,7),
2 => Array(6,5,4)
);
echo "b = ";
print_r($b);
echo "\n";
$c=array();
foreach($a as $k1 => $v1) {
foreach($b as $k2 => $v2) {
$c = $v1+$v2;
echo "working on $k1:$k2\n";
echo "v1 = ";
print_r($v1);
echo "\n";
echo "v2 = ";
print_r($v2);
echo "\n";
echo "c is currently = ";
print_r($c);
echo "\n";
}
}
echo "c is finally = ";
print_r($c);
?>
</pre>
- You can see pretty clearly what is and isn't happening that way. --Mr.98 (talk) 01:15, 18 July 2011 (UTC)
- Do you realize that $c is going to be rewritten after each rendition of the inner loop?--v/r - TP 01:24, 18 July 2011 (UTC)
- That's another good point! You're just assigning c again and again, rather than doing something like $c = $c + ($v1+$v2), or, in shorter code, $c+=$v1+$v2. Though that still won't work because of the array issue. --Mr.98 (talk) 15:35, 18 July 2011 (UTC)
Thank you very much Mr.98! I added a few more foreach loops so that the values would be used instead of the arrays, and also added the array_push function so that the sum gets stored in the third array, but because each of the two arrays require two foreach() loops, the third array is becoming longer than it should be. The order of nesting matters, I think, and I'm not able to figure it out. I also need to multiply two arrays in another program, which will be even more complicated. I can start work on that once I've finished this.
<?php
$a=array(1=>array(1,2,3),2=>array(4,5,6));
$b=array(1=>array(9,8,7),2=>array(6,5,4));
$c=array();
foreach($a as $v1)
{ foreach($b as $v2)
{ foreach($v1 as $c1)
{ foreach($v2 as $c2)
{ $d=$c1+$c2;
array_push($c,$d);
}}}}
print_r($c)
?>
Zebec 21:11, 18 July 2011 (UTC)
- I'm still sort of unclear what you are trying to actually do with the arrays. Are you trying to do matrix addition? If so, I think you're going about it wrong. You don't need all of those foreachs. What you need, essentially is something like this: take element 1 of $a, element 2 of $b. Then iterate over the three values in them, adding each of them, and using them to create element 1 of $c. Then repeat for element 2 of $a, $b, and thus $c. $c would then be an array with two elements, which correspond to the additions of the two lines of $a and $b. I think your foreachs are causing you to do too many operations (4 when you should be doing 2). --Mr.98 (talk) 00:31, 19 July 2011 (UTC)
Yes,I am trying to do matrix addition. But the number of elements in $c will be the same as that of $a and $b. For example:
$a= 1 2 3
4 5 6
7 8 9
$b=9 8 7
6 5 4
3 2 1
$c=$a+$b= 10 10 10
10 10 10
10 10 10
Hope you understand what I'm trying to do now. But, accessing one element of a 2D array like $a requires two foreach loops. Then how can I do it if I reduce the number of foreach loops? I know how to do it using for, but I need to do it with foreach. Zebec 20:16, 20 July 2011 (UTC)
- What you want to do is think of it not as going through the values of $a and $b, but as a row/column coordinate scheme. Here's one way. Notice I'm really just using the foreachs to walk me through the dimensions of $a, using the keys as coordinates. It of course assumes $a and $b have the same number of elements — in a robust function, you'd check for that first. Note though that because it is a coordinate-based set of foreachs, it is entirely independent of the size of the array (you could do 2x2 or 3x3 or 4x4 or whatever).
foreach($a as $row=>$rowarray) {
foreach($rowarray as $col=>$val) {
$c[$row][$col] = $a[$row][$col]+$b[$row][$col];
}
}
- It's been awhile since I've done matrix multiplication so it's not obvious to me at the moment (after a long day) how you'd adapt that to multiplication, but it works pretty well for addition, and is simple, to boot! --Mr.98 (talk) 22:48, 21 July 2011 (UTC)
- Thanks a ton Mr.98!! The addition logic above is working perfectly. I really appreciate your trying to help me out after a hard day's work. Thanks a lot!
The logic for multiplication using for is as follows:
<?php
$a=array(1=>array(1,2,3),2=>array(4,5,6),3=>array(7,8,9));
$b=array(1=>array(9,5,4),2=>array(8,6,7),3=>array(5,4,7));
$c=array(array());
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{ $c[$i][$j]=0;
for($k=0;$k<$j;$k++)
{
$m=$a[$i][$k]*$b[$k][$j];
$c[$i][$j]+=$m;
}
}
}
echo "$a=";
print_r($a);
echo "<br> $b=";
print_r($b);
echo "<br> Product=";
print_r($c);
?>
I tried converting it into foreach, and have got the basic logic. Here's the program:
<?php
$a=array(0=>array(1,2,3),1=>array(4,5,6),2=>array(7,8,9));
$b=array(0=>array(9,5,4),1=>array(8,6,7),2=>array(5,4,7));
$c=array();
echo "a=";
print_r($a);
echo "<br>b=";
print_r($b);
foreach($a as $row=>$rowarray)
{
foreach($rowarray as $col=>$val)
{
$c[$row][$col]+=($a[$row][$col]*$b[$col][$row]);
}
}
echo "<br> product=";
print_r($c)
?>
There are two problems:
1. The foreach loop runs only once for each row, but I it need to run thrice (as many times as the number of columns in the first array). If I put a for loop just before or after the second foreach loop, the row counter does not get incremented, but I need it to. How do I go about it?
2.The row and col counters keep changing, so how do I store the result for a particular element into the right location? That is, how can I store the product for a[0][1] into c[0][1]?
So you see, it's almost done,only these two problems need to be resolved. Thank you very much once again for telling me the basic way to traverse a 2D array.
Zebec 21:33, 24 July 2011 (UTC)
Java functions
I am writing a function that I want to return an array of elements of different types. For example, I have a class called "cat" and I want to have a function that returns the cats age and whether it is declawed or not. Age is an integer and declawed is a boolean. I want my method to return both age and declawed in one method as an array. Do I use an ArrayList? How is that used?
public String[] getAgeAndClaws() {
String[] myInfo = {Integer.toString(age), declawed };
return myInfo;
}
This is what I have and I havent compiled it yet, but I dont think it'll work.--v/r - TP 23:52, 17 July 2011 (UTC)
- I don't know if it's the best way, but you can do it with an ArrayList of Object, and then after you receive it from the method you cast each element, but you need to use Integer and Boolean since int and boolean are primitives (hence they don't inherit from Object) - frankie (talk) 00:38, 18 July 2011 (UTC)
// at Cat class
Integer age;
Boolean declawed;
public ArrayList<Object> getAgeAndClaws() {
ArrayList<Object> catData = new ArrayList<Object>();
catData.add(age);
catData.add(declawed);
return catData;
}
// then where you call it
ArrayList<Object> catData = cat.getAgeAndClaws();
Integer age = (Integer) catData.get(0);
Boolean declawed = (Boolean) catData.get(1);
- That works (I didn't post the code above; I believe it was frankie). To the OP: Is there some reason why implementing multiple methods, e.g., getAge(), getDeclawed(), getX(), is unacceptable? Using an ArrayList<Object> and then casting every call to its get() method makes me cringe because it throws type safety out the window, and you could easily trigger several
ClassCastException
s if you're not extra careful as you build on this code (I know it would happen to me!). Perhaps you could tell us more what you're planning to do and we could suggest an alternative.--el Aprel (facta-facienda) 03:34, 18 July 2011 (UTC) - The more I think about it, this is really what objects are for: holding values in memory and providing a useful set of methods for those values. You don't need to put the values into an array; just pass the instance of Cat to wherever you need it and call get() methods for whatever values you need.--el Aprel (facta-facienda)
- I suppose, and I ended up doing it that way. The above was a homework assignment, and actually the requested code was not part of the instruction. I've been a programmer for 15 odd years and never bothered learning Java. The assignment used singular methods to get the information but I've done it this way in other languages before that were less type-sensetive. I appreciate all the help though.--v/r - TP 12:20, 18 July 2011 (UTC)
- The problem is not that you have not learnt Java - actually you do not think in object-oriented terms, which is even worse. Give a look at this page, maybe it can help you.
- By the way, I'm quite dubious about you having been a programmer for 15 years. Returning multiple values from a function is quite a bad idea, and it's widely considered bad practice in any language, even in C. --151.75.17.199 (talk) 23:34, 18 July 2011 (UTC)
- Java and C don't support multiple return values, or provide a nice way to take them. This is certainly not universal. Many languages provide nice features to do this kind of thing. Paul (Stansifer) 08:19, 19 July 2011 (UTC)
- Thanks IP. Actually, I have been programming for 7 years as a hobby and 7 years professionally amounting to 14 years or reasonably rounded to 15. PHP, ASP, ASP.NET, SQL (MSSQL, Oracle, and MySQL), VB6, VB.NET, Javascript, ActionScript 3. I've never had a problem returning multiple values from a function in languages that were less type-sensitive and I've never stumbled across anyone who considered it a bad idea. Your comment makes me question your credentials.--v/r - TP 14:34, 19 July 2011 (UTC)
- Java and C don't support multiple return values, or provide a nice way to take them. This is certainly not universal. Many languages provide nice features to do this kind of thing. Paul (Stansifer) 08:19, 19 July 2011 (UTC)
- The problem is not that you have not learnt Java - actually you do not think in object-oriented terms, which is even worse. Give a look at this page, maybe it can help you.
- I suppose, and I ended up doing it that way. The above was a homework assignment, and actually the requested code was not part of the instruction. I've been a programmer for 15 odd years and never bothered learning Java. The assignment used singular methods to get the information but I've done it this way in other languages before that were less type-sensetive. I appreciate all the help though.--v/r - TP 12:20, 18 July 2011 (UTC)
- That works (I didn't post the code above; I believe it was frankie). To the OP: Is there some reason why implementing multiple methods, e.g., getAge(), getDeclawed(), getX(), is unacceptable? Using an ArrayList<Object> and then casting every call to its get() method makes me cringe because it throws type safety out the window, and you could easily trigger several