-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays_13.cpp
93 lines (91 loc) · 2.39 KB
/
arrays_13.cpp
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
// insert an element into the first index of an array
#include <iostream>
using namespace std;
int main()
{
int n, value, options;
cout << "Enter the size of the array: ";
cin >> n;
int a[n];
cout << "Enter the elements of the array: " << endl;
for (int i = 0; i < n; i++)
{
cout << "a[" << i << "] = ";
cin >> a[i];
}
cout << "Displaying the array: " << endl;
cout << "[";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << "]";
cout << endl;
Label:
cout << "1. Insert an element in first index in the array"
<< "\n"
<< "2.Exit" << endl;
cin >> options;
switch (options)
{
case 1:
cout << "Enter the value of the element: ";
cin >> value;
n = n + 1;
for (int i = n - 1; i >= 0; i--)
{
a[i] = a[i - 1];
if (i == 0)
{
break;
}
}
a[0] = value;
cout << "Array after inserting the element: " << endl;
cout << "[";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << "]";
cout << endl;
goto Label;
break;
case 2:
exit(0);
break;
default:
cout << "Invalid choice" << endl;
return 0;
}
}
/******************************
* Workings:
* --------------
* Enter the size of the array: 3
* a[0] = 1
* a[1] = 2
* a[2] = 3
* Displaying the Matrix:
* [1 2 3 ]
* 1. Insert an element in first index in the array
* Enter the value of the element: 4
* Size = size + 1 = 4
* for(i = (size - 1)=(4-1)= 3; i >= 0; i--)
* {
* array[i] = array[i - 1];
* i.e. array[3] = array[3 - 1] = array[2]=3
* i.e. array[2] = array[2 - 1] = array[1]=2
* i.e. array[1] = array[1 - 1] = array[0]=1
* if: i == 0
* i.e. It will happen :array[0] = array[0 - 1] = array[-1]=0
* Hence, break;
* }
* Now the array is: "
* [1 1 2 3 ] as a[0] is already 1
* Next:
* array[0] = value;
* i.e. array[0] = 4
* Now the array is:
* [4 1 2 3 ]
* ********************/