Skip to content

Commit a91fef8

Browse files
committed
Add files via git
0 parents  commit a91fef8

File tree

160 files changed

+11102
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+11102
-0
lines changed

Diff for: Array initializations & operations/.directory

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Dolphin]
2+
HeaderColumnWidths=559,108,138,186
3+
Timestamp=2022,6,22,3,23,0.424
4+
Version=4
5+
ViewMode=1
6+
VisibleRoles=Details_text,Details_size,Details_modificationtime,Details_type,CustomizedDetails
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
int A[5] = {10, 20, 30, 40, 50};
6+
7+
int arrayLength = sizeof(A)/sizeof(A[0]);
8+
for (int i = 0; i < arrayLength; i++) {
9+
cout << "[" << A[i] << "] ";
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
int A[5];
6+
A[0] = 10;
7+
A[1] = 20;
8+
A[2] = 30;
9+
A[3] = 40;
10+
A[4] = 50;
11+
12+
int arrayLength = sizeof(A)/sizeof(A[0]);
13+
for (int i = 0; i < arrayLength; i++) {
14+
cout << "[" << A[i] << "] ";
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
int n;
6+
cout << "Enter the size of array: ";
7+
cin >> n;
8+
9+
int *A = new int [n]; // Memory allocation
10+
for (int i = 0; i < n; i++){
11+
cout << "Enter the value of index " << i << ": ";
12+
cin >> A[i];
13+
}
14+
15+
cout << endl;
16+
17+
for (int i = 0; i < n; i++){
18+
cout << "[" << A[i] << "] ";
19+
}
20+
delete A; // Memory deallocation
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int deletion(int A[], int size, int position, int value){
5+
position = position-1; // convert to index
6+
if(position >= 0 && position < size){
7+
int prev, next;
8+
for (int i = 0; i < size; i++) {
9+
if (i >= position && i < size) {
10+
A[i] = A[i+1];
11+
}
12+
}
13+
return --size;
14+
} else {
15+
cout << "Invalid position! The position must be less than or equal to the A length." << endl << endl;
16+
return 0;
17+
}
18+
}
19+
20+
void print(int A[], int size){
21+
for (int i = 0; i < size; i++) {
22+
cout << "[" << A[i] << "] ";
23+
}
24+
cout << endl;
25+
}
26+
27+
int main(){
28+
int A[] = {1, 2, 3, 4, 5};
29+
int size = 5;
30+
size = deletion(A, size, 3, 9); // Parameters: Array, size, position => the function will return the updated size of arrray
31+
print(A, size);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int deletion(int A[], int size, int position){
5+
if(position >= 0 && position <= size){
6+
A[position-1] = A[size-1];
7+
return --size;
8+
} else {
9+
cout << "Invalid position! The position must be less than or equal to the A length." << endl << endl;
10+
return 0;
11+
}
12+
}
13+
14+
void print(int A[], int size){
15+
for (int i = 0; i < size; i++) {
16+
cout << "[" << A[i] << "] ";
17+
}
18+
cout << endl;
19+
}
20+
21+
int main(){
22+
int A[] = {3, 1, 5, 2, 4};
23+
int size = 5;
24+
size = deletion(A, size, 3); // Parameters: Array, size, position => the function will return the updated size of arrray
25+
print(A, size);
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void deletion(){
5+
int size, loop, position, value;
6+
7+
cout << "Enter the size of array: ";
8+
cin >> size;
9+
int A[size];
10+
for( int i = 0; i < size; i++ ){
11+
cout << "Enter a value for the position of [" << i+1 << "]: ";
12+
cin >> A[i];
13+
}
14+
15+
cout << endl << "Array is => ";
16+
for( int i = 0; i < size; i++ ){
17+
cout << "[" << A[i] << "] ";
18+
}
19+
cout << endl;
20+
21+
cout << "How many values do you want to delete from this array?: ";
22+
cin >> loop;
23+
24+
for( int i = 0; i < loop; i++ ){
25+
cout << endl << "[Delete-"<<i+1<<"] Enter the Postion: " ;
26+
cin >> position;
27+
position = position-1;
28+
if(position >= 0 && position < size){
29+
for( int x = position; x < size-(i+1); x++ ){
30+
A[x] = A[x+1];
31+
}
32+
33+
cout << "Array is => ";
34+
for(int y = 0; y < size-(i+1); y++){
35+
cout << "[" << A[y] << "] ";
36+
}
37+
cout << endl;
38+
}else{
39+
cout << "Invalid position! The position must be less than or equal to the A length";
40+
break;
41+
}
42+
}
43+
}
44+
45+
int main(){
46+
deletion();
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void insertion(int A[], int size, int position, int value){
5+
position = position-1; // convert to index
6+
if(position >= 0 && position < size){
7+
for (int i = 0; i < size; i++) {
8+
if (i >= position) {
9+
int prev, next;
10+
if (i == position) {
11+
prev = A[i];
12+
A[i] = value;
13+
} else {
14+
A[i] = prev;
15+
prev = next;
16+
}
17+
if (i+1 < size) next = A[i+1];
18+
}
19+
}
20+
} else {
21+
cout << "Invalid position! The position must be less than or equal to the A length." << endl << endl;
22+
}
23+
}
24+
25+
void print(int A[], int size){
26+
for (int i = 0; i < size; i++) {
27+
cout << "[" << A[i] << "] ";
28+
}
29+
cout << endl;
30+
}
31+
32+
int main(){
33+
int A[] = {1, 2, 3, 4, 5};
34+
insertion(A, 5, 3, 9); // Parameters: Array, size, position, value
35+
print(A, 5);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void insertion(int A[], int size, int position, int value){
5+
position = position-1; // convert to index
6+
if (position >= 0 && position < size) {
7+
for( int i=size-1; position < i; i-- ){
8+
A[i] = A[i-1];
9+
}
10+
A[position] = value;
11+
} else {
12+
cout << "Invalid position! The position must be less than or equal to the A length." << endl << endl;
13+
}
14+
}
15+
16+
void print(int A[], int size){
17+
for (int i = 0; i < size; i++) {
18+
cout << "[" << A[i] << "] ";
19+
}
20+
cout << endl;
21+
}
22+
23+
int main(){
24+
int A[] = {1, 2, 3, 4, 5};
25+
insertion(A, 5, 3, 9); // Parameters: Array, size, position, value
26+
print(A, 5);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int *merge(int A[], int sizeA, int B[], int sizeB){
5+
int size = sizeA+sizeB, temp = 0;
6+
int *Array = new int[size];
7+
for(int i = 0; i < size; i++){
8+
Array[i] = (i < sizeA) ? A[i] : B[temp];
9+
if(i >= sizeA) temp++;
10+
}
11+
return Array;
12+
}
13+
14+
void print(int A[], int size){
15+
for (int i = 0; i < size; i++) {
16+
cout << "[" << A[i] << "] ";
17+
}
18+
cout << endl;
19+
}
20+
21+
int main(){
22+
int A[] = {1, 3, 5, 7, 9};
23+
int B[] = {2, 4, 6, 8, 10};
24+
25+
int sizeA = sizeof(A)/sizeof(A[0]);
26+
int sizeB = sizeof(B)/sizeof(B[0]);
27+
28+
int *merged = merge(A, sizeA, B, sizeB);
29+
print(merged, (sizeA+sizeB));
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void selectionSort_ASC(int Array[], int size){
5+
for(int x = 0; x < size-1; x++){
6+
int temp = x;
7+
for(int y = x+1; y < size; y++){
8+
if(Array[y] < Array[temp]) temp = y;
9+
}
10+
if(temp != x){
11+
int swap = Array[x];
12+
Array[x] = Array[temp];
13+
Array[temp] = swap;
14+
}
15+
}
16+
}
17+
18+
int *merge(int A[], int sizeA, int B[], int sizeB){
19+
int size = sizeA+sizeB;
20+
int *sort = new int[size];
21+
if(sizeA > 0 && sizeB > 0){
22+
int i = 0, x = 0, y = 0;
23+
while(x < sizeA && y < sizeB){
24+
if(A[x] <= B[y]){
25+
sort[i++] = A[x++];
26+
} else {
27+
sort[i++] = B[y++];
28+
}
29+
}
30+
while(x < sizeA){
31+
sort[i++] = A[x++];
32+
}
33+
while(y < sizeB){
34+
sort[i++] = B[y++];
35+
}
36+
} else {
37+
cout << "Merge not possible" << endl;
38+
}
39+
return sort;
40+
}
41+
42+
void print(int A[], int size){
43+
for (int i = 0; i < size; i++) {
44+
cout << "[" << A[i] << "] ";
45+
}
46+
cout << endl;
47+
}
48+
49+
int main(){
50+
int A[] = {4, 3, 10, 8, 7};
51+
int B[] = {2, 1, 6, 5, 9};
52+
53+
int sizeA = sizeof(A)/sizeof(A[0]);
54+
int sizeB = sizeof(B)/sizeof(B[0]);
55+
56+
selectionSort_ASC(A, sizeA);
57+
selectionSort_ASC(B, sizeB);
58+
59+
int *merged = merge(A, sizeA, B, sizeB);
60+
print(merged, (sizeA+sizeB));
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void reverse(int A[], int n){
5+
if(n > 1){
6+
int j = n-1;
7+
for(int i = 0; i < j; i++, j--){
8+
int swap = A[i];
9+
A[i] = A[j];
10+
A[j] = swap;
11+
}
12+
} else {
13+
cout << "Array must have more than 1 elements" << endl;
14+
}
15+
}
16+
17+
void print(int A[], int n){
18+
for (int i = 0; i < n; i++) {
19+
cout << "[" << A[i] << "] ";
20+
}
21+
cout << endl;
22+
}
23+
24+
int main(){
25+
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
26+
int n = sizeof(A)/sizeof(A[0]);
27+
reverse(A, n);
28+
print(A, n);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void reverse(int A[], int start, int end){
5+
if(start >= end) return;
6+
int swap = A[start];
7+
A[start] = A[end];
8+
A[end] = swap;
9+
reverse(A, start+1, end-1);
10+
}
11+
12+
void print(int A[], int n){
13+
for (int i = 0; i < n; i++) {
14+
cout << "[" << A[i] << "] ";
15+
}
16+
cout << endl;
17+
}
18+
19+
int main(){
20+
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
21+
int n = sizeof(A)/sizeof(A[0]);
22+
reverse(A, 0, n-1);
23+
print(A, n);
24+
}

0 commit comments

Comments
 (0)