1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kryger [21]
4 years ago
7

Write a program called interleave that accepts two ArrayLists of integers list1 and list2 as parameters and inserts the elements

of list2 into list1 at alternating indexes. If the lists are of unequal length, the remaining elements of the longer list are left at the end of list1.
Computers and Technology
1 answer:
nata0808 [166]4 years ago
7 0

Answer:

Explanation code is given below along with step by step comments!

Explanation:

// first we create a function which accepts two ArrayList of integers named list1 and list2

public static void interleave(ArrayList<Integer> list1, ArrayList<Integer> list2)

{

// we compare the size of list1 and list2 to get the minimum of two and store it in variable n

   int n = Math.min(list1.size(), list2.size());

   int i;

// here we are getting the elements from list2 n times then we add those elements in the list1 alternating (2*i+1)

   for (i = 0; i < n; i++)

     {

       int x = list2.get(i);

       list1.add(2 * i + 1, x);

      }

// if the size of list1 and list2 is same then program stops here else we need to append extra elements at the end of list1

// then we check if the size of list2 is greater than list1 then simply add the remaining elements into list1

   if (i < list2.size())

{

       for (int j = i; j < list2.size(); j++)

           {

                list1.add(list2.get(j));

            }  

     }  

}

Sample Output:

list1=[1, 2, 3]

list2=[5, 6, 7, 8, 9]

list1=[1, 5, 2, 6, 3, 7, 8, 9]

You might be interested in
Which of the following postfix expressions corresponds to the given infix expression?
Ivan

Answer:

C) 1 4 2 / + 1 + 2 + 3 * 2 /

Explanation:

Infix, Postfix and Prefix are 3 different ways of writing expressions which differ in relative placement of operator and operands.

In Infix , operator lies between the operands whereas in postfix operator is written after its operands.

For example: 1 + 2 is infix while 1 2 + is the corresponding postfix expression.

Converting the given infix expression (1 + 4 / 2 + 1 + 2) * 3 / 2 to postfix:

Let (1 + 4 / 2 + 1 + 2) be represented by x.

Converting x to postfix:

(1 + 4 / 2 + 1 + 2)  =>  1 4 2 / + 1 + 2 +       ------------------------- (1)

So the overall infix expression becomes:

x * 3 / 2

Converting it to postfix:

x 3 * 2 /

Replacing the value of x from (1)

1 4 2 / + 1 + 2 + 3 * 2 /

4 0
3 years ago
Count positive and negative number and compute the average. The program will have the user input an unspecified number of intege
Alla [95]

Answer:

The solution code is written in Python 3

  1. total = 0
  2. count = 0
  3. neg = 0
  4. pos = 0
  5. num = int(input("Enter an integer: "))
  6. while(num != 0):
  7.    total += num  
  8.    count += 1
  9.    if(num < 0):
  10.        neg += 1
  11.    else:
  12.        pos += 1
  13.    num = int(input("Enter an integer: "))
  14. print("The number of positives: " + str(pos))
  15. print("The number of negatives: " + str(neg))
  16. print("The total is " + str(total))
  17. print("The average is " + str(total/count))

Explanation:

Firstly, we create four variables, <em>total</em> , <em>count,</em> <em>neg</em> and <em>pos </em>(Line 1- 4). This is to prepare the variable to hold the value of summation of input integer (<em>total</em>), total number of input number (<em>count</em>), total negatives (<em>neg</em>) and total positives (<em>pos</em>).

Next, we prompt user for the first integer (Line 6).

Create a sentinel while loop and set the condition so long as the current input number, <em>num</em> is not equal to zero. the program will just keep adding the current <em>num</em> to total (Line 9)  and increment the count by one (Line 10).

if <em>num</em> smaller than zero, increment the <em>neg</em> by one (Line 13) else increment the <em>pos </em>by one (Line 15). This is to track the total number of positives and negatives.

Finally, we can display all the required output (Line 20 - 23) using the Python built-in function <em>print()</em>  when user enter 0 to terminate the while loop. The output shall be as follows:

The number of positives: 3

The number of negatives: 1

The total is 5

The average is 1.25

3 0
3 years ago
Question # 4
bezimeni [28]

Answer:

the last one I think not 100% sure

6 0
4 years ago
A MAC Frame format has a Preamble of 7 octet pattern in the form of alternating 1s and 0s used by a receiver to establish synchr
Dmitrij [34]

Answer:

The Manchester encoding to describe the first octet pattern signal within the preamble is 10101010

Explanation:.

Solution

Given that:

The first octet value of Preamble is 10101010

The Manchester encoding to outline or describe the first octet of the signal pattern produced or created within the preamble is 10101010

Note: Kindly find an attached image of the first octet signal pattern produced below.

6 0
3 years ago
Describe email etiquette and netiquette when communicating electronically.
navik [9.2K]

Answer:

Email etiquette refers to the principles of behavior that one should use when writing or answering email messages. It is also known as the code of conduct for email communication. Email etiquette depends upon to whom we are writing- Friends & Relatives, Partners, Customers, Superior or Subordinates

Difference Between Etiquette and Netiquette is that Etiquette is the simple requirements needs for social behavior while Netiquette, is the code of acceptable behaviors users should follow while on the Internet.

6 0
3 years ago
Other questions:
  • A rectangular box that displays information or a program is called
    13·1 answer
  • A benefit of IPsec is __________.
    8·1 answer
  • 1. The acronym AUDIENCE serves to remind you of what to consider when analyzing an audience. The U for understanding. How does t
    15·1 answer
  • People are able to predict future events to some extent because: (Select all that apply)
    11·1 answer
  • A or an is a simple chip with two or more processor core
    5·1 answer
  • Identify a true statement about heuristics: a. They are more complicated than algorithms. b. Unlike algorithms, they do not perm
    15·1 answer
  • O O O O O O O
    9·1 answer
  • The _____ method randomly rearranges the items in the list provided in the parentheses.
    5·2 answers
  • Only need help on f and correct me if im wrong for the other questions please
    11·1 answer
  • Who is better, Tom, Ben, Hank, Angela, or Ginger
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!