Skip to content

Commit 2df6b90

Browse files
authored
Merge pull request #23 from MHMITHUN/main
Hacktoberfest.cpp
2 parents a99eecb + 9eb8bbe commit 2df6b90

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

Algo.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
void swap(int *p1, int *p2)
3+
{
4+
int temp = *p1;
5+
*p1 = *p2;
6+
*p2 = temp;
7+
}
8+
// This is an optimised code for the bubble sort
9+
void bSort(int arrnumbers[], int n)
10+
{
11+
int i, j;
12+
bool check;
13+
for (i = 0; i < n-1; i++)
14+
{
15+
check = false;
16+
for (j = 0; j < n-i-1; j++)
17+
{
18+
if (arrnumbers[j] > arrnumbers[j+1])
19+
{
20+
swap(&arrnumbers[j], &arrnumbers[j+1]);
21+
check = true;
22+
}
23+
}
24+
// We are breaking from the loop in case two elements were not swapped by inner loop.
25+
if (check == false)
26+
break;
27+
}
28+
}
29+
//This function is to print the array sequence as final output after sorting
30+
void print(int arrnumbers[], int sizeofarray)
31+
{
32+
int i;
33+
for (i=0; i < sizeofarray; i++)
34+
printf("%d ", arrnumbers[i]);
35+
}
36+
// This the main program from where the execution will start
37+
int main()
38+
{
39+
int arrnumbers[] = {5, 6, 1, 0, 2, 9};
40+
int n = sizeof(arrnumbers)/sizeof(arrnumbers[0]);
41+
bSort(arrnumbers, n);
42+
printf("Sorted array: \n");
43+
print(arrnumbers, n);
44+
return 0;
45+
}

Array.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
6+
int numbers[5] = {7, 5, 6, 12, 35};
7+
8+
cout << "The numbers are: ";
9+
10+
// Printing array elements
11+
// using range based for loop
12+
for (const int &n : numbers) {
13+
cout << n << " ";
14+
}
15+
16+
cout << "\nThe numbers are: ";
17+
18+
// Printing array elements
19+
// using traditional for loop
20+
for (int i = 0; i < 5; ++i) {
21+
cout << numbers[i] << " ";
22+
}
23+
24+
return 0;
25+
}

String.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
char str[100];
7+
8+
cout << "Enter a string: ";
9+
cin >> str;
10+
cout << "You entered: " << str << endl;
11+
12+
cout << "\nEnter another string: ";
13+
cin >> str;
14+
cout << "You entered: "<<str<<endl;
15+
16+
return 0;
17+
}

String1.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
// Declaring a string object
7+
string str;
8+
cout << "Enter a string: ";
9+
getline(cin, str);
10+
11+
cout << "You entered: " << str << endl;
12+
return 0;
13+
}

0 commit comments

Comments
 (0)