-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
88 lines (76 loc) · 1.8 KB
/
2.cpp
File metadata and controls
88 lines (76 loc) · 1.8 KB
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
#include <iostream>
using namespace std;
// Function to search for an element in the array
int searchElement(int* arr, int n, int num)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == num)
{
return i;
}
}
return -1; // Element not found
}
// Function to delete an element from the array
int* deleteElement(int* arr, int n, int index)
{
if (index < 0 || index >= n)
{
return arr; // Return the original array if index is out of bounds
}
int* b = new int[n-1];
for (int i = 0; i < index; i++)
{
b[i] = arr[i];
}
for (int i = index; i < n-1; i++)
{
b[i] = arr[i+1];
}
return b;
}
// Function to add an element at the beginning of the array
int* addBeginning(int* arr, int n, int num)
{
int* b = new int[n+1];
b[0] = num;
for (int i = 0; i < n; i++)
{
b[i+1] = arr[i];
}
return b;
}
// Function to print the elements of the array
void printList(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// Function to create a new array with each element being double of the original array
int* makeDouble(int* arr, int n)
{
int* b = new int[n];
for (int i = 0; i < n; i++)
{
b[i] = arr[i] * 2;
}
return b;
}
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printList(a, 5);
int* p = addBeginning(a, 5, 100);
printList(p, 6);
int index = searchElement(a, 5, 30);
cout << "Index of 30: " << index << endl;
int* l = deleteElement(a, 5, index);
printList(l, 4);
delete[] p; // Free dynamically allocated memory
delete[] l; // Free dynamically allocated memory
return 0;
}