I tried do this, but this return "0x0000000000000000000000000000000000000000"
// Convert bytes to address
function fromBytes(bytes32[] _additionalArgs) public view returns (address[]){
address[] memory path = new address[](_additionalArgs.length);
for(uint i = 0; i > _additionalArgs.length; i++){
path[i] = address(_additionalArgs[i]);
}
return path;
}
I need return array with addresses!
Your loop never executes.
for(uint i = 0; i > _additionalArgs.length; i++){
i begins at 0, and the loop condition is i > _additionalArgs.length, which can't ever be true. You almost certainly meant to use < instead:
for(uint i = 0; i < _additionalArgs.length; i++){
With that change, I believe your code should work.
Related
here is my function:
int repeatedNTimes(int* A, int ASize)
{
int i, count, j, temp;
for(i = 0; i < ASize; ++i)
{
count = 0;
temp = A[i];
for(j = i; j < ASize; ++j)
{
if(A[i] == A[j])
count++;
}
if(count == ASize / 2)
return A[i];
else
continue;
}
return 0;
}
Can I use return 1, or return (any integer) instead of return 0?
And secondly, what if I don't return an integer?
If you do not return an integer, then the behavior is not well defined (probably undefined, but I don't have the standard memorized). Your compiler will likely emit a warning if you have warnings on.
As for returning an integer other than 0, yes, you can do that. What matters is the return type of the function when it comes to what you can and cannot return. That said, returning a different result may not have the effect you want depending on what your function does. Sometimes values like zero are reserved for special conditions like not found.
.move is a Math.random() method in an object
After hitting the first object in my array my loop stops, the function works for that object but does not continue to the next 4 objects...
let turns = [];
let count = 0 ;
let steps = 0;
for (let i = 0; i < arr.length; i++){
if (steps <= 20 ){
steps += arr[i].move();
count++;
} else { turns.push(count);
}
}return turns
};```
As I said: Is there any difference (in performance) between these two for loop?
Between this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
var currentObject:DisplayObject = displayObjects_[i];
currentObject.width = newWidth;
}
and this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
displayObjects_[i].width = newWidth;
}
I tested them before. The result said the first one was faster but I don't know if I did it right.
I know it's not really an answer to your question, but if you're looking for fastest way to iterate through this array you should do:
for each(var currentObject:DisplayObject in displayObjects_) {
currentObject.width = newWidth;
}
I tried this out on a bare-bones project using SDK 4.6. This is the code.
public class Main extends Sprite
{
public function Main()
{
var displayObjects_:Array = [];
for (var i:int = 0; i < 1000000; i++)
{
displayObjects_.push(new Sprite());
}
var start:int = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
var currentObject:Sprite = displayObjects_[i];
currentObject.width = 100;
}
var end:int = getTimer()
trace(end, start, end - start);
start = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
displayObjects_[i].width = 100;
}
end = getTimer()
trace(end, start, end - start);
}
}
These are the results.
Done(0)
[Starting debug session with FDB]
16703 16250 453
17141 16703 438
Like I said, it would be very surprising to see any difference between the two. You'll probably see more improvements through using Vector instead of Array. Otherwise, this stuff is too mundane to fuss over.
As I know, Actionscript compiler automatically moves variable definitions to the begin of the function. So, this loop doesn't declare variable each time, first sample the same that:
var currentObject:DisplayObject;
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
currentObject = displayObjects_[i];
currentObject.width = newWidth;
}
So, I think the difference only in one additional variable declare and it willn't affects performance. But 3vilguy's example is better.
So like:
void aLoop(){
int i = 0;
while(i < 10){
aFunction();
i++;
}
}
int aFunction(int i){
if(aVariable == 1){
i = 10;
}
if(aVariable != 1){
statement;
statement;
i = i;
}
return i;
}
Where aFunction() will be called for each i (0,1,2,3,...,9) and for each call will satisfy either the first if statement or the second.
Assuming all functions and variables are declared, would this be able to stop the while loop if aVariable == 1?
How else could you accomplish the same thing?
I'm really inexperienced with programming.
FIXED:
void aLoop(){
int i = 0;
while(i < 10){
i = aFunction(i);
i++;
}
}
int aFunction(int i){
if(aVariable == 1){
i = 10;
}
if(aVariable != 1){
statement;
statement;
i = i;
}
return i;
}
instead of
aFunction(x);
just use
i = aFunction(x);
use return to terminate a method.
USe break to terminate a for/ while loop or in a switch statement.
void aLoop(){
int i = 0;
do{
aFunction();
System.out.print(i+" ");
i++;
}while(i < 10);
}
Your suggested solution under "FIXED" will work but if you wrote a large program using this approach you'd end up with software that would be very complex and very costly to work on. This is because aFunction is dependent on the function aLoop that calls it. Functions should ideally be independent of each other, whereas aFunction only works if it's being called from a while loop. You almost never want a called function to be dependent on the structure of the function that calls it.
Try to code so that the the responsibilities or "intentions" of each part of the program are clear, so that any dependencies are minimal and obvious. E.g. here you could write
void aLoop(){
bool continueProcessing = true;
for(int i=0;
continueProcessing && i < 10;
i++) {
continueProcessing = aFunction(i);
}
}
int aFunction(int i){
bool stillProcessing = aVariable != 1;
if (stillProcessing) {
statement;
statement;
}
return stillProcessing;
}
Of course, in aLoop there are some other options which amount to the same thing. You could carry on with a while loop (I think for is clearer). Also, you could break out of the loop instead of having an extra continueProcessing variable.
void aLoop(){
for(int i=0; i < 10; i++) {
if (!aFunction(i))
break;
}
}
Finally I'm not sure you even need to pass the variable i into aLoop. If you don't, or some other data is more appropriate, it would be better to change this too.
I have a string that contains numbers like this: 2243. What I need to do is get a total of these numbers. So using that example the numbers in the string would be converted to integers and I'd be able to do this: 2+2+4+3 = 11.
Not having coded in quite a while I find myself a bit stumped on this (probably) quite easy task.
All help is much appreciated!
You can do that :
var number:String = "2243"; // Represents your original string number
var result:int = 0;
for (var i:int = 0; i < number.length; i++) {
result += parseInt(number.charAt(i), 10);
}
// result var will now contain your sum
Are you sure all the individual numbers won't reach more than 9?
private var number:String = "2243";
private var result:int = 0;
public function method1(event):void{
for each (var num:String in number.split("")){
result += parseInt(num);
}
}
public function method2(event):void{
for (var i:int = 0; i < number.length; i++){
result += parseInt(number.charAt(i),10);
}
}