How efficient is this code?

Hello

I have a fairly small code here that accepts an array from the user, asks them for a element and deletes that element(if present in the array).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/** Deletion in a array **/

#include <iostream>
#define EXIT_FAILURE -1
#define EXIT_SUCCESS 0

using namespace std;

/* Function searches an array */
int BinarySearch(int * ARR, int SIZE, int ELEMENT)
{
	int BEG_i = 0, MID_i, LAST_i = SIZE - 1;
	while(BEG_i <= LAST_i)
	{
		MID_i = (BEG_i + LAST_i) / 2;

		if(ARR[MID_i] == ELEMENT)
			return MID_i;
		else if(ARR[MID_i] < ELEMENT)
			BEG_i = MID_i + 1;
		else
			LAST_i = MID_i - 1;
	}
	return -1;
}

/* Function deletes the required element */
int *Delete(int *ARR, int &SIZE, int index)
{
	int temp_index;
	ARR[index] = 0;
	for(int I = index; I < SIZE - 1; I++)
	{
		ARR[I] = ARR[I + 1];
		temp_index = I + 1;
	}
	SIZE--;
	ARR[temp_index] = 0;
	return ARR;
}

int main()
{
	int *intArr= nullptr;
	int arrSize, searchElement, index = 0;

	cout << "\nEnter the size of the array: ";
	cin >> arrSize;

	intArr = new int[arrSize + 10];		/* Allocating a little extra memory in case of emergencies :D */
	if(!intArr)
	{
		cerr << "\nOops! Something went wrong!" << endl;
		system("pause");
		exit(0);
	}

	cout << "\nEnter the array:-\n" << endl;
	for(int I= 0; I < arrSize; I++)
	{
		cout << "Enter element : ";
		cin >> intArr[I];
	}
	
	cout << "\nEnter element you want to delete: ";
	cin >> searchElement;

	index = BinarySearch(intArr, arrSize, searchElement);

	if(index != -1)
	{
		Delete(intArr, arrSize, index);
	}
	
	else
	{
		cout << "\nElement \"" << searchElement << "\" not found in array!" << endl;
		cout << endl;
		system("pause");
		delete[] intArr;
		return EXIT_SUCCESS;
	}

	cout << "\nArray after deleting '" << searchElement << "' :-\n" << endl;
	for(int I = 0; I < arrSize; I++)
	{
		cout << intArr[I] << " ";
	}

	cout << endl;
	system("pause");
	delete[] intArr;
	return EXIT_SUCCESS;
}


Can you tell me how efficient it is to use it in various situations? Or is it not practically usable in most cases? And what changes do you suggest I make in it(if any are required)?

Thanks nd Cheers!
Last edited on
Topic archived. No new replies allowed.