Expt1 (A) AP LAB

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1(a)
Student Name: UID:
Branch: CSE Section/Group:
Semester: 5th Date of Performance:
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314

1. Title: Array Reversal

2. Aim: 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. Objective: The objective of the problem "Given an array of size 𝑛 , reverse


it" is to write a function that takes an array of a given size as input and
returns a new array where the order of the elements is reversed.

4. Implementation/Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}

int temp;
for(i = 0; i < num / 2; i++) {
temp = arr[i];
arr[i] = arr[num - i - 1];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

arr[num - i - 1] = temp;
}
for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}

5. Output:

6. Time Complexity: O(n)


7. Space Complexity: O(n)

You might also like