You are on page 1of 7

Experiment – 1.

1(ARRAYS)
1.1 Aim: Given an array, A of N integers, print a’s elements in reverse
order as a single line of space separated numbers.

1.2 Input:

No of element = 5;

arr[5] = {4,3,7,6,2}

1.3 Output:

Result = 2,6,7,3,4

1.4 Code:
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    
    int arr[n];
    for(int i = 0; i < n ; i++){
        cin >> arr[i];
    }

    for(int i = n-1; i >= 0 ; i--){
        cout << arr[i] << " ";
    }

    return 0;
    // UID: 20BCS7774
}
2.1 Aim: Given an array of integers, find the sum of its elements.

Description:

Complete the simpleArraySum function in the editor below. It must return the

sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

 ar: an array of integers

2.2 Input:

No of element = 6;

arr[5] = {1,2,3,4,10,11}

2.3 Output:

Result = 31

2.4 Code:
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n];
    int s = 0;
    for(int i = 0; i < n ; i++){
        cin >> arr[i];
    }
    for(int i = 0; i < n ; i++){
        s = s + arr[i];
    }
    
    cout << s;
    return 0;
    // UID: 20BCS7774
}
3.1 Aim:

Alice and Bob each created one problem for HackerRank. A reviewer rates the
two challenges, awarding points on a scale from 1 to 100 for three
categories: problem clarity, originality, and difficulty.

The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the

rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).

The task is to find their comparison points by

comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

 If a[i] > b[i], then Alice is awarded 1 point.

 If a[i] < b[i], then Bob is awarded 1 point.

 If a[i] = b[i], then neither person receives a point.

Comparison points is the total points a person earned.

Given a and b, determine their respective comparison points.

Description:

Complete the function compare Triplets in the editor below.

Compare Triplets has the following parameter(s):


 int a[3]: Alice's challenge rating

 int b[3]: Bob's challenge rating

3.2 Input:

A=123

B=221

3.3 Output:

Result = 1 1

3.4 Code:
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a[3], b[3], al=0, bo=0;
    for(int i = 0; i < 3; i++){
        cin >> a[i];
    }
    for(int i = 0; i < 3; i++){
        cin >> b[i];
    }
    for(int i = 0; i < 3; i++){
        if(a[i] < b[i]){
            bo++;
        }
        else if(a[i] > b[i]){
            al++;
        }
    }
    cout << al << " " << bo;
    return 0;
    //UID: 20BCS7774
}
4.1 Aim:

Given a square matrix, calculate the absolute difference between the sums of its
diagonals.

Description:

Complete the diagnolDifference function in the editor below.

Diagonal Difference takes the following parameter:

 int arr[n][m]: an array of integers

4.2 Input:

3
11 2 4
456
10 8 -12

4.3 Output:

Result = 15

4.4 Code:
#include <bits/stdc++.h>

using namespace std;
int main()
{
    int n;
    cin >> n;
    int arr[n][n], s1=0, s2=0;
    
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
            
            cin >> arr[i][j];
            
            if(i == j){
                s1 = s1 + arr[i][j];
            }
            if(j == (n-i-1)){
                s2 = s2 + arr[i][j];
            }
            
        }
    }
    
    int d = s1 - s2;
    if(d >= 0){
        cout << d;
    }
    else{
        cout << -d;
    }
    
    return 0;
    //UID: 20BCS7774
}

You might also like