Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //You are given an array of n integers and three integers, a value to be replaced, the replacement value and occurrence to be replaced. Replace given occurred old value in the array with the replacement value.
- //Input:
- //Inputs are positive integer n followed by n integers and three integers, a value to be replaced, the replacement value and occurrence to be replaced.
- //Output:
- //Outputs are array values before and after replacement separated by a newline; array values are separated by spaces.
- #include<iostream>
- using namespace std;
- int main(){
- int a[100],n,value,occ,rep_value,counter=0;
- cin>>n;
- for(int i=0;i<n;i++){
- cin>>a[i];
- }
- cout<<endl;
- for(int i=0;i<n;i++){
- cout<<a[i]<<" ";
- }
- cout<<endl;
- cin>>value>>rep_value>>occ;
- cout<<endl;
- for(int i=0;i<n;i++){
- if(a[i]==value){
- counter++;
- }
- if(counter==occ){
- a[i]=rep_value;
- }
- }
- for(int i=0;i<n;i++){
- cout<<a[i]<<" ";
- }
- cout<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment