What does &array[element] means and why? - c

I was coding in MPI using C. I don't understand how the MPI_Send() works or if maybe &array[element] works.
MPI_Send(&array[element],element_left,MPI_INT,i,0,MPI_COMM_WORLD);
here array[]={1,2,3,4,5,6,7,8,9,10} and element = 6 and element_left = 4. I understand array[element]=array[6]=7 but why this function picks 7,8,9,10? I know it will pick 4 elements from the array but why do we need & here and by only giving starting entry array[6] how is this function able to pick the next 3 as well?
I thought I have to add one after another using a for loop or something, but when I searched something on Google I got this code and after going through so much I still didn't understand. Please help me understand the backwardness of this code.

&array[element] is the same expression as array + element and means the address of the elementth element of the array array.
The function you call wants this address as the first argument, and takes the number of elements to process as the second argument.

Most MPI routines take a trio of arguments:
address of buffer
count of elements
datatype of elements
So by &array[element],element_left,MPI_INT you specify the elements element as the start of the buffer, and then you take element_left many integers to send. Kinda strange that you name the count element_left which is more like a name for an index, but that's what happens.

Related

replacing the value of an element in an array without creating a new array

I'm learning Ruby and have been practicing by solving problems on Codewars and Leetcode. I've come across this problem in Leetcode where it is asking me to, given an array and a value, modify the array in place by removing an occurrence of the value given in the array. Pretty simple! I was able to solve it- but, this curious thing happened and I don't know why!
Here's my code:
def remove_element(nums, val)
nums.each_with_index do |num, index|
if num == val
nums[index] = nil
end
end
nums.compact!
nums.length
end
You can see here that on line 4 I've written "nums[index] = nil", and this worked just fine for me. However, for the longest time I was trying to solve the challenge by writing "num = nil". What doesn't make sense to me is, why does "nums[index]" work and not "num"? Don't they refer to the same thing?
Answer from Dave Newton:
num is a block-local variable, nums is the array. Modifying a local parameter is different than accessing a reference. As another example, say that the array was filled with objects. num.some_property = 5 would modify the property of the array entry, num = SomeNewObject.new would just create a new object and not modify the array entry. Same thing would happen if you were calling a function.

Is there a way to sort a no. of character arrays in alphabetical order without using the #include<string.h> or #include<stdlib.h>?

So, I have tried to do the same in a case of array of structures where 'char name[100]' is the only data member.
1st part of the code
2nd part of the code
The problem that I have encountered here is that once I provide a no. of names during program runtime, the output screen either does not print anything afterwards, or, prints the data without sorting it.
output screen
I did not get any compile time errors so I believe that there is a flaw in the logic.
There's this another method I tried hoping to get positive results. I type-casted characters to integers hoping that ASCII values could be used to compare. But, the results are exactly the same (undesired results).
updated logic of the 2nd part of the code
I hope somebody helps me find a way to correct this logic or provide another logic that is efficient.
the sorting logic you used is good , but from what is see the use of function's in C need's to be provided by pointers. other wise all the data inside the function will born and die inside the function , and the all the variables in the Main will stay the same as given, that explains why the output is the same as the input
try to print inside the sorting function's to see if this is the problem.

How do I replace an array element in LabView? (2d array of pictures)

so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.

Getting the first element in an array/any collection

So the other day I had my colleague review my code and he saw that I was using array[0], in Java terms this is basically getting the first element of the array. I did this several times for different purposes, all of which is to get the first element in an array/collection, for example list.get(0), to which he strongly disagreed with.
His argument was that somebody from non-programming background would have problem understanding it and using 0 in such cases is basically hard-coding, which is bad practice. I google-ed several times and all suggestions to getting the first element in an array or any collection is providing them the index, which is 0 in this case.
Could anyone provide me with a suggestion on getting the first element in a meaningful way?
Try using linkedlist's getfirst method to get the first element of the list.
If you were to use an ArrayList is backed by an array and hence its perfectly valid to use index as 0 to get first element.

Do arrays in AS2 have to be reset to the beginning when re-used?

I am working with an older and undocumented set of ActionScript (AS2) and I have found that an array, when looping through it the second time, does not give the proper results. It has been a while since I used ActionScript - does the array need to be reset before the second time through another for loop?
For instance PHP has reset() which returns the array's pointer back to the first item in the array.
There's is no such thing as pointers in ActionScript.
You can target each and every item in an Array by simply targeting it with myArray[index], and no pointer needs to be reset to be able to re-read it.
If your two loops produce different results, I would suggest looking into code that could change anything in it between the two loops or in the first one.
Maybe you could post it here ?

Resources