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
Ber [7]
3 years ago
6

Write a method named collapse that accepts an array of integers as a parameter and returns a new array where each pair of intege

rs from the original array has been replaced by the sum of that pair. For example, if an array called a stores {7, 2, 8, 9, 4, 13, 7, 1, 9, 10}, then the call of collapse(a) should return a new array containing {9, 17, 17, 8, 19}. The first pair from the original list is collapsed into 9 (7 2), the second pair is collapsed into 17 (8 9), and so on.
Computers and Technology
1 answer:
maxonik [38]3 years ago
8 0

Answer:

The method in Java is as follows:

public static void collapse(int [] myarr) {  

       int [] arr = new int[myarr.length/2];

       int k = 0;

       for (int i = 0;i<myarr.length;i+=2){

           arr[k] = myarr[i]+myarr[i+1];

           k++;

       }

       for (int i = 0;i<myarr.length/2;i++){

           System.out.print(arr[i]+" ");

       }

   }  

Explanation:

This defines the collapse method

public static void collapse(int [] myarr) {  

This declares a new array

       int [] arr = new int[myarr.length/2];

This declares and initializes k to 0

       int k = 0;

This iterates through the array passed as argument

       for (int i = 0;i<myarr.length;i+=2){

This calculates the elements of the new array

           arr[k] = myarr[i]+myarr[i+1];

           k++;

       }

The following iteration returns and prints the elements of the new array

<em>        for (int i = 0;i<myarr.length/2;i++){</em>

<em>            System.out.print(arr[i]+" ");</em>

<em>        } </em>

   

<em>See attachment for complete program which includes the main</em>

Download txt
You might be interested in
What do the power key and refresh key do when pressed together
9966 [12]

Answer:

shuts down the laptop.

Explanation:

i just did it and almost had a heart attack because it wouldn't turn on

3 0
3 years ago
8.10 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's a
Korvikt [17]

Answer:

// This program is written in C++ programming language

// Comments are used for explanatory purpose

// Program starts here

#include <iostream>

#include <string>

using namespace std;

// Declare variables

int inputvar;

// Declare output variable as array

int outputvar[32];

// Set a counter for binary array

int i = 0;

while (inputvar > 0) {

// Divide inputvar by 2 and store remainder in outputvar

outputvar[i] = n % 2;

inputvar/=2;

i++; // increment i by 1

}

// End of division

// Prin resulting array in reverse order

for (int j = i - 1; j >= 0; j--) {

cout << outputvar[j];

}

return 0;

}

// End of Program

3 0
4 years ago
I need this ASAP!
Elanso [62]

Answer:

b

Explanation:

4 0
3 years ago
Read 2 more answers
Define cyber law and cyber crime with examples.​
kifflom [539]

Answer:

Cyber crime is unlawful acts where the computer is either a tool or target or both. An example of such would be theft, fraud, forgery, etc. Cyberlaw on the other hand encompasses cybercrime.

4 0
3 years ago
What are pixels that are the exact same between multiple friends called
erastova [34]

Answer:

The answer is temporal redundancy

Explanation:

Computer Definition. Pixels in two video frames that have the same values in the same location. Exploiting temporal redundancy is one of the primary techniques in video compression (see interframe coding). Contrast with spatial redundancy.

7 0
3 years ago
Other questions:
  • Carmen wanted to clear up space on her computer and remove unwanted files. What would be the best for her to delete?
    6·2 answers
  • If a cheetah covers 155 meters in 5 seconds, at what speed is the cheetah running?
    14·2 answers
  • A computer that no longer works after having minor repair work done to it may have been damaged by ____
    13·2 answers
  • Question #2
    6·2 answers
  • Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer va
    7·1 answer
  • Which hexadecimal number is equivalent to the decimal number 11?
    13·1 answer
  • Why is it important to have at least one backup stored off-site?
    8·1 answer
  • How to run android apps on a chromebook?
    15·1 answer
  • The gradual wearing away or breaking down of rocks by abrasion is a type of __________________ weathering.
    8·1 answer
  • Kareem is working on a project for his manager. He has a few questions for a co-worker who he knows is knowledgeable on the subj
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!