Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Monday, October 16, 2023

Mastering JavaScript Array Functions: A Comprehensive Guide

1. `map()`: Transforming Arrays

The `map()` function is used to create a new array by applying a provided function to each element in the original array. It is particularly handy for transforming data without modifying the original array. Here's an example:

const numbers = [1, 2, 3, 4, 5];

const squaredNumbers = numbers.map(x => x * x);

// squaredNumbers will be [1, 4, 9, 16, 25]


2. `filter()`: Filtering Arrays

`filter()` is used to create a new array that contains all elements from the original array that meet a certain condition defined by a provided function. For instance:

const numbers = [1, 2, 3, 4, 5];

const evenNumbers = numbers.filter(x => x % 2 === 0);

// evenNumbers will be [2, 4]

3. `reduce()`: Reducing Arrays

The `reduce()` function is employed to reduce an array into a single value by applying a given function cumulatively to each element. It's quite versatile, allowing you to perform a wide range of operations, such as summing an array of numbers:

const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

// sum will be 15

4. `forEach()`: Iterating Over Arrays

`forEach()` is used to iterate over an array and perform a function on each element. Unlike `map()`, it doesn't create a new array but is useful for executing side effects or performing actions on array elements:

const fruits = ["apple", "banana", "cherry"];

fruits.forEach(fruit => console.log(fruit));

// This will log each fruit to the console

5. `find()`: Finding Elements

The `find()` function returns the first element in an array that satisfies a provided condition, as defined by a given function. If no element is found, it returns `undefined`:

const people = [

  { name: "Alice", age: 25 },

  { name: "Bob", age: 30 },

  { name: "Charlie", age: 35 }

];

const alice = people.find(person => person.name === "Alice");

// alice will be { name: "Alice", age: 25 }


6. `sort()`: Sorting Arrays

The `sort()` function allows you to sort the elements of an array in place. It can be used with a comparison function for customized sorting:

const fruits = ["apple", "banana", "cherry"];

fruits.sort();

// fruits will be ["apple", "banana", "cherry"]


7. `concat()`: Merging Arrays

`concat()` is used to merge two or more arrays, creating a new array that contains the elements from all the input arrays:

const arr1 = [1, 2];

const arr2 = [3, 4];

const combined = arr1.concat(arr2);

// combined will be [1, 2, 3, 4]

Thursday, July 9, 2020

Array Rotation

left rotation operation on an array shifts each of the array's elements  unit to the left. For example, if  left rotations are performed on array , then the array would become .

Given an array  of  integers and a number, , perform  left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below. It should return the resulting array of integers.

rotLeft has the following parameter(s):

  • An array of integers .
  • An integer , the number of rotations.

Input Format

The first line contains two space-separated integers  and , the size of  and the number of left rotations you must perform.
The second line contains  space-separated integers .

Constraints

Output Format

Print a single line of  space-separated integers denoting the final state of the array after performing  left rotations.

Sample Input

5 4

1 2 3 4 5

Sample Output

5 1 2 3 4

Solution:

//code by anewbieprogrammer

#include<iostream>
using namespace std;

void rotLeft(int arr[], int r, int n){
    int temp_arr[n];
    for(int i = 0; i < n; i++){
        temp_arr[i] = arr[i];
    }
    for(int ol = 0; ol < r; ol++){
        int temp = temp_arr[ol];
        for(int il = 1; il < n; il++){
            arr[il-1] = arr[il];
        }
        arr[n-1] = temp;
    }

    for(int i = 0; i < n; i++){
        cout<<arr[i]<<" ";
    }
}
int main(){
    int n, r;
    cin >> n >> r;
    int num_arr[n];
    for(int i = 0; i < n; i++){
        cin >> num_arr[i];
    }
    rotLeft(num_arr, r, n);

    return 0;
}


Saturday, June 20, 2020

Matrix Addition

Given two matrices. Print the sum of two matrices.

Input Format:

Enter the row and column of a matrix Enter the matrix

Output Format:

Addition of a matrix

Sample Input:

2

2

1

2

3

4

1

2

3

4

Sample Output:

2 4

6 8

SOLUTION:

#include<iostream>

using namespace std;

int main(){

int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;

cin >> r >> c;

//1st matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat1[i][j];

}

}

//2nd matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat2[i][j];

}

}

// Adding Two matrices

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

for(j=0;j<c;++j){

sum[i][j]=mat1[i][j]+mat2[i][j];

}

 // print the result

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

for(j=0;j<c;++j){

cout <<sum[i][j] << " ";

if(j==c-1){

cout << "\n";

}

}

return 0;

}


Sum of Zig-Zag

Write a C++ program to find the sum of Zig-Zag pattern in a given matrix.

INPUT & OUTPUT FORMAT:

Input consists of 2 integers and 1 2D-array. Integers correspond to the size of rows and columns.

SAMPLE INPUT:

3

3

1 2 3

4 5 6

7 8 9

SAMPLE OUTPUT:

Sum of Zig-Zag pattern is 35

SAMPLE INPUT:

2

2

1 2 

4 5 

SAMPLE OUTPUT:

Sum of Zig-Zag pattern is 12

SOLUTION:

#include<iostream>

using namespace std;

int main()

{

  int r, c, i, j, sum = 0;

  cin>>r>>c;

  int mat[r][c];

  for(i = 0; i < r; i++){

    for(j = 0; j < c; j++){

      cin>>mat[i][j];

    }

  }

  for(j = 0; j < c; j++){

     sum += mat[0][j];

  }

  for(i = 1; i <= r-2; i++){

    for(j = c-2; j > 0; j--){

      sum += mat[i][j];

    }

  }

  for(j = 0; j < c; j++){

    sum += mat[r-1][j];

  }

  cout<<"Sum of Zig-Zag pattern is "<<sum;

  return 0;

}


Maximum element in each row

Raju is the maths teacher in high secondary school and provided mark sheet to students.In this class room, students are arranged in the form of rows and columns. Raju needs to find the highest mark in each rows. Help him to find out.

INPUT FORMAT:

The input consists of (m*n+2) integers. 

The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix. 

The remaining integers correspond to the elements in the matrix. 

The elements are read in row-wise order, the first row first, then second row and so on. 

Assume that the maximum value of m and n is 10.

OUTPUT FORMAT:

Refer to the sample output for details.

SAMPLE INPUT:

3

2

4 5

6 9

0 3

SAMPLE OUTPUT:

5

9

3

SOLUTION:

#include<iostream>

using namespace std;

int main(){

int r, c, temp= 0;

cin >> r >> c;

int row_arr[r];

int i, j;

int mat[r][c];

for(i = 0; i < r; i++){

for(j = 0; j < c; j++)

cin >> mat[i][j];

}

for(i = 0; i < r; i++){

      temp = mat[i][0];

  for(j = 0; j < c; j++){

      if(temp < mat[i][j]){

            temp = mat[i][j];

          }

      }

      cout<<temp<<endl;

}

return 0;

}

Greatest sum

Seenu have a fruit shop. He is arranged the some set of fruits are column and row wise. Seenu needs to find which row and column has maximum number of fruits. Help him to find out.

INPUT & OUTPUT FORMAT:

Input consists of 2 integers and 1 2D-array. Integers correspond to the size of rows and columns.

SAMPLE INPUT & OUTPUT:

3

3

1 6 8

2 5 1

3 8 2

Sum of rows is 15 8 13

Row 1 has maximum sum

Sum of columns is 6 19 11

Column 2 has maximum sum

SOLUTION:

#include<iostream>

using namespace std;

int main(){

int r, c, row, col, sum = 0, row_ind = 0, col_ind = 0;

cin >> r >> c;

int row_arr[r];

int i, j;

int mat[r][c];

for(i = 0; i < r; i++){

for(j = 0; j < c; j++)

cin >> mat[i][j];

}

int z = 0;

cout << "Sum of rows is ";

for(row=0; row<r; row++){

sum = 0;

for(col=0; col<c; col++){

sum += mat[row][col];

}

cout << sum <<" ";

row_arr[z++] = sum;

}

cout<<"\n";  

int temp_row = row_arr[0];

for(i=1;i<r;i++){

if(temp_row < row_arr[i]){

temp_row = row_arr[i];

row_ind = i;

}

}

cout<<"Row "<<row_ind + 1<<" has maximum sum"<<endl;

sum = 0;

int y = 0;

int col_arr[c];

cout<<"Sum of columns is ";

for (i = 0; i < c; ++i){

sum = 0;

for (j = 0; j < r; ++j){

sum = sum + mat[j][i];

}

cout<<sum<<" ";

col_arr[y++] = sum;

}

cout<<"\n";

int temp_col = col_arr[0];

for(i=1;i<c;i++){

if(temp_col < col_arr[i]){

temp_col = col_arr[i];

col_ind = i;

}

}

cout<<"Column "<<col_ind + 1<<" has maximum sum"<<endl;

return 0;

}

horizontal ads