0% found this document useful (0 votes)
86 views

Finalproject 1

This C++ program uses binary search to find the occurrences of any integer in a given array. It first outputs the array, then prompts the user to enter a search integer. The program uses recursive functions to find the first and last indices of any matches, and prints how many times the search integer occurs in the array. It performs a binary search to efficiently find the first and last matches.

Uploaded by

api-355553261
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Finalproject 1

This C++ program uses binary search to find the occurrences of any integer in a given array. It first outputs the array, then prompts the user to enter a search integer. The program uses recursive functions to find the first and last indices of any matches, and prints how many times the search integer occurs in the array. It performs a binary search to efficiently find the first and last matches.

Uploaded by

api-355553261
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/Users/Alan Ayoub/Desk/////Final_Project1_Ayoub/FinalProject1.

cpp Page 1/3


Saved: 2/25/17, 12:47:48 PM Printed for: Alan Ayoub

1 // Title: Final Project #1


2 // Abstract: This program finds the occurances of any array
3 // using a binary search. The program first outputs
4 // the given array and then prompts the user to enter
5 // an integer to search for. If the user selects an integer
6 // that is located in the array, the program will tell the
7 // user The element _ occurs _ times in the given array.
8 // Author: Alan Ayoub
9 // ID: 002566719
10 // Date: 2/25/2017
11
12 #include<iostream>
13 using namespace std;
14
15
16 // global functions for first and last
17 int first(int arr[], int low, int high, int search, int n);
18 int last(int arr[], int low, int high, int search, int n);
19
20
21 int main()
22 {
23 // We need to check where the first match number and the last match number
24 int first_occurance; // declare first_occurance as int
25 int last_occurance; // declare last_occurance as int
26 int arr[] = { 1,1,2,2,5,5,5,5,5,5,5,8,8,9,10,11,11 }; // given array
27 int search; // declare a varia
28 int count = 0; // initialize coun
29 int length = sizeof(arr) / sizeof(arr[0]); // variable for le
30 cout << "The given array is:" << endl; // prints out the
31 for (int i = 0; i < length; i++){
32
33 cout << arr[i] << " ";
34
35
36 }
37 cout << endl;
38 cout << "\nPlease enter an integer to search for: ";
39 cin >> search;
/Users/Alan Ayoub/Desk/////Final_Project1_Ayoub/FinalProject1.cpp Page 2/3
Saved: 2/25/17, 12:47:48 PM Printed for: Alan Ayoub

40 first_occurance = first(arr, 0, length - 1, search, length); // call funct


41 last_occurance = last(arr, first_occurance, length - 1, search, length); /
42
43 // checks to see if no elements match
44 if (first_occurance == -1){
45
46 count = first_occurance;
47 }
48 // if elements do match
49 else{
50 count = last_occurance - first_occurance + 1;
51 }
52
53 cout << "\nThe element " << search << " occurs " << count << " times in th
54 return 0;
55 }
56
57 // used to check the first occurance of an element in an array
58
59 int first(int arr[], int low, int high, int search, int n)
60 {
61 if (high >= low) // if this condition true
62 {
63 int mid = (low + high) / 2; // get mid element in the array
64
65 if ((mid == 0 || search > arr[mid - 1]) && arr[mid] == search) // if s
66 return mid;
67 else if (search > arr[mid]) // check if search
68 return first(arr, (mid + 1), high, search, n); // recursive call
69 else
70 return first(arr, low, (mid - 1), search, n); // recursive call
71 }
72 return -1; //nothing found
73 }
74
75
76 // same thing that we did for first occurance
77 // except in this function we start from the last element of the array
78 int last(int arr[], int low, int high, int search, int n)
/Users/Alan Ayoub/Desk/////Final_Project1_Ayoub/FinalProject1.cpp Page 3/3
Saved: 2/25/17, 12:47:48 PM Printed for: Alan Ayoub

79 {
80 if (high >= low)
81 {
82 int mid = (low + high) / 2;
83 if ((mid == n - 1 || search < arr[mid + 1]) && arr[mid] == search)
84 return mid;
85 else if (search < arr[mid])
86 return last(arr, low, (mid - 1), search, n);
87 else
88 return last(arr, (mid + 1), high, search, n);
89 }
90 return -1;
91 }
92

You might also like