Notice error while counting number occurences
I'm creating a simple .php script that
- Generates an array with random numbers (let's say 1000 integers)
- Outputs an image, where it displays all these numbers' arrangement on a scale from 0 to the largest number drawn
The output is an image (with given width), this is what I got:
As you can see, at some points the bars are higher. It's because the image usually isn't wide enough to present all the numbers, so if there are more numbers laying on one pixel, then the bar is going higher.
The problem: Sometimes this script works well, but occasionally I get numerous Notice errors:
Notice: Undefined offset: 398 in /.../index.php on line 61
Notice: Undefined offset: 125 in /.../index.php on line 61
Notice: Undefined offset: 192 in /.../index.php on line 61 ...
The script is below, some explainations:
- Line 61 marked red (below)
- function toScale() returns intval( number / maxNumber * scaleWidth )
- inb4 unnecessary double declaration of $counts[num]=0, that's what I've done trying to solve the problem, it doesn't seem to change anything
$counts isn't used anywhere else in between these two parts- the number of Notice errors is never equal to the total numbers count, it's either a few or none of these.
Update: As Barmar mentioned, the second loop from my code can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
It looks like just code simplification, but somehow this solves the problem. Any ideas why?
php php-gd e-notices
|
show 3 more comments
I'm creating a simple .php script that
- Generates an array with random numbers (let's say 1000 integers)
- Outputs an image, where it displays all these numbers' arrangement on a scale from 0 to the largest number drawn
The output is an image (with given width), this is what I got:
As you can see, at some points the bars are higher. It's because the image usually isn't wide enough to present all the numbers, so if there are more numbers laying on one pixel, then the bar is going higher.
The problem: Sometimes this script works well, but occasionally I get numerous Notice errors:
Notice: Undefined offset: 398 in /.../index.php on line 61
Notice: Undefined offset: 125 in /.../index.php on line 61
Notice: Undefined offset: 192 in /.../index.php on line 61 ...
The script is below, some explainations:
- Line 61 marked red (below)
- function toScale() returns intval( number / maxNumber * scaleWidth )
- inb4 unnecessary double declaration of $counts[num]=0, that's what I've done trying to solve the problem, it doesn't seem to change anything
$counts isn't used anywhere else in between these two parts- the number of Notice errors is never equal to the total numbers count, it's either a few or none of these.
Update: As Barmar mentioned, the second loop from my code can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
It looks like just code simplification, but somehow this solves the problem. Any ideas why?
php php-gd e-notices
4
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
1
Your second loop can be replaced with$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Do you modify$data
between the two loops?
– Barmar
Nov 16 '18 at 0:51
1
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58
|
show 3 more comments
I'm creating a simple .php script that
- Generates an array with random numbers (let's say 1000 integers)
- Outputs an image, where it displays all these numbers' arrangement on a scale from 0 to the largest number drawn
The output is an image (with given width), this is what I got:
As you can see, at some points the bars are higher. It's because the image usually isn't wide enough to present all the numbers, so if there are more numbers laying on one pixel, then the bar is going higher.
The problem: Sometimes this script works well, but occasionally I get numerous Notice errors:
Notice: Undefined offset: 398 in /.../index.php on line 61
Notice: Undefined offset: 125 in /.../index.php on line 61
Notice: Undefined offset: 192 in /.../index.php on line 61 ...
The script is below, some explainations:
- Line 61 marked red (below)
- function toScale() returns intval( number / maxNumber * scaleWidth )
- inb4 unnecessary double declaration of $counts[num]=0, that's what I've done trying to solve the problem, it doesn't seem to change anything
$counts isn't used anywhere else in between these two parts- the number of Notice errors is never equal to the total numbers count, it's either a few or none of these.
Update: As Barmar mentioned, the second loop from my code can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
It looks like just code simplification, but somehow this solves the problem. Any ideas why?
php php-gd e-notices
I'm creating a simple .php script that
- Generates an array with random numbers (let's say 1000 integers)
- Outputs an image, where it displays all these numbers' arrangement on a scale from 0 to the largest number drawn
The output is an image (with given width), this is what I got:
As you can see, at some points the bars are higher. It's because the image usually isn't wide enough to present all the numbers, so if there are more numbers laying on one pixel, then the bar is going higher.
The problem: Sometimes this script works well, but occasionally I get numerous Notice errors:
Notice: Undefined offset: 398 in /.../index.php on line 61
Notice: Undefined offset: 125 in /.../index.php on line 61
Notice: Undefined offset: 192 in /.../index.php on line 61 ...
The script is below, some explainations:
- Line 61 marked red (below)
- function toScale() returns intval( number / maxNumber * scaleWidth )
- inb4 unnecessary double declaration of $counts[num]=0, that's what I've done trying to solve the problem, it doesn't seem to change anything
$counts isn't used anywhere else in between these two parts- the number of Notice errors is never equal to the total numbers count, it's either a few or none of these.
Update: As Barmar mentioned, the second loop from my code can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
It looks like just code simplification, but somehow this solves the problem. Any ideas why?
php php-gd e-notices
php php-gd e-notices
edited Nov 16 '18 at 1:00
Lis
asked Nov 16 '18 at 0:34
LisLis
311212
311212
4
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
1
Your second loop can be replaced with$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Do you modify$data
between the two loops?
– Barmar
Nov 16 '18 at 0:51
1
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58
|
show 3 more comments
4
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
1
Your second loop can be replaced with$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Do you modify$data
between the two loops?
– Barmar
Nov 16 '18 at 0:51
1
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58
4
4
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
1
1
Your second loop can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Your second loop can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Do you modify
$data
between the two loops?– Barmar
Nov 16 '18 at 0:51
Do you modify
$data
between the two loops?– Barmar
Nov 16 '18 at 0:51
1
1
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58
|
show 3 more comments
1 Answer
1
active
oldest
votes
The problem is that you're scaling your numbers differently in the two loops.
In the first loop, you're updating $max
each time through the loop, and then calling toScale(data[$x])
. Since toScale()
depends on $max
, some numbers will be scaled to a different maximum.
In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a]
you'll get a different toScale($data[$a])
this time.
You need to calculate the maximum of all the numbers before any calls to toScale
. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:
$max = 0;
for ($a = 0; $a < $num; $a++) {
$rand = rand();
if ($rand > $max) {
$max = $rand;
}
$data = $rand;
}
Or you can do it using a built-in function:
$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329790%2fnotice-error-while-counting-number-occurences%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you're scaling your numbers differently in the two loops.
In the first loop, you're updating $max
each time through the loop, and then calling toScale(data[$x])
. Since toScale()
depends on $max
, some numbers will be scaled to a different maximum.
In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a]
you'll get a different toScale($data[$a])
this time.
You need to calculate the maximum of all the numbers before any calls to toScale
. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:
$max = 0;
for ($a = 0; $a < $num; $a++) {
$rand = rand();
if ($rand > $max) {
$max = $rand;
}
$data = $rand;
}
Or you can do it using a built-in function:
$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
add a comment |
The problem is that you're scaling your numbers differently in the two loops.
In the first loop, you're updating $max
each time through the loop, and then calling toScale(data[$x])
. Since toScale()
depends on $max
, some numbers will be scaled to a different maximum.
In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a]
you'll get a different toScale($data[$a])
this time.
You need to calculate the maximum of all the numbers before any calls to toScale
. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:
$max = 0;
for ($a = 0; $a < $num; $a++) {
$rand = rand();
if ($rand > $max) {
$max = $rand;
}
$data = $rand;
}
Or you can do it using a built-in function:
$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
add a comment |
The problem is that you're scaling your numbers differently in the two loops.
In the first loop, you're updating $max
each time through the loop, and then calling toScale(data[$x])
. Since toScale()
depends on $max
, some numbers will be scaled to a different maximum.
In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a]
you'll get a different toScale($data[$a])
this time.
You need to calculate the maximum of all the numbers before any calls to toScale
. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:
$max = 0;
for ($a = 0; $a < $num; $a++) {
$rand = rand();
if ($rand > $max) {
$max = $rand;
}
$data = $rand;
}
Or you can do it using a built-in function:
$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
The problem is that you're scaling your numbers differently in the two loops.
In the first loop, you're updating $max
each time through the loop, and then calling toScale(data[$x])
. Since toScale()
depends on $max
, some numbers will be scaled to a different maximum.
In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a]
you'll get a different toScale($data[$a])
this time.
You need to calculate the maximum of all the numbers before any calls to toScale
. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:
$max = 0;
for ($a = 0; $a < $num; $a++) {
$rand = rand();
if ($rand > $max) {
$max = $rand;
}
$data = $rand;
}
Or you can do it using a built-in function:
$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
answered Nov 16 '18 at 0:57
BarmarBarmar
433k36257359
433k36257359
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329790%2fnotice-error-while-counting-number-occurences%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Nov 16 '18 at 0:43
1
Your second loop can be replaced with
$max = max($data); $counts = array_count_values(array_map('toScale', $data));
– Barmar
Nov 16 '18 at 0:45
Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work?
– Lis
Nov 16 '18 at 0:50
Do you modify
$data
between the two loops?– Barmar
Nov 16 '18 at 0:51
1
How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior.
– Kita
Nov 16 '18 at 0:58