AP-1.1 Ankita

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

UNIVERSITY INSTITUTE OF ENGINEERING


Department of Computer Science & Engineering

Subject Name:
Subject Code:
Submitted to: Submitted by:
Faculty name Name:
UID:
Section:
Group:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

INDEX
Ex. List of Experiments Conduct Viva Record Total Remarks/Signature
No (MM: 12) (MM: 10) (MM: 8) (MM: 30)

1.1

1.2

1.3

2.1

2.2

2.3

2.4

3.1

3.2

3.3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 1.1
Student Name:Ankita Parida UID: 21BCS8988
Branch: BE-CSE Section: 636-A
Semester: 5th Date: 10-08-23
Subject Name: Advanced Programming Lab-1 Subject Code: 21CSP-314

1. Aim: Arrays: To implement the concept of Array.

2. Objective: We will learn About Array & Data Structure

3. Problem 1.1:

Objective: Given an array A of N integers, print 's elements in reverse order as a


single line of space-separated numbers.

Pseudo Code:

1) Initialize start and end indexes as start = 0, end = n-1


2) Swap arr[start] with arr[end]
3) Recursively call reverse for rest of the array.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Code:

#!/bin/python3

import math
import os
import random
import re
import sys

if _name_ == '_main_':
n = int(input())

arr = list(map(int, input().rstrip().split()))

for i in reversed(arr):
print(i,end=' ')

Output 1.1:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 1.2:

Objective: Given an array of integers, find the sum of its elements. For example, if
the array ar = [1,2,3], 1+2+3 = 6, so return 6.

Pseudo Code:

1) Declare and initialize an array.


2) The variable sum will be used to calculate the sum of the elements. Initialize it to 0.
3) Loop through the array and add each element of array to variable sum as
sum = sum + arr[i].

Code:

arr = [1, 2, 3, 4, 10,11];


sum = 0;

for i in range(0, len(arr)):


sum = sum + arr[i];

print("Sum of all the elements of an array: " + str(sum))


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output 1.2:

1.3 Objective: 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.
Code:
vector<int> result;
int aliceScore = 0;
int bobScore = 0;
for(int i=0;i<a.size();i++){
if(a[i] > b[i])
aliceScore ++;
else if(b[i] > a[i])
bobScore++;
}
result.push_back(aliceScore);
result.push_back(bobScore);
return result;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

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

Code: #include <iostream>


using namespace std;
int main()
{
int N, LeftDiagonalSum = 0, RightDiagonal = 0;
cin >> N;
int a[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
{
cin >> a[i][j];
if (i == j)
{
LeftDiagonalSum = LeftDiagonalSum + a[i][j];
}
}
}

for (int i = 0; i < N; i++)


{
for (int j = N - 1 - i; j >= 0;)
{
RightDiagonal = RightDiagonal + a[i][j];
break;
}
}
cout << abs(LeftDiagonalSum - RightDiagonal) << endl;
return 0;
}

Output:

You might also like