-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraymze.cpp
More file actions
99 lines (86 loc) · 1.75 KB
/
arraymze.cpp
File metadata and controls
99 lines (86 loc) · 1.75 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
89
90
91
92
93
94
95
96
97
98
99
/*
#include<iostream>
using namespace std;
void moveToend(int arr[],int n){
for(int i=0;i<n;i++){
if(arr[i]==0){
for(int j=i+1;j<n;j++){
if(arr[j]!=0){
swap(arr[i],arr[j]);
}
}
}
}
}
int main(){
int arr[]={1,2,0,0,0,3,0,4};
int n=8;
cout<<"Before function"<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"After adding function"<<endl;
moveToend(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
*/
//approach 2:-9.20
/*
#include<iostream>
using namespace std;
void moveToend(int arr[],int n){
//initializing count to store value of non zero values
int count=0;
for(int i=0;i<n;i++){
//while moving right if i not equal to zero
if(arr[i]!=0){
swap(arr[i],arr[count]);
count++;
}
}
}
int main(){
int arr[]={1,2,0,0,0,3,0,4};
int n=8;
cout<<"Before function"<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"After adding function"<<endl;
moveToend(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
*/
#include<iostream>
using namespace std;
void moveToend(int arr[],int n){
//count initializing
int count =0;
//traversing
for(int i=0;i<n;i++){
if(arr[i]!=0){
swap(arr[i],arr[count]);
count++;
}
}
}
int main(){
int arr[]={1,2,0,0,1,0};
cout<<"Before function"<<endl;
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
moveToend(arr,n);
cout<<"After function"<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}