You are on page 1of 8

Introduction to Computing

Lab 13
Topic Array Searching and Sorting Algorithm
Learning objectives of this lab are to get hands on experience on the concepts of array. It is on
Objective
Searching and Sorting of an array.

Searching Algorithm:
A search algorithm is a method of locating a specific item in a collection of data. It’s very common for
programs not only to store and process data stored in arrays, but to search arrays for specific items. This
section will show you two methods of searching an array: the linear search and the binary search. Each
has its advantages and disadvantages.
Sorting Algorithm:
Sorting algorithms are used to arrange data into some order. Often the data in an array must be sorted
in some order. Customer lists, for instance, are commonly sorted in alphabetical order. Student grades
might be sorted from highest to lowest. Mailing label records could be sorted by ZIP code. To sort the
data in an array, the programmer must use an appropriate sorting algorithm. A sorting algorithm is a
technique for scanning through an array and rearranging its contents in some specific order. This section
will introduce two simple sorting algorithms: the bubble sort and the selection sort.
Task 1
Create an integer array of Size 10 and initialize it with some random numbers. Write a program that
takes array and prints it in reverse order.

Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
45,3,11,53,8,75,66,5,4,2

Task 2
Create two array called Array1 and Array2. Initialize Array1 with some random numbers. Array2 is blank
and same as size of Array1. Write a program that Array1 and Array2 and store Values of Array1 in Array2
but in reverse order and print values of array.

Array1[10]= {2,4,5,66,75,8,53,11,3,45}
Array2[10]= { , , , , , , , , , }

Output:
Array1= {2,4,5,66,75,8,53,11,3,45}
Array2={45,3,11,53,8,75,66,5,4,2}

Task 3
Create an integer array of Size 10 and initialize it with some random numbers. Take an input from user
and finds entered number into array and print not found if number does not found.
Sample 1
Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
Enter a number: 66
Index of number is: 3

Sample 2
Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
Enter a number: 100
Number is not present
Task 4
Create an integer array of Size 10 and initialize it with some random numbers. Take an input from user
and counts number of times entered number is present into array and return count.
Sample 1
Array[10]= {2,4,5,66, 5,8,53,11,3,45}
Output:
Enter a number: 5
Number is present : 2 times

Sample 2
Array[10]= {2,4,5,66,5,8,53,11,3,45}
Output:
Enter a number: 100
Number is not present

Task 5
Create an integer array of Size 10 and initialize it with some random numbers and finds minimum and
maximum number in array

Sample 1
Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
Minimum number in array is = 2
Maximum number in array is =66

Sample 2
Array[10]= {-2,-4,-5,-66,-75,-8,-53,-11,-3,-45}
Output:
Minimum number in array is = -66
Maximum number in array is =-2
Task 6
Create an integer array of Size 10 and initialize it with some random numbers. Write a program that
finds how many even number are on even index.

Sample 1
Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
Number of even number present on even index are : 1

Sample 2

Array[10]= {2,4,5,66,74,8,54,11,8,45}
Output:
Number of even number present on even index are : 4

Task 7
Create two integer array of Size 10 and initialize both with some random numbers. Find a maximum
number in Array 1 and Search it in Array2, if it is present than prints its index.
Sample 1
Array[10]= {2,4,5,66,75,8,53,11,3,45}
Output:
Minimum number in array is = 2
Maximum number in array is =66

Sample 2
Array[10]= {-2,-4,-5,-66,-75,-8,-53,-11,-3,-45}
Output:
Minimum number in array is = -66
Maximum number in array is =-2
Task 8
Write a C++ Program that finds the second maximum number in the array and print its index number.
Sample 1
Array[10]= {2,4,5,53,75,8,66,11,3,45}
Output:
The 2nd Maximum number in array is: 66

Sample 2
Array[10]= {12,41,5,66,37,98,-53,71,-3,-45}
Output:
The 2nd maximum number in array is =71

Task 9
Write a C++ Program that finds the nth maximum number in the array and print its index number.
Sample 1
Array[10]= {2,4,5,53,75,8,66,11,3,45}
Enter value: 2
Output:
The 2nd Maximum number in array is: 66

Sample 2
Array[10]= {2,4,5,53,75,8,66,11,3,45}
Enter value: 3
Output:
The 3rd maximum number in array is =53

Task 10
Write following program.
Write a program that shifts array in forward direction

1 2 3 6 5 4
After shifting
1 2 3 6 5 4

Writ a program that shifts array in backward direction


Task 11
Write program that shifts array in forward direction multiple time

1 2 3 6 5 4
After shifting 2 times
1 2 3 6 5 4 6

Write a program that shifts array in backward direction multiple time


Task 12
Write a program that rotate array towards right.

1 2 3 6 5 4
After rotation
4 1 2 3 6 5

Write a program that rotate array towards Left

Task 13
Write a program that finds a number and replace it with by another number. If there are multiple
entries than replace all entries. It also tell whether replace has been done or not?

Task 14
Write a program replace that replace number’s last occurrence only. If it present in only one time than it
should replace it.
Example 1:

5 6 7 5 9 10 58 5 14 5
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 5
Enter number to be replaced by: -1
5 6 7 5 9 10 58 5 14 -1
0 1 2 3 4 5 6 7 8 9

Example 2:
Array[5]=
12 6 7 78 9 10 58 5433 14 5
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 6
Enter number to be replaced by: -1
12 -1 7 78 9 10 58 5433 14 5
0 1 2 3 4 5 6 7 8 9

Task 15
Write a program that find number’s first and last occurrence only. If it present in only one time than it
should tell user number only occurs one time.
Example 1:

5 6 7 5 9 10 58 5 5 66
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 5
First occurrence is at: 0 index
Last occurrence is at: 8 index
Example 2:
Array[5]=
12 6 7 78 9 10 58 5433 14 5
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 6
Number is present only one time.

Task 16
Write a program that replace number only if it present in array more than 3 times
Example 1:

5 6 7 5 9 10 58 5 14 5
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 5
Enter number to be replaced by: -1
-1 6 7 -1 9 10 58 -1 14 -1
0 1 2 3 4 5 6 7 8 9

Example 2:

12 6 7 78 9 10 58 5433 14 5
0 1 2 3 4 5 6 7 8 9

Output:
Enter number to be found: 6
Enter number to be replaced by: -1
6 is not present more than 3 times hence can’t be replaced.

12 6 7 78 9 10 58 5433 14 5
0 1 2 3 4 5 6 7 8 9

You might also like