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

(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...
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.
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?
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++
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...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT