answersLogoWhite

0


Best Answer

Java solution

public static final int[] getMiddle(final int[] a, final int[] b) {

return new int[] {a[1], b[1]};

}

C solution void getMiddle(const int a[], const int b[], int ret[]) {

ret[0] = a[1];

ret[1] = b[1];

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Given 2 int arrays a and b each length 3 return a new array length 2 containing their middle elements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is array data?

Basically it's a list. Instead of typing x0, x1, x2, x3, x4... x99, an array lets you just type x[100]. Then to use each variable you just type x[number] and use it as normal. Arrays are so useful because you can iterate, or repeat, over them easily. You could define an array, then define an integer i. They you could make a loop in your program that increases each x value by 2. Just make it so I gets bigger each run of the loop and type this: x[i] = x[i] + 2 A Note: if you define the array x to have 100 values, than it will go from 0 to 99, not 1 to 100. There are also more advanced arrays that can get bigger as you need them to. They will grow without you thinking about it. You can also add data in the middle, and have all the data after it shifted one place. Or you could delete a piece of data from the middle and have all data after it shifted back one place. These arrays are generally called mutable (which means changeable) arrays. Normal arrays are called immutable arrays.


Write a program in c to find out the middle character of a word?

This kind of exercise requires a minimal amount of mathematics and logic. In C, strings are character arrays, and the variables used to contain strings actually contain the location (or "address") of the first character. The last character in the string is ASCII 0 (NUL), which signals the end of the string. This problem requires you first know how long the string is, and for this purpose you would use "strlen". Assuming you have an integer variable called "length" and a string called "str", this assignment would store the length of "str" into "length": length=strlen(str); From there, the middle character is located at the halfway point in the array. So, if you had an integer variable called "halfpos", then this assignment would place the middle position of the string in "halfpos": halfpos=length/2; Note that strings with an even number of character (2, 4, 6, 8, 10, etc.) actually have two middle characters. So "halfpos" would actually contain one character less than what's considered the "middle" of the string. Examples of this are: - "Grape" contains five characters, ranged 0 to 4, so "halfpos" is 2, or "a" - although 5/2 is 2.5, because "length" and "halfpos" are integers that .5 is stripped off. - "Tomato" contains six characters, ranged 0 to 5, so "halfpos" would be the point between the "m" and the "a". To write the code for this exercise, you will need to know more about arrays. See the related links below for more help.


How do you write a program in c plus plus to swap each half of an array?

Swapping array elements is simple enough. The only issue is what to do with the middle element when there are an odd number of elements. The following program simply leaves it where it is. #include<iostream> #include<vector> template<typename T> void exchange (std::vector<T>& v) { if (v.size()<2) return; size_t left = 0; size_t right = v.size() / 2; if (v.size()%2) ++right; // skip middle element while (right < v.size()) std::swap (v[left++], v[right++]); } int main() { std::vector<unsigned> v {4, 8, 15, 16, 23, 42, 69}; for (auto n : v) std::cout << n << ' '; std::cout << std::endl; exchange (v); for (auto n : v) std::cout << n << ' '; std::cout << std::endl; }


Which algorithm is preferable to find a specific number from unsorted array?

If the array is unsorted then there is no algorithm that can be applied other than a sequential traversal from one end of the array to the other until the number is found or the end of the array is reached. This is an extremely poor algorithm as the worst case would be O(n) for n elements. While this may be fine for small arrays, it is highly inefficient for larger arrays. To apply a more efficient algorithm with a worst case of O(log n), you must sort the array. You can then use a binary search algorithm by starting at the middle element. If that's your value, you're done. If not and the value you seek is less than this value, then you can eliminate all the elements in the upper half of the array, otherwise you can eliminate all the elements in the lower half of the array. Repeat the process with the remaining subarray, reducing the number of remaining elements by half each time. If you end up with a subarray with no elements then the value does not exist. Sorted arrays that are perfectly balanced offer the best performance. For instance, an array with 63 elements is perfectly balanced because after the first iteration you are left with another perfectly balanced array of 31 elements (because you eliminated 31 elements plus the middle element). After the next iteration you will be left with 15 elements, then 7, 3, 1 and finally 0. Thus a 63-element array takes no more than 6 iterations to determine that a value does not exist, compared to 63 iterations with a sequential traversal search upon an unsorted array. 6 iterations is also the average because the 6th iteration can locate any one of 32 values, which is more than half the values.


Which is faster array or arraylist?

Array Lists and Arrays can be compared to one another. Actually an Array List is nothing but a growable array that provides us with a lot more features than traditional Arrays. Apart from this - they are both same - used to hold a group of java objects.

Related questions

What are the elements in the middle of the periodic called?

transition elements


What is the middle number in set of data?

The median. If there are an odd number of elements in the set, there is a middle number which is the median. If there are an even number of elements in the set, the median is the mean of the middle two numbers.


What is array data?

Basically it's a list. Instead of typing x0, x1, x2, x3, x4... x99, an array lets you just type x[100]. Then to use each variable you just type x[number] and use it as normal. Arrays are so useful because you can iterate, or repeat, over them easily. You could define an array, then define an integer i. They you could make a loop in your program that increases each x value by 2. Just make it so I gets bigger each run of the loop and type this: x[i] = x[i] + 2 A Note: if you define the array x to have 100 values, than it will go from 0 to 99, not 1 to 100. There are also more advanced arrays that can get bigger as you need them to. They will grow without you thinking about it. You can also add data in the middle, and have all the data after it shifted one place. Or you could delete a piece of data from the middle and have all data after it shifted back one place. These arrays are generally called mutable (which means changeable) arrays. Normal arrays are called immutable arrays.


Bulky middle coat containing smooth muscle and elastin?

Tunic Media


How many elements do you learn in Middle school?

I believe that its 16 elements, but that changes in high school.


What are the elements in the middle of the periodic table are known as these kinds of metals?

Transition metals


What is a 70's song with lyrics containing middle of the night?

Climax Blues Couldn't Get It Right. Sounds like middle of the night.


What are elements in the middle of the periodic table?

transition metals


What is the name of the elements in the middle of the periodic table?

they are the metals


How is the midpoint of a segment different from the length of a segment?

Mostly because, assuming it just one segment, a midpoint by definition (mid- means middle) is the point at the exact middle of a line segment; whereas the length of the segment is the entire length of the segment. Pretty much, a midpoint is a point in the middle of the line, the length is the measurement of the same line.


What is the length of the middle A string in a piano?

45 centimeters


What is the average length of a middle size car?

4500mm