-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsort_array_containing_zero_and_one.cpp
More file actions
61 lines (49 loc) · 1.05 KB
/
Copy pathsort_array_containing_zero_and_one.cpp
File metadata and controls
61 lines (49 loc) · 1.05 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
// Write a C++ program to sort an array containing only 0 and 1
// to run program
// g++ -o sort_array_containing_zero_and_one sort_array_containing_zero_and_one.cpp
// ./sort_array_containing_zero_and_one
#include<iostream>
using namespace std;
int main()
{
int A[100], n, i, tmp, nb_zero;
cout<<"Enter number of elements you want to insert ";
cin>>n;
nb_zero = 0;
i = 0;
while (i < n)
{
cout<<"Enter element "<<i+1<<":";
cin>>tmp;
if ((tmp == 0) || (tmp == 1))
{
if (tmp == 0)
{
nb_zero += 1;
}
A[i] = tmp;
i+=1;
} else
{
cout<<"Please enter 0 or 1\n";
}
}
// print array
cout << "\nYour initial array :";
for(i=0;i<n;i++)
{
cout << A[i] << " ";
}
// sorted array
for(i=0; i<n;i++)
{
tmp = i < nb_zero? 0 : 1;
A[i] = tmp;
}
// print sorted array
cout << "\nYour array sorted: ";
for(i=0; i<n;i++)
{
cout << A[i] << " ";
}
}