0% found this document useful (0 votes)
11 views4 pages

Tushar (AP 1)

The document outlines two programming experiments from a Computer Science and Engineering lab. The first experiment focuses on reversing an array, while the second involves calculating the sum of an array's elements, including code implementations and time complexity analyses for both tasks. Key learning outcomes include input handling, the use of built-in functions, and encapsulating logic in functions.

Uploaded by

tusharsingh06.ts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Tushar (AP 1)

The document outlines two programming experiments from a Computer Science and Engineering lab. The first experiment focuses on reversing an array, while the second involves calculating the sum of an array's elements, including code implementations and time complexity analyses for both tasks. Key learning outcomes include input handling, the use of built-in functions, and encapsulating logic in functions.

Uploaded by

tusharsingh06.ts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1
Student Name: Tushar Singh UID: 22BCS15923
Branch: CSE Section/Group: 22BCS_IOT-622/B
Semester: 5th Date of Performance: 23-7-24
Subject Name: AP lab-I Subject Code: 22CSP-314

1. Aim:
Array Reversal

2. Objective:
Given an array, of size, reverse it. Example: If array, arr= {1,2,3,4,5}, after
reversing it, the array should be, arr= {5,4,3,2,1}.

3. Implementation/Code:

#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];

for(int i = 0; i < n; i++) {


scanf("%d", &arr[i]);
}

for(int i = 0; i < n / 2; i++) {


int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for(int i = 0; i < n; i++) {


printf("%d ", arr[i]);
}
return 0;
}

4. Output

5. Time Complexity
The overall time complexity of the entire process is dominated by the
reading and summing of the array elements, each of which takes
O(n) time. Thus, the overall time complexity is O(n)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim:
Given an array of integers, find the sum of its elements.

2. Objective:
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

3. Implementation/Code:

import math
import os
import random
import re
import sys

def simpleArraySum(ar):
return sum(ar)

if __name__ == '__main__':
import os
fptr = open(os.environ['OUTPUT_PATH'], 'w')

ar_count = int(input().strip())
ar = list(map(int, input().rstrip().split()))

result = simpleArraySum(ar)
fptr.write(str(result) + '\n')
fptr.close()
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Output

5. Time Complexity :
The overall time complexity of the entire program is dominated by the
input reading part, which is O(n2). The diagonal sum calculation part is
O(n), but since O(n2) is the larger term, the overall time complexity is
O(n2)

6. Learning Outcome :
 The code demonstrates how to read multiple lines of input from
the user. input().strip() is used to read and clean the input.
 The sum() function is used to calculate the sum of the elements
in a list. This shows how built-in functions can simplify
common operations.
 The map() function is used to convert input strings to integers,
and list() is used to convert the map object to a list.
 The simpleArraySum function is defined to encapsulate the logic
for summing the array elements.

You might also like