Question

In: Computer Science

Write an algorithm which has a pre-populated array an array_input of array type and separates it,...

Write an algorithm which has a pre-populated array an array_input of array type and separates it, every other character, into two separate arrays called array1 and array2.

Solutions

Expert Solution

Algorithm:

  1. Populate a list with characters. Suppose a=['a','b','c','d','e','f']. Now we need to split this list into two consecutive lists.
  2. So, we will capture the length of the array or list.
  3. After getting the length of the array we will assign each array with the number of elements that are present in the first array. Say, if an array consists of 6 elements than we will fun a for( x =1; x<=6;x++) then add array1[x] = array_input[x].
  4. Similarly, we will repeat the third step again so that array2[x] = array_input[x].
  5. Once all the elements are added then we can print out the array with the help of a for loop and then display them on the screen.

Hence, this is the algorithm for the above program.

Solution:

I would like to make a python program for it for just the example as we can easily write the same code in any language applicable. Please find the code below:

main.py

def split_array(array_input):

lnt= len(array_input)

array1 = array_input[0:lnt]

array2 = array_input[0:lnt]

return array1,array2

array_input = ['a','b','c','d','e','f']

array1, array2 = split_array(array_input)

print(array1)

print(array2)

Indentation

Output


Related Solutions

In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
What type of algorithm is the Quicksort algorithm if it has random pivots?
What type of algorithm is the Quicksort algorithm if it has random pivots?
Q1. Write an algorithm to do the following operation on an array passed as parameter: If...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If an element of the array is having more than one occurrence, then keep the first occurrence and remove all other occurrences of the element.
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
Write the algorithm and code that creates an array of link lists where the user determines...
Write the algorithm and code that creates an array of link lists where the user determines the number of rows and the number of nodes for each row. Needs to be in C++ language. IT IS DUE TONIGHT AT 11:59. HELP PLEASE Two-Dimensional Array Example: [0] [1] [2] [3] [0] 2 4 6 8 [1] 1 3 [2] 8 4 6 [3] 5
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT