# Tasks solving using one-dimensional arrays. Examples

This topic provides examples of task solving on one-dimensional arrays in JavaScript. The topic reveals the basic operations of creating array, allocating memory for array, processing array, and displaying array.

### Contents

Search other resources:

##### 1. Entering and processing the entered numbers from the keyboard. Array formation. Carrying out calculations

Task. Numbers are entered from the keyboard. End of input – number 0. Form a one-dimensional array of input numbers. Calculate the sum and arithmetic mean of the array elements.

Solution.

A do-while loop is best for entering and manipulating numbers.

```// One dimensional arrays. Tasks solving.

// Task. Numbers are entered from the keyboard.
// Form the array of entered numbers.
// Calculate the sum and arithmetic mean of the array elements.

// 1. Declaring an empty array
var A = []

// 2. Entering numbers in a loop and forming the array
var num

do
{
num = prompt("num = ")
if (num!=0)
A += parseFloat(num) // add number to array
} while (num!=0);

// 3. Output the input array A

document.write("A:<br>")
for (var i=0; i<A.length; i++)
document.write(A[i]+" ")
document.write("<br><br>")

// 4. Calculate the sum of the array elements
var sum = 0

for (var i=0; i<A.length; i++)
sum += parseFloat(A[i])

// 5. Calculate the arithmetic mean of the array elements
var avg = sum / A.length

// 6. Print the sum and the arithmetic mean
document.write("sum = " + sum + "<br>")
document.write("avg = " + avg + "<br>")```

Result

```A:
8 5 6 4 1

sum = 24
avg = 4.8```

### ⇑

##### 2. Array reversing

Task. Given a one-dimensional array of integers with dimension n elements. The array is entered from the keyboard. Form a new array in which the elements are placed in reverse order.

Solution.

```// One dimensional arrays. Task solving.

// Form a reversed array.

// 1. Arrays declaration
var A1 = [] // source array
var A2 = [] // resulting array

// 2. Set the number of elements in array A1
A1.length = prompt("n = ")

// 3. Enter the elements of array A1 in a loop
for (var i=0; i<A1.length; i++)
A1[i] = prompt("A1[" + i + "] = ")

// 4. Print n and array A1
document.write("n = " + A1.length + "<br>")

document.write("A1:<br>")
for (var i=0; i<A1.length; i++)
document.write(A1[i]+" ")
document.write("<br>")

// 4. Form a reversed array A2

for (var i=A1.length-1; i>=0; i--)
A2[A1.length-i-1] = A1[i];

// 5. Form array A2
document.write("A2:<br>")
for (var i=0; i<A2.length; i++)
document.write(A2[i] + " ")
document.write("<br>")```

Result

```n = 5
A1:
3 4 1 8 7
A2:
7 8 1 4 3```

### ⇑

##### 3. Array compression. Removing negative elements from the array

Task. An array of n elements is specified. The array is entered from the keyboard. Implement array compression by extracting all negative elements from it.

Решение. One possible solution to this problem is to create a new array containing no negative elements.

```// One dimensional arrays. Task solution.

// Task. An array of dimension n is given.
// Compress the array by removing all negative elements from it.

// 1. Declaring arrays.
var A1 = [] // source array
var A2 = [] // resulting array

// 2. Set the number of elements in array A1
A1.length = prompt("n = ")

// 3. Enter the elements of array A1 in a loop
for (var i=0; i<A1.length; i++)
A1[i] = prompt("A1[" + i + "] = ")

// 4. Output n and the original array A1
document.write("n = " + A1.length + "<br>")

document.write("A1:<br>")
for (var i=0; i<A1.length; i++)
document.write(A1[i]+" ")
document.write("<br>")

// 4. Form compressed array A2

var j = 0 // the current number of elements in the array A2

for (var i=0; i<A1.length; i++)
if (A1[i]<0)
A2[j++] = A1[i]

// 5. Display the A2 array
document.write("A2:<br>")
for (var i=0; i<A2.length; i++)
document.write(A2[i] + " ")
document.write("<br>")```

Result

```n = 8
A1:
1 -2 3 -4 5 -6 7 -8
A2:
-2 -4 -6 -8```

### ⇑

##### 4. Splitting an array into 2 parts

Task. Given a one-dimensional array of length n. Divide it into two parts so that one part has positive elements and the other part has negative elements.

Solution.

```// One-dimensional arrays. Tasks solving.

// Splitting an array into parts.

// 1. Arrays declaration
var A = [] // source array
var B = [], C = new Array() // resulting arrays

// 2. Specify the number of elements in array A
A.length = prompt("n = ")

// 3. Enter the elements of array A in a loop
for (var i=0; i<A.length; i++)
A[i] = prompt("A[" + i + "] = ")

// 4. Print n and source array A1
document.write("n = " + A.length + "<br>")

document.write("A:<br>")
for (var i=0; i<A.length; i++)
document.write(A[i]+" ")
document.write("<br>")

// 4. Form arrays B, C,
// array B - positive elements, array C - negative elements.
var nb = 0 // the current number of elements in array B
var nc = 0 // the current number of elements in array C

for (var i=0; i<A.length; i++)
if (A[i]>=0)
B[nb++] = A[i];
else
C[nc++] = A[i];

// 5. Display the array B
document.write("B:<br>")
for (var i=0; i<B.length; i++)
document.write(B[i] + " ")
document.write("<br>")

// 6. Display the array C
document.write("C:<br>")
for (var i=0; i<C.length; i++)
document.write(C[i] + " ")
document.write("<br>")```

Result

```n = 9
A:
-1 2 -3 4 -5 6 -7 8 -9
B:
2 4 6 8
C:
-1 -3 -5 -7 -9```

### ⇑

##### 5. Concatenation of two arrays

Task. Two one-dimensional arrays A1 and A2 are given, the elements of which are random integers in the interval [-10; +10]. Merge these two arrays into a third array, which will first contain the elements of the first array, and then the second, preserving their original sequence.

Solution.

```// One dimensional arrays. Tasks solving.

// Union of arrays B = A1 + A2

// 1. Declaring arrays and initializing them with random values
var A1 = new Array(), A2 = new Array() // source arrays
var B = [] // resulting array

// 2. Set the number of elements in the A1 array
A1.length = prompt("A1.length = ")

// 3. Set the number of elements in the A2 array
A2.length = prompt("A2.length = ")

// 4. Fill arrays A1, A2 with random numbers in the interval [-10; ten]
for (var i=0; i<A1.length; i++)
A1[i] = parseInt(-10 + Math.random()*(10-(-10)+1))

for (var i=0; i<A2.length; i++)
A2[i] = parseInt(-10 + Math.random()*(10-(-10)+1))

// 5. Display array A1
document.write("A1:<br>")
for (var i=0; i<A1.length; i++)
document.write(A1[i] + "\t")
document.write("<br><br>")

// 6. Display array A1
document.write("A2:<br>")
for (var i=0; i<A2.length; i++)
document.write(A2[i] + "\t")
document.write("<br><br>")

// 7. Form the resulting array B

// Form the length of array B
B.length = A1.length + A2.length

// Write the elements of array A1
for (var i=0; i<A1.length; i++)
B[i] = A1[i]

// Write the elements of array A2
for (var i=0; i<A2.length; i++)
B[A1.length + i] = parseInt(A2[i])

// 8. Display the array B
document.write("B:<br>")
for (var i=0; i<B.length; i++)
document.write(B[i] + " ")
document.write("<br>")```

For the given n1 = 9, n2 = 7, the program produced the following result

```A1:
0 -9 3 4 -6 6 1 -9 5

A2:
-3 -6 10 -3 -1 6 6

B:
0 -9 3 4 -6 6 1 -9 5 -3 -6 10 -3 -1 6 6```

### ⇑

##### 6. Sorting an array of strings using the insertion method

Task. An array of strings of dimension n is given. Sort the elements of the array in ascending order (“a”, “b”, “cd”,…).

Solution.

```// One-dimensional arrays. Tasks solving.

// Sorting an array of strings using the insertion method.

// 1. Declaring the array of strings
var A = new Array()

// 2. Display the size of array
var n = prompt("n = ")

// 3. Allocate memory for the string array
A.length = n

// 4. The cycle of forming an array of strings
for (var i=0; i<n; i++)
{
A[i] = prompt("A[" + i + "] = ")
}

// 5. Display the array A
document.write("A:<br>")
for (var i=0; i<A.length; i++)
document.write(A[i] + "<br>")
document.write("<br><br>")

// 6. Insertion sort loop
for (var i=0; i<A.length-1; i++)
for (var j=i; j>=0; j--)
if (A[j]>A[j+1])
{
// swap places
var t = A[j]
A[j] = A[j+1]
A[j+1] = t
}

// 7. Output sorted array A
document.write("-----------------------------<br>")
for (var i=0; i<A.length; i++)
document.write(A[i]+"<br>")
document.write("<br>")```

Result

```A:
kd
fcw
aet
tqw ew
abcd
wer
oiww
xlsdk

-----------------------------
abcd
aet
fcw
kd
oiww
tqw ew
wer
xlsdk```