Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

BAHRIA UNIVERSITY, (Karachi

Campus)
Department of Computer Engineering
Assignment 1 – Spring 2021

COURSE TITLE: Data Structures And Algorithms


COURSE CODE: CSC-221
CLO 1: [Knowledge] PLO 1:[Engineering Knowledge]
Class: BCE-4(A) Shift: Morning
Course Instructor: Engr. Naveera Sami Time Allowed: 28th Mar 2021
Max. Marks: 05 Marks
Q1. Describe a Procedure (Algorithm) FIND (DATA,N,LOC1,LOC2) which finds the location
LOC2 of the second largest element in an array DATA with n>1 elements.

SOLUTION:

1) Initialize two variables first and second to INT_MIN as first = second = INT_MIN

2) Start traversing the array,


A. If the current element in array says arr[i] is greater than first. Then update first
and second as, second = first first = arr[i]
B. If the current element is in between first and second, then update second to
store the value of current variable as second = arr[i]
3) Return the value stored in second.

You might also like