I'm working on an exercise in Swift 3 Playground.
I have an array called sums with a bunch of numbers in. I want to cycle through each of the array items and print 'The sum is: x' but I'm getting a generic error with the print command.
var i = 0
repeat {
print ("the sum is: \(sums[i])")
i = i + 1
} while i <= sums.count
Does anyone know what I'm doing wrong?
It must be done in a repeat loop as that's what the exercise is asking for.
sums.count will give you the size of the array.
Arrays are 0-indexed in Swift. You're accessing out of the array range.
Check for sums.count - 1 or:
var i = 0
repeat {
print ("the sum is: \(sums[i])")
i = i + 1
} while i < sums.count
Related
Like the title says, my total array won't add any of the values taken from any of my distance arrays and would only return a 1. My total array wont also recognize any i values and seem to completely ignore it.
Any help would be appreciated for giving me advice.
perms_x = perms(x)
perms_y = perms(y)
lngth = length(x)
fctrl = factorial(lngth)
total = zeros(fctrl) //initializes a zero array using factorial as the number of elements
for i=1:fctrl //loop that goes from 1 to factorial value
for k=1:lngth //loops from the start of the array until the length
distance(k) = sqrt((perms_x(i,k)-perms_x(i, k+1))^2 + (perms_y(i, k+1)-perms_y(i, k+1))^2);
total(i) = distance(k) + sum(i);
end
//adds the value of city 1 to 0
distance_1(i) = sqrt(perms_x(i,1)^2+perms_y(i,1)^2);
//adds the value of city n to 0
distance_n(i) = sqrt(perms_x(i,lngth)^2+perms_y(i,lngth)^2);
//adds both values of city 1 and city n to the current sum
total(i) = total(i) + distance_1(i) + distance_n(i);
end
disp (total)
g=min(total)
Looking at the man page of perms show that perms_x and perms_y have a size of lngth!-by-lngth. But since your inner loop goes from 1 to lngth, Scilab raised an out-of-bounds error when you extracted the lngth+1 column.
Inner loop should be
for k=1:lngth-1
I have:
def third_greatest(nums)
highest_num_array = []
highest_num = 0
i = 0
while highest_num_array.length < 3
while i < nums.length
if nums[i] > highest_num
highest_num = nums[i]
end
i = i + 1
end
nums.delete(highest_num)
highest_num_array.push(highest_num)
end
return highest_num_array[2]
end
I keep getting the highest number. What am I doing wrong?
highest_num is not renullified after each of the outer loops. Therefore it will remain the highest number in the list and will just get pushed in the array three times.
If you put the
highest_num = 0
after
while highest_num_array.length < 3
it should work.
Even then, there are a lot of problems with your code:
It doesn't look like Ruby at all.
You are mutating the list that was passed as argument.
You don't handle the case when values can be repeating.
You assume numbers to be > = 0.
All you wanted was
numbers.max(3).last
I am just learning matlab now. I faced a difficulty in creating an array of 3 elements in a row.
I wrote a code
Source = randi ([0,1],1,3);
which gave me output
[1,1,0].....
[0,1,1]....
but I was willing to get only one 1 and two zeros in the output instead of getting two 1 and one zero.
I know I am wrong because I am using randi function and gives random value of 0 & 1 and output I get can be [0,0,1] ... [1,0,0]... too.
My clear problem is to only get only one 1 if I repeat as many times. e.g. I should get only [0,0,1] or [0,1,0] or [1,0,0].
Hope I can get solution.
Thank you.
Ujwal
Here's a way using randperm:
n = 3; %// total number of elements
m = 1; %// number of ones
x = [ones(1,m) zeros(1,n-m)];
x = x(randperm(numel(x)));
Here is a couple of alternative solutions for your problem.
Create zero-filled matrix and set random element to one:
x = zeros(1, 3);
x(randi(3)) = 1;
Create 1x3 eye matrix and randomly circshift it:
x = circshift(eye(1,3), [0, randi(3)]);
I'm having some trouble with the exercise I got given from my teacher.
Exercise:
Write a program to input 5 numbers. Ask the user to a input a number for searching the array. The program should search for this number and tell the user if it has been found in the array or not. For example, if it has been found then the position of the array which the number occupies should be display. For example "Your number is 6. It has been fond in the position 3 of the list."
Obviously, I can just use a for loop and get 5 numbers and put them into the array. But Im not sure how to check if then the number the user wants to search for is in the array.
Heres my attempt http://pastebin.com/t2DcdSvU Im not sure how to put it into code tags :S
First, obtain you user input. So let's say you have your array, and a target value. For the example, let's just say your user input created the following:
Dim numbers = {1, 2, 9, 6, 4}
Dim target = 2
Now all you need to do is loop through the array, and compare the target, to the current value of the array.
For x = 0 To 4
If target = numbers(x) Then
MsgBox "Your number is " + target ", found at position " + x
Exit For
End If
Next x
You can use that same concept to search the array.
Assuming you won't have a sorted array, you can simply use a for loop to check each value of the array and compare with the entered value to search.
Use a for loop or whatever construct you want to populate the array, then use another to loop through the array and for each value, do a compare and determine if the user entered a number that's in the array.
If you get a match, print out the resulting index number and return.
Here's a sample of code that will do what you need:
Dim value As Integer
value = 0
' This loop goes from 0 to 4.
For index As Integer = 0 To 4
value = myArray(index)
' Exit condition if the value is the user number.
If (value = usernum) Then
Console.writeline("Your number was " & usernum & " found at: " & index & "\n")
Exit For
End If
Next
How can i Plot an array in a graph against its order in array in matlab ??
Example : x= [6,10,12,20] point 1 become 6:1 and point 3= 12:3 for example even after i remove some elemets from this array i want to preserve same order
example of what am trying to do here is to remove all values bellow mean while keep order cause it represent the time in sec's (this is from video processing code):
m=mean(amp);
for i=totalframes
if (amp(i) >= m)
time(i)=i/framerate;
end
end
amp(amp >= m) = [];
time(time > 0) = [];
figure, plot(time,amp) %% plot my curve
P.s: time and amp array was created by Zeros earlier in my code..
Thanks
If you want to remove all values below the mean m you should do:
inds=(amp<m) %% find where amp is lower than m
amp(inds) = []; %% remove corresponding amp
time(inds) = []; %% remove corresponding time
figure, plot(time,amp)
Change design of x from [6; 10; 12; 20] to [1 6; 2 10; 3 12; 4 20] and traverse this array deleting rows that you dont want then you will have result array with indexes.